Specify the order in which Maven plugins' executions run - java

I'm using the copy-rename-maven-plugin during the package phase to first copy the jar that I just generated to another directory, then using launch4j-maven-plugin I'm generating exes that wrap the jar and then I need to rename one of the exes (to scr), so, I'm using copy-rename-maven-plugin again.
The problem is that all copy-rename-maven-plugin executions are run together, before launch4j-maven-plugin, so, the second execution fails.
How do define the order of executions? I'm happy creating more phases if that's what's necessary, but creating a Maven plugin seemed a bit of an overkill.
A simplified example of what's going with my pom.xml would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>tech.projecx</groupId>
<artifactId>projecx</artifactId>
<version>1.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution> <!-- Copy the just-built projecx jar to targte/win32/jars -->
<id>copy-jar-for-exe</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/${project.build.finalName}.jar</sourceFile>
<destinationFile>${project.build.directory}/win32/jars/${project.build.finalName}.jar
</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <!-- Make the exes -->
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.21</version>
<executions>
<execution> <!-- Make the screensaver exe -->
<id>wrap-screensaver-as-exe</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<outfile>${project.build.directory}\win32\${screensaverExe}.exe</outfile>
<jar>jars\${project.build.finalName}.jar</jar>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution> <!-- Copy the screensaver from the exe to the proper scr -->
<id>rename-screensaver-to-scr</id>
<phase>package</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/win32/${screensaverExe}.exe</sourceFile>
<destinationFile>${project.build.directory}/win32/${screensaverExe}.scr</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The order in which executions need to run is this:
copy-jar-for-exe
wrap-screensaver-as-exe
rename-screensaver-to-scr
Any other order doesn't work, but because, I think, copy-jar-for-exe and renamer-screensaver-to-scr are executions from the same plugin, Maven runs it like this:
copy-jar-for-exe
rename-screensaver-to-scr
wrap-screensaver-as-exe
so, it fails.

You could run the copy-jar-for-exe in the prepare-package phase. I beleive you could define both executions in the same plugin configuration but declare the plugin after the launch4j plugin.
Basic idea is, the plugins with executions in the same phase are executed in the order of appearance in the pom. If you bind a single execution to another (earlier) phase, it should be executed before.
I haven't tested this, but I think it should work
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.21</version>
<executions>
<execution> <!-- Make the screensaver exe -->
<id>wrap-screensaver-as-exe</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<outfile>${project.build.directory}\win32\${screensaverExe}.exe</outfile>
<jar>jars\${project.build.finalName}.jar</jar>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>copy-jar-for-exe</id>
<phase>prepare-package</phase> <!-- run this execution before package phase -->
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/${project.build.finalName}.jar</sourceFile>
<destinationFile>${project.build.directory}/win32/jars/${project.build.finalName}.jar
</destinationFile>
</configuration>
</execution>
<execution>
<id>rename-screensaver-to-scr</id>
<phase>package</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/win32/${screensaverExe}.exe</sourceFile>
<destinationFile>${project.build.directory}/win32/${screensaverExe}.scr</destinationFile>
</configuration>
</execution>
</executions>
</plugin>

Related

Add one additional File as Project Artifact

Im trying to make a maven project which has only one xml file as artifact.
Currently i zip the file with this Configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/master.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
But i would like to have the file itself be the artifact.
Is it possible to tell the assemply plugin to just use a file as artifact?
You can use the deploy:deploy-file goal instead. It allows you to upload arbitrary files.
You can use the build-helper-maven-plugin and the goal attach-artifact which looks like this.
<project>
...
<build>
<plugins>
<plugin>
<!-- add configuration for antrun or another plugin here -->
</plugin>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>some file</file>
<type>extension of your file </type>
<classifier>optional</classifier>
</artifact>
...
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But usually I can't recommend that.

How can I build two repackaged jars with Spring Boot

