How to have exec-maven-plugin:exec run before 'site' phase? - maven

I have this script I need to run before site phase to fix an issue (http://jira.codehaus.org/browse/MSITE-640).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<inherited>false</inherited>
<executions>
<execution>
<id>workaround-MSITE-640</id>
<!--it should be 'site' but maven doesn't pick up there-->
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<failWithEmptyArgument>false</failWithEmptyArgument>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>./workaround-MSITE-640.sh</executable>
<arguments>
<argument>${settings.localRepository}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
But I can't seem to run it on that phase. I tried to put it under reporting -> plugins but it can't have configuration there.
It is idempotent, so no problem in running it more than once, but would be nice if I could tie it to the correct phase.
Thanks

Related

Why does exec-maven-plugin run all phases twice?

When I run a build with maven using the exec-maven-plugin, it runs everything twice for some reason. Is there a way to fix this so it only runs once? I've tried setting my phase in the pom.xml to compile and package and either way, it runs twice. My pom looks like
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.0</version>
<executions>
<execution>
<id>foo</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>bash</executable>
<commandlineArgs>myscript.sh</commandlineArgs>
</configuration>
</plugin>
</plugins>
</build>
It turned out that adding the phase tag caused the command to get executed twice. Leaving that out, it is now getting run once as expected. I guess it doesn't matter what phase I give it now, it'll always run the goal, which works for me.
If you need to run this early in the build, excluding the phase isn't an option.
You can do something like this instead in the plugin config:
<executions>
<execution>
<id>default</id>
<phase>none</phase> <!-- disable the default execution in validate phase -->
</execution>
<execution>
<id>exec-do-something</id>
<goals>
<goal>java</goal>
</goals>
<phase>generate-sources</phase><!-- now it will run once but in an earlier phase -->
</execution>
</executions>
I saw this happening due to the inclusion of:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
This seems to be that the maven-source-plugin causes a re-execution of the generate-sources phase. See https://maven.apache.org/plugins/maven-source-plugin/jar-mojo.html
Invokes the execution of the lifecycle phase generate-sources prior to executing itself.
If I removed this plugin, the exec goal only executed once.

Maven Config Plugins running twice

I have the following pom config. I added the cobertura plugin, and now pmd, cpd, findbugs, and test are running twice.
I understand that is because of my "phases" config, but I don't understand how can I achieve the following:
What I want is before I commit to the repo, build my app and check for pmd errors, findbugs errors, check my tests, and check my cobertura.
How can I achieve this? I am used to run "mvn clean package" before commit. It's that ok?
Here's my config:
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<linkXref>false</linkXref>
<rulesets>
<!-- Custom Ruleset -->
<ruleset>codequality/pmd.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>pmd</goal>
<goal>cpd</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<check>
<haltOnFailure>true</haltOnFailure>
<branchRate>70</branchRate>
<lineRate>70</lineRate>
<totalBranchRate>70</totalBranchRate>
<totalLineRate>70</totalLineRate>
<packageLineRate>70</packageLineRate>
<packageBranchRate>70</packageBranchRate>
</check>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any maven plugin has a default execution phase. In this case, the plugins that you apply are executed in the verify phase (pmd-plugin). But you are defining it to run also in the compile phase. Removes the phase tag and lets it run in the verification phase.
<execution>
<!--<phase>compile</phase>-->
<goals>
<goal>...</goal>
...
</goals>
</execution>
Finally, the good practice to validate your project before a commit is to run:
mvn clean verify

Execute multiple goals with parameters in maven

I am trying to execute multiple goals in maven
I have my Pom.xml like
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
<configuration>
<testFilesIncluded>
<jMeterTestFile>${mytest}</jMeterTestFile>
</testFilesIncluded>
<propertiesUser>
<hostName>${myhost}</hostName>
<port>${myport}</port>
<protocol>${myprotocol}</protocol>
</propertiesUser>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.codecentric</groupId>
<artifactId>jmeter-graph-maven-plugin</artifactId>
<version>0.1.0</version>
<executions>
<execution>
<id>create-graphs</id>
<goals>
<goal>create-graph</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>runcommand</id>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>**com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx**</argument>
<argument>de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph#create-graphs</argument>
</arguments>
</configuration>
</plugin>
</plugins>
I have two arguments mentioned in the org.codehaus plugin.
running the below command with argument1 disabled works fine.
mvn org.codehaus.mojo:exec-maven-plugin:1.4.0:exec#runcommand
But when I run the command with both are arguments enabled gives me an error
failed to execute goal com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx
running goal1 and goal2 indivudally with parameters from cmd line works fine.
mvn de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph#create-graphs
mvn com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests "-Dmyhost=hix.qa.com" "-Dmyport=80" "-Dmyprotocol=http" "-Dmythreads=5" "-Dmyloopcount=20" "-Dmyrampup=1" "-Dmytest=ScreenerAPI.jmx"
How to pass parameters to one goal from command line while running multiple goals?
You want to execute multiple commands, so you want to have multiple executions of the plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<!-- First execution executing jmeter-maven-plugin -->
<execution>
<id>runcommand1</id>
<goals>
<goal>exec</goal>
</goals>
<phase> <!-- you need to write a phase here --> </phase>
<configuration>
<arguments>
<argument>com.lazerycode.jmeter:jmeter-maven-plugin:1.4.1:jmeter#jmeter-tests -Dmyhost=hix.qa.com -Dmyport=80 -Dmyprotocol=http -Dmythreads=5 -Dmyloopcount=20 -Dmyrampup=1 -Dmytest=ScreenerAPI.jmx</argument>
</arguments>
</configuration>
</execution>
<!-- Second execution executing jmeter-graph-maven-plugin -->
<execution>
<id>runcommand2</id>
<goals>
<goal>exec</goal>
</goals>
<phase> <!-- you need to write a phase here --> </phase>
<configuration>
<arguments>
<argument>de.codecentric:jmeter-graph-maven-plugin:0.1.0:create-graph#create-graphs</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
</configuration>
</plugin>
This configuration does multiple things:
It configures 2 executions for the 2 commands you want to invoke. They are both configured with the correct <argument> attribute.
It factors out the common configuration of those two inside the global configuration element. In this case, this is simply the executable.
There's an important factor though: those executions need to be bound to a phase if you want to execute both of them in a single build. As as example, if you bind them to verify then invoking mvn verify will invoke the two executions.

how to execute multiple command prompt commands using maven in single pom.xml

I want to run multiple command prompt commands in maven using single pom.xml. How can I do that?
For ex: I have 2 commands to execute. I am executing the first command by using exec-maven-plugin.
Below is the portion of my pom.xml to execute the first command:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>load files</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>windchill</executable>
<arguments>
<argument>wt.load.LoadFileSet</argument>
<argument>-file</argument>
<argument>${basedir}/fileSet.xml</argument>
<argument>-UNATTENDED</argument>
<argument>-NOSERVERSTOP</argument>
<argument>-u</argument>
<argument>wcadmin</argument>
<argument>-p</argument>
<argument>wcadmin</argument>
</arguments>
</configuration>
</plugin>
For this the build is success.
Is it possible to execute one more command just like above in the same pom.xml? I was not able to do that. So someone please help how to add it in pom.xml
The answer can be found in the FAQ.
The full answer is here: http://article.gmane.org/gmane.comp.java.maven-plugins.mojo.user/1307
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>id1</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd1</executable>
</configuration>
</execution>
<execution>
<id>id2</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd2</executable>
</configuration>
</execution>
</executions>
</plugin>
Then you specify execution ids as:
mvn exec:exec#id2
But this syntax is possible starting from maven 3.3.1

Maven - determine order of different plugin goals in the same phase

The following snippet is an excerpt of the configuration of the maven-cargo plugin, but the question is independent from that specific plugin.
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
<goal>start</goal>
</goals>
</execution>
</executions>
This configuration (lets simply call it plugin A) will wait till pre-integration-test phase, then fire its goals deploy and start (in that order).
Say I have another plugin B which is relevant in the same phase. What are my options to
execute plugin B's goals before (after) A? (someStuff - > deploy -> start)
execute plugin B's goals in-between plugin A's goals (deploy -> someStuff -> start)
I figure that the answer to (1) is here, linking the order of the goals to the order of the plugin definition in the POM. But I have no idea about (2).
You are right about (1). If two plugins are to be executed on the same phase, then they will be executed in the order they are declared in pom.xml.
I'm not 100% sure about the (2), but I think it's impossible without some hacks, like using exec-maven-plugin, for example:
<!-- deploy -->
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- do something -->
<plugin>
<groupId>some_other_plugin</groupId>
<artifactId>some_other_plugin</artifactId>
<executions>
<execution>
<id>someStuff</id>
<phase>pre-integration-test</phase>
<goals>
<goal>some_goal</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- start -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>mvn</executable>
<commandlineArgs>org.codehaus.cargo:cargo-maven2-plugin:start -Dparam=value</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>

Resources