0x01 生命周期
clean
- pre-clean:执行清理前的工作。
- clean:清理上一次构建生成的所有文件。
- post-clean:执行清理后的工作。
default
- validate:
- initialize:
- generate-sources:
- process-sources:
- generate-resources:
- process-resources:复制和处理资源文件到target目录,准备打包。
- compile:编译项目的源代码。
- process-classes:
- generate-test-sources:
- process-test-sources:
- generate-test-resources:
- process-test-resources:
- test-compile:编译测试源代码。
- process-test-classes:
- test:运行测试代码。
- prepare-package:
- package:打包成jar或者war或者其他格式的分发包。
- pre-integration-test:
- integration-test:
- post-integration-test:
- verify:
- install:将打好的包安装到本地仓库,供其他项目使用。
- deploy:将打好的包安装到远程仓库,供其他项目使用。
pluginManagement、dependencyManagement和plugins、denpendency的区别
pluginmManagement和dependencyManagement标签一般用在父pom中,子元素可以包含plugins插件和denpendency。
Management只是声明,并不会触发插件的调用和依赖,必须在pom中去调用相应的plugins插件或denpendency。
0x02 maven-compiler-plugin插件
指定编译参数
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source> <!-- 使用java8的语言功能 -->
<target>1.8</target> <!-- 编译出来的classes文件兼容jvm1.8 -->
<encoding>UTF8</encoding> <!-- 指定文件编码为utf-8 -->
<compilerArgs> <!-- 编译时依赖lib目录 -->
<arg>-extdirs</arg>
<arg>${basedir}/lib</arg>
</compilerArgs>
</configuration>
</plugin>
0x03 build-helper-maven-plugin插件
指定多个src、resource、test目录
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id> <!-- execution的id -->
<phase>generate-sources</phase> <!-- execute绑到的生命周期 -->
<goals> <!-- execute指定的目标(可选值add-source、add-resource、add-test-source) -->
<goal>add-source</goal>
</goals>
<configuration> <!-- 配置具体的目录 -->
<sources>
<source>${basedir}/gen/</source>
<source>${basedir}/confgen</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
0x04 maven-assembly-plugin
打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors> <!-- 指定assembly文件路径 -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest> <!-- 指定manifest -->
<mainClass>xxx</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
0x05 maven-dependency-plugin
拷贝依赖
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals> <!-- 可选值:copy、unpack、copy-dependencies、unpack-dependencies-->
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<!-- <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer> -->
</configuration>
</execution>
</executions>
</plugin>
copy:拷贝单个文件到某个目录。
unpack:拷贝单个文件到某个目录并且解压。
copy-dependencies:拷贝依赖到某个目录。
unpack-dependencies:拷贝依赖到某个目录并且解压。
0x06 maven-antrun-plugin
调用ant来执行一些jar文件。
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>generate-sources</phase> <!-- 绑定到生成代码阶段 -->
<configuration>
<target>
<property name="limax_location" value="${maven.dependency.com.ledo.kof.limax.jar.path}"/> <!-- 找到maven依赖的地址 -->
<delete dir="${gen}" />
<java jar="${limax_location}" fork="yes">
<arg value="xmlgen" />
<arg value="-java" />
<arg value="-script" />
<arg value="-isMaven" />
<arg value="-useJson" />
<arg value="-usePreLoad" />
<arg value="-isOpenCheckKey" />
<arg value="-sizepolicyxml" />
<arg value="./xmls/gameserver.xml" />
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
0x07 maven-surefire-plugin
默认mvn test
的时候执行的TestCase需要命名需要符合以下规则
- Test*
- *Test
- *TestCase
如果需要修改规则可以修改pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
</configuration>
</plugin>