Executing maven-compiler-plugin's generate source only if the sources have not changed - maven

I have a section in my pom.xml that generates OpenJPA's metamodel like this:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>generate-entity-metamodel</id>
<phase>generate-sources</phase>
<goals>
<goal>
compile
</goal>
</goals>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<optimize>true</optimize>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<proc>only</proc>
<compilerArgument>-Aopenjpa.metamodel=true</compilerArgument>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/openjpa-metamodel</generatedSourcesDirectory>
</configuration>
</execution>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The "problem" I have is that this "default" execution of maven-compiler-plugin is called only when there is a change in source code - which is expected, but "generate-entity-metamodel" is called always which leads to failure in build since those generate classes already exist from a previous build. I have always to do mvn clean compile in order for my build to succeed.
How can I fix this and have "generate-entity-metamodel" called only when there were changes in source files?
Here's the output when I do mvn test
[INFO] --- build-helper-maven-plugin:1.8:add-source (default) # icmynet-core ---
[INFO] Source directory: E:\Projekti\release-repo\icmynet\trunk\icmynet-core\target\generated-sources\openjpa-metamodel added.
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (generate-entity-metamodel) # icmynet-core ---
[INFO] Changes detected - recompiling the module!
Hm... why are there "changes detected"?

Related

maven - why is dependency check skipped?

In my pom plugins I have...
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>6.2.2</version>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
But when I run mvn dependency-check:check or mvn verify in the logs I see
[INFO] --- dependency-check-maven:6.2.2:check (default-cli) # my-project ---
[INFO] Skipping dependency-check
How do I get it to run??
Turned out the dependency-check.skip was evaluating to true in the properties.
I simply set it to false.

Maven skip copy of test resources

I want to skip copying the test resources during build.
I tried the following plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>testResources</goal>
</goals>
<phase>process-test-resources</phase>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
Alas it's still copying the resources. I tried a couple of different phases (compile, test) but to no avail.
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) # Project---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5635 resources
What do I have to do so this is skipped?

How to generate Jacoco integration test report using offline instrumentation?

Build tool: Maven
Reason to use offline instrumentation: Removing Powermock is not an option
Problem:
Both failsafe and surefire are run and reports are generated. However, jacoco.exec is generate but jacoco-it.exec is not. Apart from IT , offline instrumentation, coverage and reporting works fine.
This is the maven plugin configuration I use:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
To run the test i use maven clean install.
At the end of the test execution I get the following output:
[INFO]
[INFO] --- maven-failsafe-plugin:2.15:integration-test (integration-tests) # elune ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:report (default-report) # elune ---
[INFO] Loading execution data file C:\Projects\elune\target\jacoco.exec
[INFO] Analyzed bundle 'elune' with 4 classes
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:report-integration (default-report-integration) # elune ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
Another possible pointer might be that the de-instrumentation of the classes happens after the unit tests are run but before the integration tests. But I don't know if this is right or wrong:
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:restore-instrumented-classes (default-restore-instrumented-classes) # elune ---
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) # elune ---
[INFO] Building jar: C:\Projects\elune\target\elune-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) # elune ---
[INFO]
[INFO] --- maven-failsafe-plugin:2.15:integration-test (default) # elune ---
[INFO] Failsafe report directory: C:\Projects\elune\target\failsafe-reports
Any idea why the jacoco-it.exec is not showing up?
I think the instrumentation of Integration Tests with the failsafe plugin are just not baked in. The restore-instrumented-classes goal defaults to the prepare-package phase which runs before the integration test phase: http://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html - so it may be enough to move that goal into the post-integration-test phase:
<execution>
<id>default-restore-instrumented-classes</id>
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
that may be enough. Otherwise you may be able to change the includes pattern of the surefire plugin to also include the integration tests (if that seems suitable in your case).
#rhinoceros.xn this is the plugin configuration that did the trick for me. But I use mvn clean install to run this
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
The jacoco-it.exec is not showing up after I set the phrase.
command line is:
mvn clean install -Djacoco.version=0.7.8 -DfailOnError=false -Dmaven.test.failure.ignore=true
I finally found the problem was that I didn't add configuration jacoco-agent.destfile in maven-failsafe-plugin.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco-it.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Jacoco Report are not generated in the site