I'm trying to get spring-boot-maven-plugin to build two repackaged jars for two different main classes. I'added two execution blocks with their specific mainClass parameter in the configuration block but it seems that the plugin does not respect it because the configuration block is inside the execution block and not on plugin level and I always get
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.mystuff.tools.b4commandline.Application, com.mystuff.tools.loadtester.Application]
here's the plugin section of maven-spring-boot-maven plugin of the pom.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>${artifactId}</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.mystuff.tools.loadtester.Application</mainClass>
<finalName>${artifactId}</finalName>
</configuration>
</execution>
<execution>
<id>b4-commandline</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.mystuff.tools.b4commandline.Application</mainClass>
<finalName>b4-commandline</finalName>
</configuration>
</execution>
</executions>
</plugin>
Any help on this would be great. I saw recipes on stackoverflow for the maven-plugin but those do not apply to spring-boot.
You can do this with <classifier>. Example:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<executions>
<execution>
<id>Pack application 1</id>
<phase>package</phase>
<configuration>
<finalName>application</finalName>
<mainClass>com.test.Application1</mainClass>
<outputDirectory>target/application1</outputDirectory>
<classifier>1</classifier>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Pack application 2</id>
<phase>package</phase>
<configuration>
<finalName>application</finalName>
<mainClass>com.test.Application2</mainClass>
<outputDirectory>target/application2</outputDirectory>
<classifier>2</classifier>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
I could not make it work both within a single build (I'm not saying it is not possible though), however one option is to define 2 maven profiles:
<profiles>
<profile>
<id>one</id>
<activation>
<property>
<name>one</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xyz.LauncherOne</mainClass>
<finalName>one</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>two</id>
<activation>
<property>
<name>two</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xyz.LauncherTwo</mainClass>
<finalName>two</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then, build it twice using both profiles (activated with a property in my code sample):
$ mvn -Done clean install
$ mvn -Dtwo clean install
It creates: one.jar and two.jar
Assuming its doable, doing what you are asking would mean that the 2 jars produced would be identical except their manifest's main-class attribute. I would rather suggest you to package a single jar and use spring profiles to launch it:
$ java -jar -Dspring.profiles.active=profile1 YourApp.jar
$ java -jar -Dspring.profiles.active=profile2 YourApp.jar
This allows you to define 2 application-${profile}.properties, and #Conditional configuration classes, all based on the profile name.
It seems that you must consider first execution without id and another executions with id; Then it is working.
for me below working fine:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<!--<id>Batch 1(Don't put this one)</id>-->
<phase>package</phase>
<configuration>
<classifier>1</classifier>
<finalName>Core</finalName>
<mainClass>com.mainclass1</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Batch 2</id>
<phase>package</phase>
<configuration>
<classifier>2</classifier>
<finalName>BatchA</finalName>
<mainClass>com.mainclass2</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Batch 3</id>
<phase>package</phase>
<configuration>
<classifier>3</classifier>
<finalName>BatchB</finalName>
<mainClass>com.mainclass3</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

How to make PMD run at the start of the maven build rather than at the end of it?

I have the following configuration within my pom.xml that checks for PMD violations:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
When I run a build using the command mvn clean install, the PMD checks are run as last step of the build process. Rather, I would want the PMD checks to run as the first step of the build.
Does anybody know how could I achieve this?
Add the phase element to your POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
The validate phase is the first phase of the maven lifecycle: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
Thanks for your answers #JamesB and #PetrMensik for letting me know about the phase element within the POM. It helped me solve my problem. I finally settled for this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.version}</version>
<configuration>
<linkXRef>true</linkXRef>
<sourceEncoding>UTF-8</sourceEncoding>
<minimumTokens>100</minimumTokens>
<targetJdk>1.7</targetJdk>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
I used the phase:compile, the reason being I have plenty of tests in my project which take up a lot of time to execute. And, its quite irritating to wait for those tests to finish and be notified about a PMD violation at the end of all the tests. I needed something just before the tests. Hence, I settled for compile.
Further suggestions are welcome. :)
You need to hook execution of this plugin to a different Maven lifecycle phase (validation comes as the first one in default lifecycle).
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
See this the list of the available Maven phases for reference.
I set build configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
<configuration>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Tomcat7 Maven Plugin and JaCoCo

Is there any way to get code coverage using JaCoCo with the tomcat7-maven-plugin embedded instance?
The jacoco-maven-plugin is configured in my WAR's POM to instrument my unit tests, but I'm not sure how to attach the jacoco agent to the embedded Tomcat instance to instrument my integration tests that run against Tomcat. Given that the Tomcat instance is embedded, I'm not sure if this approach is possible. Is there any other way to accomplish this? I can probably switch from using the Tomcat Maven Plugin to using Cargo to get coverage, but I'd prefer to stick with the Tomcat plugin if possible.
Here are a few relevant snippets from my POM:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<systemProperties>
<!-- as expected, this system property doesn't work since Tomcat is embedded, but this is the type of config I'm looking for -->
<JAVA_OPTS>-javaagent:${project.build.directory}/${jacoco.jar}=destfile=${project.build.directory}/jacoco.exec,append=true</JAVA_OPTS>
</systemProperties>
</configuration>
<executions>
<execution>
<id>tomcat-startup</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
Versions: Maven 3.0.4, Tomcat Maven Plugin 2.1, Jacoco 0.6.2.201302030002, Java 7
I know its been awhile since the question was posted but I don't feel the answer really addressed the root of the problem. Code coverage may work with failsafe or surefire if you are running tests within those plugins. However, if you just want to monitor tomcat with jacoco to get a coverage report current information doesn't provide that. I found that the tomcat7-maven-plugin doesn't allow you to inject the -javaagent for jacoco by simply providing JAVA_OPTS. Switching to cargo I was able to do that like so.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
<classDumpDir>${project.reporting.outputDirectory}/jacoco-it/classes</classDumpDir>
<skip>${skipITs}</skip>
</configuration>
<executions>
<execution>
<id>jacoco-agent</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>jacoco.agent.itArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>dump</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.11</version>
<configuration>
<skip>${skipITs}</skip>
<container>
<containerId>tomcat7x</containerId>
<zipUrlInstaller>
<url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip
</url>
<downloadDir>${project.build.directory}/downloads</downloadDir>
<extractDir>${project.build.directory}/extracts</extractDir>
</zipUrlInstaller>
<dependencies>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc6</artifactId>
</dependency>
</dependencies>
</container>
<configuration>
<home>${project.build.directory}/catalina-base</home>
<properties>
<cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 -Drunmode=TEST</cargo.jvmargs>
<cargo.servlet.port>9090</cargo.servlet.port>
</properties>
<configfiles>
<configfile>
<file>${basedir}/src/test/conf/context.xml</file>
<todir>conf/Catalina/localhost/</todir>
<tofile>context.xml.default</tofile>
</configfile>
</configfiles>
</configuration>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
the important parts are:
<plugin><groupId>org.jacoco</groupId> ...<propertyName>jacoco.agent.itArgLine</propertyName>
<cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 </cargo.jvmargs>
When report target is run on the jacoco plugin it will create a directory in ${projectbase}/target/site/jacoco-it/index.html with your coverage report. I use this with the soapui-maven-plugin but it could be used with selenium-maven-plugin also.
You don't need pass JAVA_OPTS to tomcat embedded if you use maven-failsafe-plugin (or maven-surefire-plugin) to run yours integration test. It is because tomcat embedded run in the same process of maven-failsafe-plugin.
So when jacoco-maven-plugin execute prepare-agent it sets argLine that maven-failsafe-plugin uses too.
I created a project to test this, below part of pom:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<includes>
<include>my.project.package.only.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>tomcat-startup</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
<configuration>
<background>true</background>
<logOutput>true</logOutput>
<multiWindow>true</multiWindow>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
you can try to take a look at a workaround at this post: http://dougonjava.blogspot.co.il/2013/07/integration-testing-using-maven-tomcat.html
I resolved problems when setting up JaCoCo agent with embedded Tomcat by instrumenting classes offline and then just placing JaCoCo agent on Tomcat classpath (plugin dependency) and adding file jacoco-agent.properties.
I put the working configuration on my blog:
http://burkond.blogspot.de/2014/05/selenium-in-sonar-code-coverage-metrics.html
I had the exact same problem and the only solution I found was to set the MAVEN_OPTS previously to the Maven build (for example on the command line or in the configuration of a Jenkins job):
export MAVEN_OPTS=-javaagent:~/.m2/repository/org/jacoco/org.jacoco.agent/0.7.4.201502262128/org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=./target/jacoco.exec,append=true
This will attach the jacoco agent to the embedded tomcat instance, which will report back the coverage results into the given destfile.
First, it is important that the path to the jacoco runtime JAR is correct. You can manually download and refer to it or use another Maven command to download it into your local .m2 repository:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.jacoco:org.jacoco.agent:0.7.4.201502262128:jar:runtime
Second, make sure that the path to the jacoco.exec file is correct. In my case, I already have an existing jacoco.exec file in the target folder, which contains unit test results. The append=true makes sure that unit and integration tests are combined.
I managed to do it and it involves some tinkering with finicky stuff:
server/container needs to be on a separate jvm that can receive arguments (jacoco-agent). Cargo using embedded containers did not seem to work and was a pain to debug...
jvm with jacoco-it needs to stop before the jacoco analysis (duh!) but registering container-stop and jacoco-report on post-integration-test does not guarantee this... (the tcpdump, etc option in a previous answer had this problem)
defining random ports for the server/container makes this easy to integrate with continuous integration
phantomjs is an extra ;)
jacoco should be used as prepare-agent-integration and report-integration for integration-test (does not really make a difference)
Should be run as 'mvn clean verify'
Pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<executions>
<!-- unit test coverage -->
<execution>
<id>jacoco-pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>jacoco.ut.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
<!-- integration test coverage -->
<execution>
<id>jacoco-pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<propertyName>jacoco.it.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report-integration</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.6</version>
</plugin>
<!-- Installs PhantomJS so it doesn't have to be pre-installed -->
<plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>0.4</version>
<executions>
<execution>
<!-- should be post-integration-test ? -->
<phase>test</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.7</version>
</configuration>
</plugin>
<!-- Get two free ports for our test server to use -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<portNames>
<portName>jetty.port</portName>
<portName>jetty.port.stop</portName>
</portNames>
</configuration>
<executions>
<execution>
<id>reserve-port</id>
<phase>test</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Run tests (UT) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${jacoco.ut.argLine}</argLine>
<skipTests>${skip.unit.tests}</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<excludes>
<!-- no UT execution, to test only IT
<exclude>**/<remove this>*Test.java</exclude> -->
</excludes>
</configuration>
</plugin>
<!-- Use failsafe to run our integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<!-- pass these values to the test classes -->
<phantomjs.binary>${phantomjs.binary}</phantomjs.binary>
<jetty.port>${jetty.port}</jetty.port>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.16</version>
<configuration>
<skip>${skipITs}</skip>
<container>
<containerId>tomcat8x</containerId>
<zipUrlInstaller>
<!-- did not work with 'https'. Certificate problem? -->
<url>http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.26/bin/apache-tomcat-8.0.26.zip</url>
<downloadDir>${project.build.directory}/downloads</downloadDir>
<extractDir>${project.build.directory}/extracts</extractDir>
</zipUrlInstaller>
</container>
<configuration>
<home>${project.build.directory}/catalina-base</home>
<properties>
<cargo.jvmargs>${jacoco.it.argLine}</cargo.jvmargs>
<cargo.servlet.port>${jetty.port}</cargo.servlet.port>
<!-- <cargo.logging>high</cargo.logging> -->
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>cargo-stop-tomcat</id>
<phase>integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

