文章目录
Maven是基于项目对象模型(pom),可以进行项目的管理,项目的构建,仓库管理jar包。
mvn项目的目录结构:
maven 仓库
概念:仓库就是存放依赖和插件的地方,可以分为本地仓库和远程仓库
本地仓库路径和远程仓库的地址都是通过maven的settings.xml文件配置的。
maven在加载依赖时,先在本地仓库进行查找,若没有找到则去远程仓库查找
本地仓库路径示例:
<localRepository>E:\maven\repo</localRepository>
远程镜像仓库地址示例:
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
<mirror>
<id>mirrorId3</id>
<mirrorOf>repositoryId3</mirrorOf>
<name>Human Readable Name for this Mirror2.</name>
<url>http://mvnrepository.com</url>
</mirror>
maven的生命周期
完整的项目构建过程包括:清理、编译、测试、打包、集成测试、验证、部署
maven生命周期:
clean 清理项目
pre-clean 执行清理前的工作
clean 清理上一次构建生成的所有文件
post-clean 执行清理后的文件
default 构建项目(最核心)
compile 编译
test 测试
package 打包
install 将打好的jar包安装到本地仓库
site 生成项目站点
pre-site 在生成项目站点前要完成的工作
site 生成项目的站点文档
post-site 在生成项目站点后要完成的工作
site-deploy 发布生成的站点到服务器上
对于我们平常的使用比较重要的就是clean、compile,test,package,install 阶段
maven常用的命令
mvn -v 查看版本
mvn clean 清除是删除target目录(用于存储编译的字节码文件和测试报告)
mvn compile 编译
mvn test 测试
mvn package 打包
mvn install 安装jar包到本地仓库中
maven命令的执行顺序:根据maven的生命周期来先后执行,即若执行mvn package命令,则maven会自动执行,mvn clean命令和maven compile命令
maven 命令的执行:进入项目的根目录,执行maven命令即可,例如:
pom.xml文件
pom是maven项目的核心管理文件用于:项目描述、组织管理、依赖管理、构建信息管理
pom.xml示例及基本标签的简单介绍
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion><!--指定了当前pom的版本 -->
<!--坐标信息 -->
<groupId>com.perdesk</groupId><!--主项目的标识 反写的公司网址+项目名 -->
<artifactId>PerDesk</artifactId><!--模块标识 项目名+模块名 -->
<packaging>war</packaging><!--指定maven项目打包的方式,不指定默认是jar,war,zip,pom -->
<!--第一个0表示大版本号 第二个0表示分支版本号 第三个0表示小版本号 -->
<!--snapshot 快照版本 alpha 内部测试版本 beta 公测版本 release稳定版本 ga正式发布版本 -->
<version>0.0.1-SNAPSHOT</version><!--项目的版本号 -->
<name>PerDesk Maven Webapp</name><!--项目的描述名 -->
<url>http://maven.apache.org</url><!--项目的地址 -->
<description></description><!--项目描述 -->
<developers></developers><!-- 开发人员列表 -->
<licenses></licenses><!-- 许可证的信息 -->
<organization></organization><!-- 组织信息 -->
<dependencies><!--依赖列表 -->
<dependency><!--依赖项 -->
<!--坐标信息 -->
<groupId>junit</groupId><!--项目的包名-->
<artifactId>junit</artifactId><!--模块名(项目名)-->
<version>4.9</version>!--版本(SNAPSHOT-快照版本)-->
<type></type>
<scope>test</scope><!--指定依赖的范围 test 表示 Junit 只在测试的依赖范围内有用 -->
<optional></optional><!--true or false 用来设置依赖是否可选 -->
<exclusions><!--排除依赖传递列表 -->
<exclusion></exclusion>
</exclusions>
</dependency>
</dependencies>
<!--依赖的管理 -->
<dependencyManagement><!--主要用于定义在父模块中,用于子模块继承 -->
<dependencies>
<dependency>
</dependency>
</dependencies>
</dependencyManagement>
<build><!--为构建行为提供相应的支持 -->
<plugins><!--插件列表 -->
<plugin>
<groupId></groupId>
<artifactId></artifactId>
<version>4</version>
</plugin>
</plugins>
</build>
<parent></parent><!--用于子模块对于父模块pom的继承 -->
<modules><!--用来聚合运行多个maven项目 -->
</modules>
</project>
几个常用的pom元素解析
scope:maven的依赖范围设置
maven有三种依赖范围:编译,测试,运行例如:
<scope>test</scope>
除了test外还有:
<!--compile:默认的范围,编译测试运行都有效-->
<!--provided :在编译和测试时有效,在最后运行时不会被加入-->
<!--runtime:在测试和运行时有效 jdbc驱动-->
<!--test:在测试时有效 Junit-->
<!--system:与本机系统相关联,可移植性差 在测试和运行时有效-->
<!--import:导入的范围,它只使用在dependencyManagement中表示从其他的pom文件中导入dependecy的配置-->
exclusion:排除依赖传递列表
说明:若A依赖于B,B依赖于C
当A引入B时,这时C也会引入进来,若不想引入C则可以用将其排除。
例如:下面这种情况会将spring-webmvc依赖的commons-logging排除掉,这样我们就可以引入自己想要的commons-logging版本了
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<!-- replace with jcl-over-slf4j -->
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
dependencyManagement:依赖版本号的管理
说明:如果dependencies里的dependency自己没有声明version元素,那么maven就会到dependencyManagement里面去找有没有对该artifactId和groupId进行过版本声明。
例如:
//只是对版本进行管理,不会实际引入jar
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.7</version>
</dependency>
</dependencies>
</dependencyManagement>
//会实际下载jar包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
module:多模块的聚集管理
说明:用于父子项目中,在父项目中定义modules,在子项目中引用即可,示例:
父项目:
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>service</module>
<module>admin</module>
</modules>
子项目admin进行引用
<parent>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
profile:构建自定义的运行环境
profile可以构建自定义的运行环境,我们的项目一般会有多个运行环境,例如dev,qa,uat,prod的环境,在不同的环境下,会加载不同的配置文件。
profile配置示例:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>DEV</env>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<env>QA</env>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<env>PROD</env>
</properties>
</profile>
</profiles>
profile的激活:
Maven给我们提供了多种不同的profile激活方式。
我们可以使用-P参数显示的激活一个profile,例如在Jekins的配置 则指定了要构建qa环境,此时就会加载QA环境的配置文件。
我们可以在profile中的activation元素中指定激活条件,当没有指定条件,然后指定activeByDefault为true的时候就表示当没有指定其他profile为激活状态时,该profile就默认会被激活。所以当我们调用mvn package的时候上面的dev将会被激活,但是当我们使用mvn package –P qa的时候将激活qa,而这个时候qa将不会被激活。
build:自定义项目的编译与打包
build标签描述了如何来编译及打包项目,而具体的编译和打包工作是通过build中配置的 plugin 来完成。
build标签的简单示例:
<build>
<finalName>samplecode</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>env/**/*.*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/env/${env}</directory>
<targetPath>env</targetPath>
<includes>
<include>config.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
build分为两种:一种是project标签下的build
另一种是profile标签下的build
两种模式示例:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
一种build被称为Project Build,即是project的直接子元素。
另一种build被称为Profile Build,即是profile的直接子元素。
Profile Build包含了基本的build元素,而Project Build还包含两个特殊的元素,即各种…Directory和extensions
共用的基本build元素
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
说明:
defaultGoal,执行构建时默认的goal或phase,如jar:jar或者package等
directory,构建的结果所在的路径,默认为${basedir}/target目录
finalName,构建的最终结果的名字,该名字可能在其他plugin中被改变
resources:指定配置文件路径
资源往往不是代码,无需编译,而是一些properties或XML配置文件,构建过程中会往往会将资源文件从源路径复制到指定的目标路径。
resources给出各个资源在Maven项目中的具体路径。示例如下
<build>
...
<resources>
<resource>
<targetPath>META-INF/plexus</targetPath>
<filtering>false</filtering>
<directory>${basedir}/src/main/plexus</directory>
<includes>
<include>configuration.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<testResources>
...
</testResources>
...
</build>
说明:
resources,build过程中涉及的资源文件
targetPath,资源文件的目标路径
filtering,构建过程中是否对资源进行过滤,默认false
directory,资源文件的路径,默认位于basedir/src/main/resources/目录下
includes,一组文件名的匹配模式,被匹配的资源文件将被构建过程处理
excludes,一组文件名的匹配模式,被匹配的资源文件将被构建过程忽略。同时被includes和excludes匹配的资源文件,将被忽略。
filters,给出对资源文件进行过滤的属性文件的路径,默认位于basedir/src/main/filters/目录下。属性文件中定义若干键值对。在构建过程中,对于资源文件中出现的变量(键),将使用属性文件中该键对应的值替换。
testResources,test过程中涉及的资源文件,默认位于basedir/src/test/resources/目录下。这里的资源文件不会被构建到目标构件中
plugins:给出构建过程中所用到的插件
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
</plugins>
</build>
说明:
groupId:构件坐标
artifactId:构件坐标
version:构件坐标
extensions,是否加载该插件的扩展,默认false
inherited,该插件的configuration中的配置是否可以被(继承该POM的其他Maven项目)继承,默认true
configuration,该插件所需要的特殊配置,在父子项目之间可以覆盖或合并
dependencies,该插件所特有的依赖类库
executions,该插件的某个goal(一个插件中可能包含多个goal)的执行方式。一个execution有如下设置:
id,唯一标识
goals,要执行的插件的goal(可以有多个),如run
phase,插件的goal要嵌入到Maven的phase中执行,如verify
inherited,该execution是否可被子项目继承
configuration,该execution的其他配置参数
pluginManagement:插件的父级声明
插件的声明,一般用于父子项目插件版本的管理
一般是父项目进行插件的声明,此时不会加载声明的插件,子项目直接进行引用并加载声明的插件。类似于dependencyManagement与dependencies之间的关系。
若子项目对声明的插件进行了单独的配置,则会覆盖父项目中声明的插件
例如:父项目中的声明
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
子项目中的引用
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>
properties:常量的声明
例如:常量的声明
<properties>
<spring.version>4.0.3.RELEASE</spring.version>
<spring.data.version>1.5.1.RELEASE</spring.data.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<hibernate.jpa.version>1.0.0.Final</hibernate.jpa.version>
<hqldb.version>2.2.8</hqldb.version>
<logback.version>1.1.1</logback.version>
<jackson.version>2.7.3</jackson.version>
<shiro.version>1.2.3</shiro.version>
</properties>
常量的使用:用${常量名}进行使用
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
更多Java相关文章请关注公众号:九刀流