Home > Enterprise >  How to run multiple plugin executions as a single argument to Maven?
How to run multiple plugin executions as a single argument to Maven?

Time:01-13

Is it possible to run all <execution> definitions on one maven command?

Like:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>first-execution</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.FooServer</mainClass>
                    </configuration>
                </execution>
                <execution>
                    <id>second-execution</id>
                    <goals>
                        <goal>java</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mycompany.BarServer</mainClass>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
 </build>

I could run mvn exec:java@first-execution exec:java@second-excution to run both, but is there also a maven command to simple tell maven to run just any <execution> that is found in the definitions?

Or at least, all executions that matches a <goal>?

CodePudding user response:

Not possible and not planned as a feature. As per Robert Scholte, Maven project lead:

Maven is lifecycle driven. Being able to execute a goal from commandline is a feature, but I don't see the need to execute all available exeucutionIds.

CodePudding user response:

Don't try to use maven as universal scripting tool - it is written with the motto "convention over configuration".

If your build requires execution of external executables or Java code, you can bind executions to a maven phase like this:

<execution>
  <id>first-execution</id>
  <goals>
    <goal>java</goal>
  </goals>
  <phase>compile</phase>
  <configuration>
    <mainClass>com.mycompany.FooServer</mainClass>
  </configuration>
</execution>

If you call it with mvn compile then all executions bound to this phase (and all other phases up to this) will be executed. Usually in the order of their IDs, so you might want to set the id to something like 01-first, 02-second, 03-aaaa-third.

For further information about configuration maven plugins, see the official documentation

For a reference which phases exist and in which order they are executed (and in which lifecycle), there is also a nice offical documentation: Lifecycle Reference

Please note that some plugins are configured by default and also have bound several executions to phases by default (this is why e.g. a mvn compile compiles Java code without any configuration of the compiler-plugin).

Another alternative would be to switch to another build tool with a different philosophy, like Apache Ant - this doesn't have dependency management included so nicely, but could be even called from maven. Or the Gradle Build Tool that uses a DSL built on Groovy or Kotlin as configuration, so you can have a very flexible configuration (but don't do that there or you will not understand your own build configuration a few months after you have written it ;)

  • Related