打包是开发、测试及发版不可避免的步骤,这里对assembly使用过程中遇到的问题做下记录。
1.pom引入plugin依赖
profile中properties的属性声明将其与assembly结合起来,不同环境使用不同配置文件
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env.devMode>dev</env.devMode>
<skipAssemblyDEV>false</skipAssemblyDEV>
<skipAssemblyUAT>true</skipAssemblyUAT>
<skipAssemblyPROD>true</skipAssemblyPROD>
</properties>
</profile>
<profile>
<id>uat</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<env.devMode>uat</env.devMode>
<skipAssemblyDEV>true</skipAssemblyDEV>
<skipAssemblyUAT>false</skipAssemblyUAT>
<skipAssemblyPROD>true</skipAssemblyPROD>
</properties>
</profile>
</profiles>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>make-assembly-dev</id>
<phase>package</phase>
<goals>
<goal>single</goal> </goals>
<configuration>
<skipAssembly>${skipAssemblyDEV}</skipAssembly>
<descriptors>
<descriptor>src/main/assembly/dev/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>make-assembly-uat</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<skipAssembly>${skipAssemblyUAT}</skipAssembly>
<descriptors>
<descriptor>src/main/assembly/uat/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>make-assembly-prod</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<skipAssembly>${skipAssemblyPROD}</skipAssembly>
<descriptors>
<descriptor>src/main/assembly/prod/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
2.目录项目路劲配置变化
2.5.4以上版本使用时,fileSet标签下的outputDirectory不能再使用/作为开始的字符表示输出的相对目录了,可以使用点表示当前目录
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
3.覆盖特点
使用Assembly时,fileSet顺序决定最终打包进去的文件,前面的fileSet如果已经放置了同名文件,后面的fileSet不会覆盖。
下面的配置示例中,最终打包中的a.txt包含的是prod内容。
<fileSet>
<directory>src/main/resources/prod</directory> <!-- 此目录文件a.txt中记录为prod内容 -->
<outputDirectory>.</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources</directory><!-- 此目录文件a.txt中记录为默认测试内容 -->
<outputDirectory>.</outputDirectory>
<excludes>
<exclude>prod/**/*<exclude>
</excludes>
</fileSet>