Changing the order of maven plugins execution - build

I am new to maven, I would like to change the order of the maven plugins execution.
In my pom.xml, I have maven-assembly-plugin and maven-ant-plugin.
maven-assembly-plugin for creating a zip file.
maven-ant-plugin for copying the zip file from target to some other directory.
When I run pom.xml, maven-ant-plugin got triggered, and looking for zip file finally I got the error saying zip file not found.
Please suggest to me the way how to run maven-assembly-plugin first before maven-ant-plugin so that it will find and copy the zip file to the corresponding directory.

Since you say you are very new to Maven....Maven builds are executions of an ordered series of phases. These phases are determined by the lifecycle that is appropriate to your project based on its packaging.
Therefore, you control when a plugin's goal is executed by binding it to a particular phase.
Hope that helps.
EDIT: Also, since Maven 3.0.3, for two plugins bound to the same phase, the order of execution is the same as the order in which you define them. For example:
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
...
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-2</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
...
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-3</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
...
</execution>
</executions>
</plugin>
In the above instance, the execution order would be:
maven-plugin-3 (generate-resources)
maven-plugin-1 (process-resources)
maven-plugin-2 (process-resources)

Plugins in the same phase are executed in the declared order.
In the case of pom hierachy, you have to re-declare the plugins from the parent pom (just its groupId and its artifactId) into the child pom to specify the execution order :
Parent pom.xml
<plugins>
<plugin>
<groupId>groupid.maven.1</groupId>
<artifactId>maven-plugin-1</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
Child pom.xml
<plugins>
<plugin>
<groupId>groupid.maven.2</groupId>
<artifactId>maven-plugin-2</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>groupid.maven.1</groupId>
<artifactId>maven-plugin-1</artifactId>
</plugin>
</plugins>
Then the execution is :
maven.plugin.2
maven.plugin.1

In Maven 3.0.3 and later, there are two rules
Plugin executions are ordered according to their phases. See
https://maven.apache.org/ref/current/maven-core/lifecycles.html for
the order of phases.
For example, here mavin-plugin-1 is executed before maven-plugin-2
because the process-resources phase is defined as taking place before
the compile phase.
<plugin>
<artifactId>maven-plugin-2</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>compile</phase>
...
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
...
</execution>
</executions>
</plugin>
If multiple executions have the same phase, then the first one to be
executed will be the built-in one (e.g. maven-compiler-plugin) whose id
is default-something, then the other executions will take place in the
order they appear in your pom file.
For example, if you have this somewhere in your pom
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<id>my-compile</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-2</artifactId>
<version>4.5.6</version>
<executions>
<execution>
<id>my-compile-2</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
and this anywhere in your effective pom
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>**default-compile**</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
...
</executions>
</plugin>
then maven-compiler-plugin will execute maven-compiler-plugin followed by maven-plugin-1, and maven-plugin-2.
If you want maven-compiler-plugin:compile goal to execute after maven-plugin-1 then you could do this
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<id>my-compile</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>something-other-than-**default-compile**</id>
<phase>compile</phase>
</execution>
<execution>
<id>**default-compile**</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

This might be an old questions and the answers provided are correct, just adding an extra point regarding the case for inheritance because I found myself in this situation and could not figure it out.
There seem to be 2 rules for plugin order execution when no inheritance is provided:
The phases to which the plugins are bound to are executed in the order provided in the documentation: see "Default Lifecycle" on Introduction to the Build Lifecycle
Plugin executions are ordered according to their phases.
"In Maven 2.0.5 and above, multiple goals bound to a phase are executed in the same order as they are declared in the POM, however multiple instances of the same plugin are not supported. Multiple instances of the same plugin are grouped to execute together and ordered in Maven 2.0.11 and above" - source
IN THE CASE OF INHERITANCE: "Parent POM bindings execute AFTER child bindings.". So if your plugins are not executed in the order you expect them to (by following the previous two points mentioned in this answer), you should maybe look if they are defined in the parent POM. - Incorrect execution order of plugins in the same phase.
I could not find this being clarely documented anywhere, however there is an open ticket to document the algorithm calculating the order of plugin executions.

Related

Unit Test Case reporting for WSO2 EI

I am trying to integrate Unit Test cases in my existing wso2 code base.
Is there any way we can have a reporting structure like Surefire reports for our Unit Test Cases.
You need to add the plugin synapse-unit-test-maven-plugin in your pom.xml
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>synapse-unit-test-maven-plugin</artifactId>
<version>5.2.27</version>
<executions>
<execution>
<id>synapse-unit-test</id>
<phase>test</phase>
<goals>
<goal>synapse-unit-test</goal>
</goals>
</execution>
</executions>
<configuration>
<server>
<testServerType>${testServerType}</testServerType>
<testServerHost>${testServerHost}</testServerHost>
<testServerPort>${testServerPort}</testServerPort>
<testServerPath>${testServerPath}</testServerPath>
</server>
<testCasesFilePath>${project.basedir}/test/${testFile}</testCasesFilePath>
<mavenTestSkip>${maven.test.skip}</mavenTestSkip>
</configuration>
</plugin>
The content of the tag <testCasesFilePath> is the path where units tests are saved

