gpt4 book ai didi

maven - 使用 maven 构建多个非常相似的资源程序集

转载 作者:行者123 更新时间:2023-12-04 10:18:30 25 4
gpt4 key购买 nike

我遇到了这个非常冗余的 maven 配置的问题。

我正在开发的应用程序有时是为需要备用资源包的环境而构建的。为部署准备资源的当前方式是将它们添加到单个 .zip 文件中。构建主然后将压缩的资源部署到服务器。对于某些环境,需要使用某些资源的替代版本。项目中的文件夹结构如下所示:

src/main/resources  - resource1  - resource2  - resource2  ENV1/    -resource1 (environment-specific)  ENV2/    -resource1 (environment-specific)    -resource3 (environment-specific)

For most environments, the resource zip file needs to contain the files resource1, resource2, resource3. For ENV1, the zip file needs to contain ENV1/resource1, resource2, resource3. For ENV3, the zip file needs ENV2/resource1, resource2, ENV2/resource3.

I am currently doing this with an assembly descriptor for each environment, and a separate execution in my POM file:

        <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>

此解决方案有效,但感觉效率很低。除了环境名称之外,我不喜欢每个环境都有多个几乎相同的程序集。有什么办法可以简化这个吗?

最佳答案

假设以下结构:

src/
└── main
├── assembly
│   └── iterator.xml
└── environment
├── dev
│   └── server.properties
├── pretest
│   └── server.properties
├── production
│   └── server.properties
├── qa
│   └── server.properties
└── test
└── server.properties

您可以在其中使用以下程序集描述符:

<assembly>
<id>${item}</id>
<formats>
<format>war</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>WEB-INF</outputDirectory>
<directory>${basedir}/src/main/environment/${item}/</directory>
<includes>
<include>**</include>
</includes>
</fileSet>
<fileSet>
<outputDirectory>WEB-INF</outputDirectory>
<directory>${project.build.directory}/environment/${item}</directory>
<includes>
<include>**</include>
</includes>
</fileSet>
</fileSets>
</assembly>

使用 iterator-maven-plugin将解决问题:

  <build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>executor</goal>
</goals>
<configuration>
<items>
<item>dev</item>
<item>test</item>
<item>qa</item>
<item>production</item>
<item>pretest</item>
</items>
<pluginExecutors>
<pluginExecutor>
<goal>single</goal>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/iterator.xml</descriptor>
</descriptors>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

iterator-maven-plugin可通过 maven central 获得.我认为您可以根据您的问题调整上述配置/结构。

关于maven - 使用 maven 构建多个非常相似的资源程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19637787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com