How to run two Maven Mojos with different parameters - maven

I wrote a Maven plugin with two Mojos. They are meant to be run in the command line, not attached to any phase of the build.
They happen to have different parameters, and I don't know how to specify them. So far I've managed to use two executions one for each one, but it looks clumsy in the command line.
This is the plugin declaration in my pom.xml:
<plugin>
<groupId>myplugin.tools.camera</groupId>
<artifactId>camera-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>exe1</id>
<configuration>
<snapshotfile>file1.shot</snapshotfile>
<dir>dev</dir>
</configuration>
<goals>
<goal>take-shot</goal>
</goals>
</execution>
<execution>
<id>exe2</id>
<configuration>
<time>12:34.56</time>
<dir>test</dir> <!-- same parameter, with different value -->
</configuration>
<goals>
<goal>show-shot</goal>
</goals>
</execution>
</executions>
</plugin>
It works, but to run each Mojo I need to run:
$ mvn camera:take-shot#exe1
$ mvn camera:show-shot#exe2
Would it be possible to get rid of those #exe1 and #exe2?
Now, there could be an advantage to these executions, if I wanted to create multiple different parameters to later pick which one to use. I hoped there could be a default execution, but don't know how to do that.

Related

How can I manually run an exec plugin config bound to `pre-integration-test`?

We have a pre-integration-test config setup like follows. I would like to know how to run just this phase/step without all the preceding steps
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>Create test databases</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<skip>${skipITs}</skip>
<classpathScope>test</classpathScope>
<mainClass>TestDatabaseGenerator</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<arguments>
<argument>${db_admin_user}</argument>
<argument>${db_admin_password}</argument>
<argument>${itest.forkCount}</argument>
<argument>${itest.dbTemplate}</argument>
<argument>${db_user}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
The reason is simply that, while this works when doing mvn verify, it takes too long when all I want is to get the side effect of generating new test databases, which is what is being setup in the above. I could manually run the class, of course, but it is tedious to have to fill in all the arguments, so if there was a way of reusing the work that would be great.
After some searching I did find MNG-5768, but I have not managed to figure out the right incantations to make it work. I have so far tried:
mvn exec:execute#"Create test databases"
mvn exec:exec#'Create test databases'
mvn exec:java#'Create test databases'
But nothing works, obviously because I have no idea what I am doing and is just modifying other examples in the hopes of making it work :) Also, having spaces in the id obviously needs to be handled in some other way than what I am doing, as Maven will have none of it.

How to invoke specific execution

I am trying to replace maven exec with MavenInvokerPlugin because of problems on Jenkins with forwarding the maven settings file.
So in bash it looks straight:
mvn dependency:copy-dependencies#resolve-maven-deps
My translation to MavenInvokerPlugin configuration is
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<projectsDirectory>${project.basedir}/src/main/docker</projectsDirectory>
<localRepositoryPath>${project.build.mavenDependencies}</localRepositoryPath>
<goal>dependency:copy-dependencies#resolve-maven-deps</goal>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>run</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
It looks like that execution id is completely ignored, because I tried random strings and mvn builds the project with success.
mvn dependency:copy-dependencies#asdfasdfa
So I'd like to know whether this feature is supported at all and what I am doing wrong.
P.S. I know that calling maven out of maven is anti pattern, but here is exactly that rare case when there is no other way.
After looking at projects using maven invoker I figured out the trick.
goal tag is not used, instead provide invokerPropertiesFile:
<pom>${project.basedir}/xxx/pom.xml</pom>
<invokerPropertiesFile>${project.basedir}/invoker.properties</invokerPropertiesFile>
content of the file:
invoker.goals=compile -P resolve-maven-deps

Maven maven-dependency-plugin copy-dependencies ignores outputDirectory

I'm try to use the maven-dependency-plugin's copy-dependencies goal.
I checked its official example with the snippet below.
My problem is: the dependencies are always copied to
target\dependency folder, even if I specify an <outputDirectory> node.
Here is the part of my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
<configuration>
<outputDirectory>${project.build.directory}/aaa</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</executions>
</plugin>
</plugins>
</build>
Question: What I'm doing wrong? Is it possible to declare the output directory outside of the project? For example: c:\temp ?
You configured an execution of the maven-dependency-plugin with a configuration only defined within its scope, hence it will only be picked up by the plugin during a mvn package invocation, that is, while performing the package phase and the plugin (executions) having a binding to it.
If you invoke the plugin from command line as following:
mvn dependency:copy-dependencies
It will indeed only use default values, since your configuration will be ignored.
In fact, the default value for the outputDirectory option is indeed:
Default: ${project.build.directory}/dependency
In maven, a plugin configuration can be defined as general configuration (outside of an execution section, applied to all executions and to command line invocations) or per execution (within an execution section, like in your case).
In your case, you probably want the configuration to be valid in both cases, so simply change your plugin section to the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<configuration>
<outputDirectory>${project.build.directory}/aaa</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: we moved up the configuration, from execution scope to plugin (global) scope.
Also note, in the configuration above we kept the execution, which means maven will always execute this plugin goal at each an every mvn package invocation. If you don't want this behavior and only expect to use the command line execution, then you can remove the executions section at all.
Since Maven 3.3.1 it's also possible (see the note at the very end of Using the executions Tag section):
Since Maven 3.3.1 this is not the case anymore as you can specify on the command line the execution id for direct plugin goal invocation.
to execute copy-dependencies execution directly not modifying your pom at all:
mvn dependency:copy-dependencies#copy-dependencies
Note that of the two copy-dependencies separated by #, the former refers to the plugin goal and the latter refers to the execution id. And general direct invocation of an execution is:
mvn <plugin-prefix>:<goal>#<execution>
See also accepted answer to almost the same question

Execute maven plugin goal on specific modules

I have the following situation:
masterpom.xml:
...
<modules>
<module>sample-module</module>
</modules>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
<inherited>true</inherited>
<executions>
<execution>
<id>update-parent</id>
<phase>pre-clean</phase>
<goals>
<goal>update-parent</goal>
</goals>
<configuration>
<parentVersion>4.4.2.1</parentVersion>
</configuration>
</execution>
</executions>
</plugin>
...
I would like to execute the versions-maven-plugin's update-parent goal on every module listed between <modules> and </modules>.
How can I do that? This configuration that I already tried doesn't work, because the modules do not inherit from masterpom.xml (they have a different parent which shouldn't be modified).
I also tried running the plugin from the command line:
mvn versions:update-parent "-DparentVersion=[4.4.2.1]"
but the changes will not be limited to the modules I want.
EDIT: running the plugin from the command line at the appropriate location seems to work. I still don't know how to produce the same effect by specifying options inside the POM.

Can't execute a maven shell script with the prefix I gave it

I'm trying to make a post deploy script for my project, which will test validity.
Now, the script is made, and I'm trying to make it run after all the sub modules.
The simplest way I could think of was simply to call the exec-maven-plugin with a specific prefix. I need a new prefix though, since I already have a different exec running on one of the sub modules.
so, I want to call it as: validate:exec
the relevant part of the xml looks like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>validate.py</executable>
<goalPrefix>validate</goalPrefix>
<arguments>
<argument>${deploy.host.fe</argument>
<argument>${deploy.port.fe}/argument>
</arguments>
</configuration>
</plugin>
This I put in my pluginsmanagement or plugins tag (spelled correctly), neither helped.
when I try to run:
mvn validate:exec
I get:
No plugin found for prefix 'validate' in the current project and in the plugin groups...
Thanks in advance for any help

Resources