Jacoco reports are not generated when mvn builds the site.
Jacoco reports are generated in my target folder at target\site\jacoco , but somehow I am not able to see "Jacoco Tests" option under "Project Reports" when I try to build the jar site
The only thing I see in the log while generating the site is as below :
[INFO] <<< clirr-maven-plugin:2.6.1:clirr # data-binding <<<
[INFO] configuring report plugin com.cerner.engineering:maven-cerner-ml-dependency-plugin:1.1.1
[INFO] configuring report plugin org.jacoco:jacoco-maven-plugin:0.7.4.201502262128
[INFO] Skipping JaCoCo execution due to missing execution data file:C:\MSVCWorkspace\br-hla-april-data-binding\target\jacoco.exec
I am using the command mvn clean install site
This is how I have configure jacoco in my pom.xml in my build section :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*Suite*.java</exclude>
</excludes>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-Xms64m -Xmx512m -XX:MaxPermSize=256M -enableassertions -XX:+HeapDumpOnOutOfMemoryError ${jacoco.argLine}</argLine>
<junitArtifactName>junit:junit-dep</junitArtifactName>
</configuration>
</plugin>
Jacoco plugin is called like :
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jacoco.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
I am not sure what is causing this issue , not to generate Jacoco reports on my site.
Although I am able to see the below in my logs
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:prepare-agent (default-cli) # data-binding ---
[INFO] argLine set to -javaagent:C:\\_repository\\org\\jacoco\\org.jacoco.agent\\0.7.4.201502262128\\org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=C:\\target\\coverage-reports\\jacoco-unit.exec
Thanks !!!
I was able to fix by calling plugin as below
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jacoco.argLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>

Why does jetty server hangs after server start and doesn't run integration test?

I am running integration test using maven-jetty-plugin and maven-failsafe-plugin. Here's my configuration:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>3600000</maxIdleTime>
</connector>
</connectors>
<contextPath>/</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>**/*.jsp</exclude>
<exclude>**/*.html</exclude>
</excludes>
<includes>
<include>**/*.page</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
When i run mvn clean install, jetty server starts and after that nothing happens (it gets stuck). Last line in my logs is : [INFO] Started Jetty Server. When i press control-c, here is what it prints:
2013-04-25 15:24:16.315:INFO::Shutdown hook executing
2013-04-25 15:24:16.317:INFO::Stopped SelectChannelConnector#0.0.0.0:8080
2013-04-25 15:24:16.821:INFO:/ca-app:Shutting down log4j
2013-04-25 15:24:16.821:INFO:/ca-app:Closing Spring root WebApplicationContext
2013-04-25 15:24:22.108:INFO::Shutdown hook complete[INFO]
Jetty server exiting.
[INFO]
[INFO] --- maven-failsafe-plugin:2.7.1:integration-test (default) # my-app ---
[INFO] Failsafe report directory: my-app/target/failsafe-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-jetty-plugin:6.1.26:stop (stop-jetty) # my-app ---
[INFO]
[INFO] --- maven-failsafe-plugin:2.7.1:verify (default) # my-app ---
[INFO] Killing Jetty
[INFO] Failsafe report directory: my-app/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) # my-app ---
[INFO] Installing my-app/target/ca-app-0.1.5-SNAPSHOT.war to ~/.m2/....../my-app/0.1.5-SNAPSHOT/my-app-0.1.5-SNAPSHOT.war
[INFO] Installing my-app/pom.xml to ~/.m2/....../my-app/0.1.5-SNAPSHOT/my-app-0.1.5-SNAPSHOT.war
[INFO]
[INFO] --- maven-dependency-plugin:2.1:sources (install) # my-app ---
Why does it get stuck? When i press control-c, why does it perform rest of the steps? How can i fix it?
Try using start instead of run-war.
From javadoc on org\eclipse\jetty\jetty-maven-plugin\9.3.0.M2\jetty-maven-plugin-9.3.0.M2.jar!\org\eclipse\jetty\maven\plugin\AbstractJettyMojo.nonblocking
Determines whether or not the server blocks when started. The default behavior (false) will cause the server to pause other processes while it continues to handle web requests. This is useful when starting the server with the intent to work with it interactively. This is the behaviour of the jetty:run, jetty:run-war, jetty:run-war-exploded goals.
If true, the server will not block the execution of subsequent code. This is the behaviour of the jetty:start and default behaviour of the jetty:deploy goals.
So in order to start the jetty add run:
<goals>
<goal>stop</goal>
<goal>start</goal>
</goals>
And the whole:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.10.v20150310</version>
<configuration>
<war>webapp/target/MyWar.war</war>
<stopKey>fooKey</stopKey>
<stopPort>8081</stopPort>
<stopWait>1</stopWait>
<httpConnector>
<port>8888</port>
</httpConnector>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>stop</goal>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<stopKey>STOP</stopKey>
<stopPort>8866</stopPort>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<stopKey>STOP</stopKey>
<stopPort>8866</stopPort>
</configuration>
</execution>
</executions>
</plugin>

Resources