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

时间:2013-10-28 14:43:36

标签: maven maven-assembly-plugin

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

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

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

我目前正在使用每个环境的程序集描述符,并在我的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>

此解决方案有效,但感觉非常低效。除了环境名称之外,我不喜欢为每个环境提供几乎相同的多个程序集。有没有办法简化这个?

2 个答案:

答案 0 :(得分:10)

假设以下结构:

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获取。我认为您可以根据您的问题调整上述配置/结构。

答案 1 :(得分:2)

长时间搞乱了类似问题,我使用以下解决方法摆脱了大量的XML代码:

  1. 为所有环境创建相同的zip存档;应根据环境(服务器的主机名,环境变量等)提取或访问资源

  2. 创建一个JAVA / Groovy类,它将构建所有必需的特定于环境的程序集,并通过执行maven-exec-plugin来运行它。