How do I run a specific goal with a particular configuration in a Maven plugin when I have several configurations for that goal

87,293

Solution 1

I can do mvn myplugin:myGoal
Which runs myGoal (both executions I suppose)

None of them (assuming they had unique id). Executions are bound to a phase, you need to run the given phase to trigger them.

I know I can add an id to the execution element, but how do I refer to that id on the command line.

Not supported. What is possible for plugins invoked on the CLI is to define a non global configuration in the POM using the special default-cli executionId, like this:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>default-cli</id>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
          <descriptorRef>project</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>
  </executions>
</plugin>

Is this possible, or am I going about this the wrong way?

No, not possible. Either pass the parameters on the command line or use profiles (with or without the above default execution).

References

Solution 2

Execution of multiple goals from the CLI is now supported in Maven 3.3.1+

mvn exec:java@first-cli
mvn exec:java@second-cli

Where first-cli/second-cli are the execution ids.

https://blog.soebes.de/blog/2015/03/17/apache-maven-3-dot-3-1-features/

For your example the commands would be

mvn myplugin:mygoal@process-cats
mvn myplugin:mygoal@process-dogs

Solution 3

Hey you can create your goal like this:-

org.myplugin:myplugin-maven-plugin:1.1.1:myGoal  i.e
<groupId>:<artifactId>:<version>:<yourgoal>

It works in my case ...

Solution 4

The assumption you made that if you call

  mvn myplugin:myGoal

But the problem is that you will get an error message cause the execution have no unique id's (both in this case have the same).

You can reach what you like to do via profiles which you can activate via mvn -PXYZ and mvn -PABC

Share:
87,293

Related videos on Youtube

lukewm
Author by

lukewm

Technical Startup Guy, working on Juliet

Updated on May 05, 2021

Comments

  • lukewm
    lukewm almost 3 years

    See plugin config from pom.xml below.

    I can do:

    mvn myplugin:myGoal

    Which runs myGoal (both executions I suppose) but I want to be able to choose either the first or the second executions independently.

    I know I can add an id to the execution element, but how do I refer to that id on the command line. I'd like to get to something which does what this imagined command does:

    mvn myplugin:myGoal --executionId=1

    Is this possible, or am I going about this the wrong way?

            <plugin>
                <groupId>org.myplugin</groupId>
                <artifactId>myplugin-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <id>process-cats</id>
                        <goals>
                            <goal>myGoal</goal>
                        </goals>
                        <configuration>
                            <myParam>cats</myParam>
                        </configuration>
                    </execution>
                    <execution>
                        <id>process-dogs</id>
                        <goals>
                            <goal>myGoal</goal>
                        </goals>
                        <configuration>
                            <myParam>dogs</myParam>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
    • ᄂ ᄀ
      ᄂ ᄀ about 7 years
      Would you mind accepting stackoverflow.com/a/33627949/318054? The previously accepted answer is not valid anymore.
    • Raid
      Raid about 3 years
      Agreed, the accepted answer "No, not possible" is incorrect. You can mvn plugin:goal@an_id now.. So if you had an <id>an_id</id> it would work.
    • Michael Piefel
      Michael Piefel about 2 years
      Unfortunately, @lukewm has not been seen for quite some time.
  • Maksim Kostromin
    Maksim Kostromin over 6 years
    yes. and if you are in multi-module project, then do not forget to add -pl my-module
  • Martin P.
    Martin P. almost 2 years
    FYI: I tried to do mvn maven-compiler-plugin:compile@process-annotations but it yield "No plugin found for prefix 'maven-compiler-plugin'" but this works: mvn org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile‌​@process-annotations