首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何拥有Maven程序集插件的自定义属性?

如何拥有Maven程序集插件的自定义属性?
EN

Stack Overflow用户
提问于 2016-09-28 06:11:10
回答 1查看 5.6K关注 0票数 4

我有一个使用Maven编译的Java项目,最后使用maven程序集插件将编译好的JAR文件、DLL等打包到10个不同的ZIP文件中。每个ZIP文件都针对不同的环境(有不同的DLL),但它们的内容通常是相同的。

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

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

代码语言:javascript
运行
复制
<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}值执行10x,如下所示:

代码语言:javascript
运行
复制
<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文件。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-28 07:56:26

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

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

就你而言,你可以:

代码语言:javascript
运行
复制
<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}检索当前迭代的值。因此,您的公共程序集描述符将变成:

代码语言:javascript
运行
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39739277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档