7

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

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

源/主/资源
  - 资源1
  - 资源2
  - 资源2
  ENV1/
    -resource1(特定于环境)
  ENV2/
    -resource1(特定于环境)
    -resource3(特定于环境)

对于大多数环境,资源 zip 文件需要包含文件resource1resource2resource3. 对于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>

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

4

2 回答 2

14

假设以下结构:

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

于 2013-10-28T21:05:41.633 回答
3

很长一段时间以来一直在处理类似的问题,我摆脱了大量的 XML 代码,使用以下解决方法:

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

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

于 2013-10-28T20:23:16.647 回答