使用Maven构建非常相似的多个资源程序集 [英] Using maven to build multiple resources assemblies that are very similar

查看:63
本文介绍了使用Maven构建非常相似的多个资源程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种非常冗余的Maven配置有疑问.

I'm having a problem with this very redundant maven configuration.

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

The application I'm working on sometimes gets built for environments that need alternate resource bundles. The current way the resources are prepared for deployment is that they are added to a single .zip file. The build master then deploys the zipped resources to the server. For some environments, alternate versions of some of the resources need to be used. The folder structure in the project looks like this:


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

对于大多数环境,资源zip文件需要包含文件resource1resource2resource3.对于ENV1,zip文件需要包含ENV1/resource1resource2resource3.对于ENV3,zip文件需要ENV2/resource1resource2ENV2/resource3.

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.

我目前正在使用每种环境的程序集描述符进行此操作,并在我的POM文件中单独执行:

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>

每个描述符看起来都很相似. 常规"组装描述符:

And each descriptor looks very similar. The "normal" assembly desrciptor:

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

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

This solution works but it feels very inefficient. I don't like having multiple assemblies for each environment that are almost identical, except for the environment names. Is there any way to streamline this?

推荐答案

假定以下结构:

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

您可以在其中使用以下汇编描述符,如下所示:

where you can use the following assembly-descriptor like this:

<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 可通过

The iterator-maven-plugin is available via maven central. I think you can adapt the above configuration/structure to your problem.

这篇关于使用Maven构建非常相似的多个资源程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