【问题标题】:How to provide different set of properties to the different executions of a plugin?如何为插件的不同执行提供不同的属性集?
【发布时间】:2019-02-27 21:39:09
【问题描述】:

我正在使用maven-assembly-plugin 来组装不同的工件,如下所示:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>configuration-staging</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
                <execution>
                    <id>configuration-production</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

assembly.xml,我启用了模板过滤:

<fileSets>
    <fileSet>
        <filtered>true</filtered>

这很好用。例如,如果我在要组装的资源之一中输入${name},则将其替换为项目名称。我还可以在pom.xml 中定义属性,将被插件替换。

现在,我想为maven-assembly-plugin 的每次执行设置不同的属性。例如,我想介绍一个${url},它包含要在目标环境中使用的URL(上例中的stagingproduction)。

这可能吗?怎么样?

【问题讨论】:

  • 您是否尝试将&lt;configuration&gt; 放入&lt;execution&gt;
  • 这与assembly.xml 无关。这是在不同的执行之间共享的;因此我决定在&lt;configuration> 之外定义它。这是关于为不同的执行设置不同的属性集。对于执行configuration-staging,我想解析与configuration-production不同的{$url}值。

标签: maven maven-3 maven-assembly-plugin


【解决方案1】:

显然,可以为maven-assembly-plugin 中的每次执行传递不同的属性,如下所示:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>configuration-staging</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>staging</finalName>
                        <filters>
                            <filter>src/main/assembly/staging.properties</filter>
                        </filters>
                    </configuration>
                </execution>
                <execution>
                    <id>configuration-production</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>production</finalName>
                        <filters>
                            <filter>src/main/assembly/production.properties</filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>

虽然这没有回答一般问题,但它专门回答了maven-assembly-plugin 的问题。

更多信息请访问https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html

【讨论】:

  • 如果变量没有被插值怎么办?在我的例子中, 文件中的每个属性都没有被 Interpolator 使用。
  • 它似乎只适用于 部分中声明的属性,因此适用于输出上下文。它对 assembly.xml 本身没有影响,因此如果您有一些属性,则不能在 assembly.xml 中使用
【解决方案2】:

您可以尝试使用属性 Maven 插件

https://www.mojohaus.org/properties-maven-plugin/index.html

允许您从文件或 URL 中读取属性。

【讨论】:

  • 如何将其链接到 maven-assembly-plugin 插件的不同执行?每个执行都应该有一组不同的属性。
  • 您可能必须将其包装到配置文件中并分别运行两次 Maven。 Maven 会在运行的早期解析属性,因此您可能无法在同一生命周期运行中更改它们。
  • 不同的配置文件是个好主意,我会试一试。希望有办法在没有不同配置文件的情况下做到这一点,因为不同配置文件的引入可能会给 Jenkins 带来问题。
猜你喜欢
  • 2018-08-08
  • 2016-03-15
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多