Maven: Copy resources to dynamic directory

I'm a Maven newbie and my project is finally compiling and running correctly.
On each run my project writes reports on a dynamic location created at runtime (username_timestamp) and sets a System.property called REPORTS_LOCATION with this location. After execution I would like to copy some static resources (style, images, js, etc) to this dynamic folder using a maven goal.
What I can't figure out is how to let Maven know about this dynamic location or access this System.property
I'm about ready to just let my project copy these resources to the directory but I figure I'll give it another try in case there is an easy/Maven way of doing this.
I've gotten as far as copying the resources to a hard coded location. Here is a snippet of the POM. I'm using Jbehave's Maven goals and they do execute in order
<plugins>
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>${jbehave.core.version}</version>
<executions>
<execution>
<id>embeddable-stories</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/Stories.java</include>
</includes>
<excludes />
<metaFilters>
<metaFilter>${meta.filter}</metaFilter>
</metaFilters>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
<!-- Copy the resources AFTER the execution is done -->
<execution>
<id>unpack-view-resources</id>
<phase>integration-test</phase>
<configuration>
<viewDirectory>${basedir}/src/main/java/project/reports/{NEED TO FEED DIRECTORY HERE}</viewDirectory>
</configuration>
<goals>
<goal>unpack-view-resources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
It sounds like you have a piece of Java code calculating {username_timestamp}, and you then want that code to be able to communicate the calculated {username_timestamp} back to Maven for use in later steps of its lifecycle. I'm not sure that this is possible. Instead, how about inverting the process, so that Maven produces the timestamp, and you consume it from your code? You can achieve this using a combination of build-helper-maven-plugin, Maven resource filtering, and Java code to load a properties file.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>junk</groupId>
<artifactId>junk</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!--
Use build-helper-maven-plugin to generate a timestamp during the
initialize phase and store it as a property named "timestamp".
-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>timestamp-property</id>
<phase>initialize</phase>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<locale>en_US</locale>
<name>timestamp</name>
<pattern>yyyyMMDDHHmmssSSS</pattern>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${pom.artifactId}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!--
Turn on resource filtering so that references to ${timestamp} in a
properties file get replaced with the value of the timestamp property.
-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
src/main/resources/junk.properties
timestamp=${timestamp}
src/main/java/Main.java
import java.util.Properties;
public final class Main {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.load(Main.class.getResourceAsStream("/junk.properties"));
System.out.println(props.getProperty("timestamp"));
}
}
Thanks a lot cnauroth, this worked like a charm. Here is my working updated POM in case it helps someone else
<resources>
<resource>
<directory>${basedir}/src/main/java/resources</directory>
<excludes><exclude>**/locale/**</exclude></excludes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- Use build-helper-maven-plugin to generate a timestamp during the initialize
phase and store it as a property named "mavenTimestamp". -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>timestamp-property</id>
<phase>initialize</phase>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<locale>en_US</locale>
<name>mavenTimestamp</name>
<pattern>yyyyMMDDHHmmssSSS</pattern>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>${jbehave.core.version}</version>
<executions>
<execution>
<id>embeddable-stories</id>
<phase>integration-test</phase>
<configuration>
<includes>
<include>**/Stories.java</include>
</includes>
<excludes />
<ignoreFailureInStories>true</ignoreFailureInStories>
<verboseFailures>true</verboseFailures>
<threads>5</threads>
<metaFilters>
<metaFilter>${meta.filter}</metaFilter>
</metaFilters>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
</goals>
</execution>
<!-- THIS WORKS :) Copy the resources AFTER the execution is done -->
<execution>
<id>unpack-view-resources</id>
<phase>integration-test</phase>
<configuration>
<viewDirectory>${basedir}/src/main/java/project/reports/${mavenTimestamp}</viewDirectory>
</configuration>
<goals>
<goal>unpack-view-resources</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
If you set the folder as an environment variable during the runtime, you can do the following:
just use
<properties>
<REPORTS_LOCATION><${env.REPORTS_LOCATION}></REPORTS_LOCATION>
</properties>
then you can reference to theproperty via ${REPORTS_LOCATION} in you pom

Categories