【问题标题】:How to have custom properties for the Maven Assembly Plugin?如何为 Maven 程序集插件提供自定义属性?
【发布时间】:2017-02-05 22:45:21
【问题描述】:

我有一个使用 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>

有可能吗?仅供参考:&lt;properties&gt; 部分不起作用,我只是想展示我想做的事情。

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

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

【问题讨论】:

标签: maven variables properties maven-assembly-plugin


【解决方案1】:

您可以使用iterator-maven-plugin 来迭代所有不同的属性值。这个 Mojo 有一个 iterator 目标,它可以迭代一组给定的属性,并将它们添加为 Maven 属性:

iterator-maven-plugin 会将当前值作为属性注入,这意味着您可以使用该属性来参数化您的构建。

在您的情况下,您可以:

<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 组装插件时执行。对于给定的执行,可以使用${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>

【讨论】:

  • 谢谢,这看起来很有希望。我只有一些 API 错误,我希望它不是由旧 Java 1.3 引起的:无法执行目标 com.soebes.maven.plugins:iterator-maven-plugin:0.4:iterator (default): Execution default of goal com.soebes .maven.plugins:iterator-maven-plugin:0.4:iterator failed: 由于 API 不兼容,无法在插件 'com.soebes.maven.plugins:iterator-maven-plugin:0.4' 中加载 mojo 'iterator': org.codehaus.plexus.component.repository.exception.ComponentLookupException:com/soebes/maven/plugins/iterator/IteratorMojo:不支持的major.minor 51.0版
  • @Racky 哎哟。是的,Java 1.3 已经非常老了。使用 Java 7 或 8 运行它没有这个问题。您使用的是哪个版本的 Maven?该插件至少需要 Maven 3,Maven 3.3 至少需要 1.7。
  • 所以现在它似乎工作了。我将 iterator-maven-plugin 的版本更改为 0.3。所以谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
相关资源
最近更新 更多