如何拥有 Maven Assembly 插件的自定义属性?

How to have custom properties for the Maven Assembly Plugin?

我有一个使用 Maven 编译的 Java 项目,最后使用 maven-assembly-plugin 将编译后的 JAR 文件、DLL 等打包到 10 个不同的 ZIP 文件中。每个 ZIP 文件都适用于不同的环境(具有不同的 DLL),但它们的内容通常是相同的。

现在我使用 10 个不同的 assembly.xml 文件来创建这 10 个 ZIP 文件。

问题是这些 XML 几乎相同,唯一的区别是 DLL 路径中的 1 个单词。此类文件的示例:(实际上它要长得多)

<assembly>
  <id>{someVariable}</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <!-- Copying DLLs: -->
    <fileSet>
        <directory>target/dll/{someVariable}</directory>
        <outputDirectory>dll</outputDirectory>
        <includes>
            <include>*.dll</include>
        </includes>
    </fileSet>
  </fileSets>
</assembly>

如您所见,我想在更多地方使用 {someVariable},这是我想要的功能,但我无法让它发挥作用。希望这是可能的,这是我问题的核心。我想使用相同的 assembly.xml 文件并始终使用不同的 {someVariable} 值执行 10 次,如下所示:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
    <execution>
        <id>make-the-zip</id>
        <goals>
            <goal>single</goal>
        </goals>
        <phase>package</phase>
        <configuration>
            <descriptors>
                <descriptor>src/assembly/myCommonAssembly.xml</descriptor>
            </descriptors>
            <properties>
                <someVariable>my-value</someVariable>
            </properties>
        </configuration>
    </execution>
  </executions>
</plugin>

可能吗?仅供参考:第 <properties> 部分不起作用,我只是想展示我想做什么。

我知道我可以在 poml.xml 中创建属性并在 assembly.xml 中使用它们,但这并不能解决我的问题,因为我仍然需要创建 10 个不同的 assembly.xml文件。

This 是我找到的最好的建议,但这不是答案。

您可以使用 iterator-maven-plugin, in order to iterate over all your different property values. This Mojo has an iterator 目标来迭代一组给定的属性,并将它们添加为 Maven 属性:

The iterator-maven-plugin will inject the current value as a property which means you can use this property to parameterize your build.

在你的情况下,你可以:

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <version>0.4</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>iterator</goal>
      </goals>
      <configuration>
        <items>
          <item>my-value-1</item>
          <item>my-value-2</item>
          <item>my-value-3</item>
        </items>
        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-assembly-plugin</artifactId>
              <version>2.6</version>
            </plugin>
            <goal>single</goal>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/assembly/myCommonAssembly.xml</descriptor>
              </descriptors>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

package阶段,此配置将迭代3个给定值,my-value-1my-value-3,并在每次Maven Assembly Plugin时执行。对于给定的执行,可以使用 ${item} 检索当前迭代值。因此,您的通用程序集描述符将变为:

<assembly>
  <id>${item}</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <!-- Copying DLLs: -->
    <fileSet>
        <directory>target/dll/${item}</directory>
        <outputDirectory>dll</outputDirectory>
        <includes>
            <include>*.dll</include>
        </includes>
    </fileSet>
  </fileSets>
</assembly>