1. 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/xsd/maven-4.0.0.xsd">
<!-- 声明项目描述符遵循哪一个POM模型版本。 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目的全球唯一标识符,通常使用全限定的包名区分该项目和其他项目。-->
<groupId>com.hyou</groupId>
<!--
构件的标识符,它和group ID一起唯一标识一个构件。
换句话说,你不能有两个不同的项目拥有同样的artifact ID和groupID。
在某个 特定的group ID下,artifact ID也必须是唯一的。
-->
<artifactId>ctms-v2-util</artifactId>
<!-- 项目当前版本,格式为:主版本.次版本.增量版本-限定版本号 -->
<version>2.0.0</version>
<!-- 项目产生的构件类型,例如jar、war、ear、pom等等 -->
<packaging>jar</packaging>
<!-- 项目的名称 -->
<name>ctms-v2-util</name>
<!-- 项目主页的URL -->
<url>http://maven.apache.org</url>
<!-- 全局变量属性定义。定义一些变量,在pom文件的其他位置可以使用 -->
<properties>
<!--
这里面子节点名称可以自定,即为变量名。
节点的值即变变量值。
使用的时候类似EL表达式的写法: ${spring.version}, ${yourVarName}
-->
<spring.version>4.2.0.RELEASE</spring.version>
<yourVarName>yourValue</yourVarName>
</properties>
<!-- 远程仓库发布配置 -->
<distributionManagement>
<repository>
<id>releases</id>
<name>192.168.0.6 nexus repository</name>
<url>http://192.168.0.6:9999/nexus/content/repositories/releases</url>
</repository>
</distributionManagement>
<!-- 远程仓库代理配置 -->
<repositories>
<repository>
<id>nexus-myland</id>
<name>192.168.0.6 nexus repository</name>
<url>http://192.168.0.6:9999/nexus/content/groups/public/</url>
</repository>
</repositories>
<!-- 依赖配置,项目使用的各种类库都通过该依赖配置加载 *** -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<!-- 构建项目使用 -->
<build>
<!-- 一般配置一些插件,用于实现编译、打包、测试等等应用场景 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- 不同插件有不同的配置参数 -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</build>
</project>
2. 配置补充说明
2.1. 关于版本号的管理
-
常见的GNU风格的版本号命名格式:
主版本号 . 子版本号 [. 修正版本号 [. 编译版本号 ]]
示例: 4.2.5, 4.2.8.1
-
GNU风格的版本号管理策略:
- 项目初版本时,版本号可以为 0.1 或 0.1.0, 也可以为 1.0 或 1.0.0
- 当项目在进行了局部修改或 bug 修正时,主版本号和子版本号都不变,修正版本号加 1
- 当项目在原有的基础上增加了部分功能时,主版本号不变,子版本号加 1,修正版本号复位为 0,因而可以被忽略掉.
- 当项目在进行了重大修改或局部修正累积较多,而导致项目整体发生全局变化时,主版本号加 1
- 编译版本号一般是编译器在编译过程中自动生成的,我们只定义其格式,并不进行人为控制.
-
maven中的快照版本与正式版本
- release : 发布库
- snapshot : 快照库
2.2. 依赖配置中的scope节点属性
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
- scope节点可选,用于标记该依赖的库用于哪个环节。
- 可选范围如下:
- compile :默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。适用于所有阶段,==会随着项目一起发布==。
- provided :跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时==没有传递性==。
- runtime :表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。
- test :表示dependency作用在测试时,不作用在运行时。 只在测试时使用,用于编译和运行测试代码。==不会随项目发布==。
- system : 跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。
3. 基本命令(操作)
3.1. compile
mvn compile
编译源代码,编译后在target目录下生成相应的class等编译结果文件。
3.2. test
mnv test
执行测试,基于JUnit。它会运行test代码目录下的所有可运行的JUnit测试代码。
3.3. package
mvn package
打包指令,将project打包成jar文件或war文件等。根据pom.xml中的packaging属性配置打包成相应的文件。
生成的包文件命名格式如下:
[artifactId]-[version].jar
例如pom.xml中对project配置:
<groupId>com.hyou</groupId>
<artifactId>ctms-framework</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>
则打包生成的文件名如下:
ctms-framework-2.0.0.jar
[artifactId].war
例如:
xbv2.war
3.4. source:jar
mvn source:jar
源代码打包指令,将java源文件等源代码打成jar文件。
生成的包文件命名格式如下:
[artifactId]-[version]-source.jar
例如pom.xml中对project配置:
<groupId>com.hyou</groupId>
<artifactId>ctms-framework</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>
ctms-framework-2.0.0-sources.jar
3.5. install
mvn install
将当前工程的代码打包并发布到本地仓库。在本地仓库生成相应的jar文件等。
在本地仓库中生成的jar文件命名格式与package打成的jar包命名方式相同。
4. 部分插件
4.1. 编译相关的插件
与 mvn compile, mvn package 等编译相关的指令有关。
配置示例:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
4.2. 单元测试相关的插件
与 mvn test 单元测试指令有关。不显式配置则会按照默认配置处理。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
4.3. 单元测试覆盖率统计相关的插件
与JUnit相结合的辅助工具,用于检查JUnit测试用例对所测试源代码的覆盖率。可以生成单元测试报告和相关的代码覆盖率统计。
<!-- 单元测试覆盖率 -->
<!-- cobertura:cobertura -->
<!-- cobertura:clean -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
参考指令:
// 代码覆盖率统计执行
mvn cobertura:cobertura
// 清空cobertura插件运行结果
mvn cobertura:clean
4.4. 源代码打包相关的插件
将工程源代码打包成jar文件
<!-- 源代码打Jar包 -->
<!-- source:jar -->
<!-- package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>spring.properties</exclude>
<exclude>applicationContext.xml</exclude>
<exclude>conf/**</exclude>
</excludes>
</configuration>
</plugin>
4.5. jetty web容器运行插件
web工程下,开发时用于启动web容器。 这个6.1.26版本,只支持servlet2.X版本。
<!-- 启动命令: jetty:run -->
<!-- reload参数: [manual|automatic] -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<contextPath>/xbv2</contextPath>
<webDefaultXml>${project.basedir}/src/main/webapp/WEB-INF/webdefault.xml</webDefaultXml>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
</connector>
</connectors>
<reload>manual</reload>
<scanIntervalSeconds>60</scanIntervalSeconds>
</configuration>
</plugin>
这个9.3.11.v20160721版本较高,可支持高版本servlet。不过与现有的web工程里一些路径的写法有点不大兼容。
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.11.v20160721</version>
<configuration>
<scanIntervalSecond>20</scanIntervalSecond>
<webApp>
<contextPath>/xbv2</contextPath>
</webApp>
<classesDirectory>${project.basedir}/target/classes</classesDirectory>
<httpConnector>
<port>8080</port>
</httpConnector>
</configuration>
</plugin>
5. 相关资源
- Maven Repository : http://mvnrepository.com/
- Apache中央仓库: https://repo.maven.apache.org/maven2
- Maven官网 : http://maven.apache.org/
- github