如何使用不同的属性执行两次maven插件

时间:2022-09-03 22:53:02

I would like to build from a maven pom running two sequential executions of the same plugin, in the same phase differing only by a single property, which will result in two different archives being created. Since the configuration is rather complicated, I'd rather NOT copy it just to change one value, which would create a maintenance nightmare. If it was somehow possible to define such a property in the <executions> section of the plugin config, I could avoid this headache.

我想从一个maven pom构建,运行同一个插件的两个连续执行,在同一阶段只有一个属性不同,这将导致创建两个不同的存档。由于配置相当复杂,我宁愿不复制它只是为了改变一个值,这会造成维护噩梦。如果以某种方式可以在插件配置的 部分中定义这样的属性,我可以避免这种麻烦。

Question: Is this possible and if so how?

问题:这可能吗?如果可以,怎么样?

Update: Two answers have mentioned using multiple executions and one of them mentions that you can have separate configurations in each execution. But given that the majority of my configuration is constant between the two executions, can I have one configuration on the plugin level and also have configuration sections in each execution for the parts that are different?

更新:两个答案提到使用多个执行,其中一个提到您可以在每次执行中使用单独的配置。但鉴于我的大部分配置在两次执行之间是恒定的,我可以在插件级别上有一个配置,并且每个执行中的配置部分对于不同的部分也是如此吗?

3 个解决方案

#1


12  

Given the simple Maven Source Plugin configuration (as an example) you have a shared configuration across all of its executions (outside the executions element) and then a custom configuration per each execution, for the same phase, as requested by your question:

给定简单的Maven Source Plugin配置(作为示例),您可以在其所有执行(执行元素之外)中拥有共享配置,然后根据您的问题的请求,针对同一阶段执行每次执行的自定义配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <includePom>true</includePom>
            </configuration>
            <executions>
                <execution>
                    <id>test-id1</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <finalName>aaa</finalName>
                    </configuration>
                </execution>
                <execution>
                    <id>test-id2</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <finalName>bbb</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The configuration entry <includePom>true</includePom> will in this case be merged with the custom configurations of each execution and as such centralize the common configuration as plugin generic configuration.

在这种情况下,配置条目 true 将与每个执行的自定义配置合并,因此将公共配置集中为插件通用配置。

For more details on the different level of configurations, you can check official Maven documentation, here, in particular the example "Configuring compile to run twice". Further details are also available on the official POM documentation, here, Plugins section.

有关不同配置级别的更多详细信息,可以在此处查看官方Maven文档,特别是“配置编译以运行两次”示例。有关详细信息,请参阅官方POM文档,此处为插件部分。

#2


0  

You create two <execution> elements within the <plugin> declaration. Each <execution> element can have it's own <configuration> section.

您在 声明中创建了两个 元素。每个 元素都可以拥有自己的 部分。

#3


0  

You need to create a different execution (still bound to the same phase)

您需要创建不同的执行(仍然绑定到同一阶段)

To avoid duplication of the config, you can put the <configuration> outside the <execution> element and then in the 2 executions, you only define the property that is different.

为避免重复配置,可以将 放在 元素之外,然后在2次执行中,只定义不同的属性。

Taken from the maven docs:

摘自maven文档:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-myquery-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <id>execution1</id>
            <phase>test</phase>
            <configuration>
              <url>http://www.foo.com/query</url>
              <timeout>10</timeout>
              <options>
                <option>one</option>
                <option>two</option>
                <option>three</option>
              </options>
            </configuration>
            <goals>
              <goal>query</goal>
            </goals>
          </execution>
          <execution>
            <id>execution2</id>
            <configuration>
              <url>http://www.bar.com/query</url>
              <timeout>15</timeout>
              <options>
                <option>four</option>
                <option>five</option>
                <option>six</option>
              </options>
            </configuration>
            <goals>
              <goal>query</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

#1


12  

Given the simple Maven Source Plugin configuration (as an example) you have a shared configuration across all of its executions (outside the executions element) and then a custom configuration per each execution, for the same phase, as requested by your question:

给定简单的Maven Source Plugin配置(作为示例),您可以在其所有执行(执行元素之外)中拥有共享配置,然后根据您的问题的请求,针对同一阶段执行每次执行的自定义配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <includePom>true</includePom>
            </configuration>
            <executions>
                <execution>
                    <id>test-id1</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <finalName>aaa</finalName>
                    </configuration>
                </execution>
                <execution>
                    <id>test-id2</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <finalName>bbb</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The configuration entry <includePom>true</includePom> will in this case be merged with the custom configurations of each execution and as such centralize the common configuration as plugin generic configuration.

在这种情况下,配置条目 true 将与每个执行的自定义配置合并,因此将公共配置集中为插件通用配置。

For more details on the different level of configurations, you can check official Maven documentation, here, in particular the example "Configuring compile to run twice". Further details are also available on the official POM documentation, here, Plugins section.

有关不同配置级别的更多详细信息,可以在此处查看官方Maven文档,特别是“配置编译以运行两次”示例。有关详细信息,请参阅官方POM文档,此处为插件部分。

#2


0  

You create two <execution> elements within the <plugin> declaration. Each <execution> element can have it's own <configuration> section.

您在 声明中创建了两个 元素。每个 元素都可以拥有自己的 部分。

#3


0  

You need to create a different execution (still bound to the same phase)

您需要创建不同的执行(仍然绑定到同一阶段)

To avoid duplication of the config, you can put the <configuration> outside the <execution> element and then in the 2 executions, you only define the property that is different.

为避免重复配置,可以将 放在 元素之外,然后在2次执行中,只定义不同的属性。

Taken from the maven docs:

摘自maven文档:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-myquery-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <id>execution1</id>
            <phase>test</phase>
            <configuration>
              <url>http://www.foo.com/query</url>
              <timeout>10</timeout>
              <options>
                <option>one</option>
                <option>two</option>
                <option>three</option>
              </options>
            </configuration>
            <goals>
              <goal>query</goal>
            </goals>
          </execution>
          <execution>
            <id>execution2</id>
            <configuration>
              <url>http://www.bar.com/query</url>
              <timeout>15</timeout>
              <options>
                <option>four</option>
                <option>five</option>
                <option>six</option>
              </options>
            </configuration>
            <goals>
              <goal>query</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>