Maven cargo plugin with Surefire + Jacoco report

I have a project that fires up tomcat using the cargo plugin and then runs integration tests against that tomcat instance. My goal is get integration test coverage reports of the code that runs in tomcat (not coverage of my integration tests).
The question is, how do I get code coverage results of code running in tomcat (separate JVM)?
I have been able to get coverage reports of the integration tests itself and java classes within the test module, however these are rather worthless.
In the jacoco-sessions.html file, I can only see classes that are available in the test module's classpath. Classes that are running in the tomcat server are not present.
In addition to the answer of Godin you also have to make sure that the coverage data of the test classes itself doesn't use the same output file as the coverage data of the code running in the cargo container. I think in my case the coverage-of-the-tests was overwriting the coverage-of-the-code-under-test.
Note that I am using jacoco.exec for the coverage data of the code-under-integration-test. Normally this is used for the unit tests, but there are no unit tests in my module. This way I don't need to configure SonarQube for an extra filename, but if you like you can use another filename here.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent-integration-cargo</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>argLineCargo</propertyName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<!-- ... -->
</container>
<configuration>
<properties>
<cargo.start.jvmargs>${argLineCargo}</cargo.start.jvmargs>
<!-- ... -->
</properties>
</configuration>
<!-- ... -->
</configuration>
<!-- ... -->
</plugin>
And the JaCoCo configuration in the parent POM:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>
<include>my/project/package/**/*</include>
</includes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>reports</id>
<goals>
<goal>report</goal>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>#{argLine}</argLine>
</configuration>
</plugin>
</plugins>
</build>
According to documentation of jacoco-maven-plugin http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html :
Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:
tycho.testArgLine for packaging type eclipse-test-plugin and
argLine otherwise.
argLine property affects maven-surefire-plugin and maven-failsafe-plugin that start JVM with tests - see http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine and http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#argLine respectively, but this property has no effect on cargo-maven2-plugin that starts JVM with Tomcat.
According to https://stackoverflow.com/a/38435778/244993 and https://codehaus-cargo.github.io/cargo/Tomcat+9.x.html you need to pass property set by jacoco-maven-plugin to cargo-maven2-plugin as cargo.jvmargs.

Jacoco.exec is not getting created for all modules in multi-module maven project

We are using JaCoCo for unit test code coverage in our multi-module maven project and able to get the coverage info(jacoco.exec) for most of the modules where unit tests are available. The only issue that i'm seeing is, jacoco.exec is not getting generated for some of the classes in few modules even though unit tests are available (note that surefire-reports are generated).
Able to get coverage info for the same unit tests when used Cobertura.
The following is the parent pom.xml where I've added jacoco-maven-plugin info.
Please help if there are any issues with pom.xml.
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>module4</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>#{argLine} -Xmx500m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
</plugins>
</build>
Thanks in advance!!

axistools-maven-plugin not generating source from WSDL

I am using Apache Axis to generate the java source from my WSDL file. The maven run was successful without any errors but no generated classes.
Question: What am I missing here?
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<sourceDirectory>/src/main/resources</sourceDirectory>
<outputDirectory>/src/main/java</outputDirectory>
<wsdlFiles>
<wsdlFile>thesourcewsdlfile.wsdl</wsdlFile>
</wsdlFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I am clearly not sure what is the problem with this configuration.
Try to define source and output directory bases on your maven project, that is, using standard maven properties to point to the right absolute path at runtime, changing your configuration as following:
<sourceDirectory>${basedir}/src/main/resources</sourceDirectory>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
Based on this configuration, the thesourcewsdlfile.wsdl is supposed to be located under src/main/resources/thesourcewsdlfile.wsdl
The full plugin configuration should hence be:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>axistools-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>wsdl2java</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/src/main/resources</sourceDirectory>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<wsdlFiles>
<wsdlFile>thesourcewsdlfile.wsdl</wsdlFile>
</wsdlFiles>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Moreover, the following dependencies must be added to the project:
<dependencies>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxrpc-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
As a general note, it would be much better to place generated sources under the target directory, since they are generated automatically and should not be added to the version control in use. A standard location would then be any desired (meaningful) directory under ${project.build.directory}/generated-sources (where ${project.build.directory} is the standard property pointing to the target directory indeed).
IMHO you are missing the wsdl folder under resources, just add it and run:
mvn clean generate-sources

Running tests after packaging

One of my projects needs a pretty complex setup for the resulting JAR file, so I'd like to run a test after the package phase to make sure the JAR contains what it should.
How do I do that with Maven 2?
You can use the surefire-plugin for this. what you need to do is associate a phase with an execution (see below). You will need to change the phase to be whatever you want it to be in your case one after the package phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>unittests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/**/**/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Convert your project into a multi-module build. In the first module, build your original project. In the second module, add a dependency to the first.
This will add the first JAR to the classpath.
Update by OP: This works but I had to add this to my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
The important part is <useSystemClassLoader>false</useSystemClassLoader>. Without this, my classpath only contained a couple of VM JARs plus the surefire bootstrap JAR (which contains the test classpath in the MANIFEST.MF). I have no idea why this test classpath isn't visible from the classes loaded from it.