我遇到了这个非常冗余的 Maven 配置的问题。
我正在开发的应用程序有时是为需要备用资源包的环境构建的。当前为部署准备资源的方式是将它们添加到单个 .zip 文件中。然后构建主机将压缩资源部署到服务器。对于某些环境,需要使用某些资源的替代版本。项目中的文件夹结构如下所示:
源/主/资源 - 资源1 - 资源2 - 资源2 ENV1/ -resource1(特定于环境) ENV2/ -resource1(特定于环境) -resource3(特定于环境)
对于大多数环境,资源 zip 文件需要包含文件resource1
、resource2
、resource3
. 对于ENV1
, zip 文件需要包含ENV1/resource1
, resource2
, resource3
. 对于ENV3
, zip 文件需要ENV2/resource1
, resource2
, ENV2/resource3
.
我目前正在为每个环境使用一个程序集描述符,并在我的 POM 文件中单独执行:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>zip-normal-resources</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>assembly-descriptor.xml</descriptor>
</configuration>
</execution>
<execution>
<id>zip-ENV1-resources</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptor>assembly-descriptor-ENV1.xml</descriptor>
</configuration>
</execution>
每个描述符看起来都非常相似。“正常”程序集描述符:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd ">
<id>Resources</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}/resources</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
特定于环境的描述符:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd ">
<id>Resources-ENV1</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
<!-- exclude environment-specific files -->
<excludes>
<exclude>resource1</exclude>
</excludes>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</fileSet>
<fileSet>
<directory>src/main/resources/ENV1</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</fileSet>
<fileSet>
<directory>${project.build.outputDirectory}/resources</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
该解决方案有效,但感觉非常低效。我不喜欢每个环境都有多个几乎相同的程序集,除了环境名称。有没有办法简化这个?