31

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.

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?

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Steve Cohen
  • 4,679
  • 9
  • 51
  • 89
  • Possible duplicate of [How to invoke the same maven build twice in one call](http://stackoverflow.com/questions/7239786/how-to-invoke-the-same-maven-build-twice-in-one-call) – approxiblue Dec 09 '15 at 22:10
  • 1
    Not a duplicate of that question I would say – A_Di-Matteo Dec 09 '15 at 22:10
  • @Steve check my answer, yes, it gets exactly to your point (clarified in your update) – A_Di-Matteo Dec 09 '15 at 22:12
  • @A_Di-Matteo Ive tried to use your solution for my own problem but seems to make no diffrenence, could you help please http://stackoverflow.com/questions/42299040/how-do-i-make-this-maven-pom-have-two-profiles-only-difference-being-in-mainclas – Paul Taylor Feb 20 '17 at 09:45
  • try gradle. These kinds of issues drove me away from using maven to build rpms :-) – Steve Cohen Feb 25 '17 at 16:52

4 Answers4

32

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:

<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.

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.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • If I understand you correctly, you have provided the answer to my update question. You seem to be saying that it is possible to define one configuration for the whole plugin (containing the constant items) and separate configurations in the executions (containing the per-execution differences). Is that what you are saying? – Steve Cohen Dec 09 '15 at 22:13
  • Exactly, I just tested the snippet I posted before adding it to my answer and it works perfectly. I will link soon the official documentation (which I read a while ago exactly about this point). – A_Di-Matteo Dec 09 '15 at 22:15
  • concerned Official documentation link added. – A_Di-Matteo Dec 09 '15 at 22:22
  • I'd still like to know the significance in the "options" in the official documentation. They seem useless to me. – Steve Cohen Dec 09 '15 at 22:23
  • And it's still not as nice as being able to define different properties between the two executions. – Steve Cohen Dec 09 '15 at 22:27
  • Further official documentation about plugin configuration here https://maven.apache.org/pom.html#Plugins speaking in that case about merging between parent and child pom, but it would be the same about generic plugin configuration and execution configuration – A_Di-Matteo Dec 09 '15 at 22:31
5

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.

Taken from the maven docs:

<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>
Augusto
  • 28,839
  • 5
  • 58
  • 88
  • 2
    Thanks. Yes, I saw this documentation, but I don't understand from it what the significance of the "options" is. How are they used? The example doesn't show that. A property has a name and a value, but an option doesn't seem to. – Steve Cohen Dec 09 '15 at 22:03
  • Hi Steve, I think I don't fully understand your question then. Could you clarify what bits are not clear? ... and have your tried reading the whole page that is linked? As it does explain how plugins are configured and how the configuration is inherited. – Augusto Dec 09 '15 at 22:06
  • going to clarify question. – Steve Cohen Dec 09 '15 at 22:08
2

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

tdrury
  • 2,307
  • 1
  • 22
  • 25
0

I wanted to create a jar and a put in in a zip file with other config files This worked for me

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
     <execution>
     <id>build-jar-with_dep1</id>
      <phase>package</phase>
      <configuration>
       <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
       </descriptorRefs>
       <finalName>${buildversion}</finalName>
       <finalName>finalname</finalName>
       <appendAssemblyId>false</appendAssemblyId>
      </configuration>
      <goals>
                         <goal>assembly</goal>
                     </goals>
     </execution>
     <execution>
     <id>build_zip1</id>
      <phase>package</phase>
      <configuration>
       <descriptor>src/assembly/bin.xml</descriptor>
       <finalName>${buildversion}</finalName>
       <finalName>finalname</finalName>
       <appendAssemblyId>false</appendAssemblyId>
      </configuration>
      <goals>
                         <goal>single</goal>
                     </goals>
     </execution>
   
    </executions>
   </plugin>