maven-compiler-plugin exclude - compilation

I have a following problem.
I would like to exclude some .java files (**/jsfunit/*.java) during the test-compile phase and on the other side I would like to include them during the compile phase (id i start tomcat with tomcat:run goal)
My pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<!-- <excludes>
<exclude>**/*JSFIntegration*.java</exclude>
</excludes> -->
</configuration>
<executions>
<!-- <execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/jsfunit/*.java</include>
</includes>
</configuration>
</execution>-->
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<configuration>
<excludes>
<exclude>**/jsfunit/*.java</exclude>
</excludes>
</configuration>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
But it does not work : exclude in default-testCompile execution does not filter these classes.
If I remove the comments then all classes matched **/jsfunit/*.java would be compiled but only if I touch them!

To exclude files from the default-testCompile phase, you have to use <testExcludes>. So your example above would look like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<configuration>
<testExcludes>
<exclude>**/jsfunit/*.java</exclude>
</testExcludes>
</configuration>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>

Related

Jacoco test coverage report is not generated

I have configured the maven jacoco plugin as follows in my pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<systemPropertyVariables>
<application.name>${run.application.name}</application.name>
<application.home>${run.application.home}</application.home>
<application.environment>${run.application.environment}</application.environment>
<spring.profiles.active>${run.application.environment}</spring.profiles.active>
</systemPropertyVariables>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
<argLine>
--illegal-access=permit
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<argLine>
--illegal-access=permit
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-plugin.version}</version>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<limits>
<limit>
<counter>INSTRUCTION</counter>
<value>COVEREDRATIO</value>
<minimum>0.85</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>check</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
I tried by setting different parameters but could not make it work.
Whenever I am running the test cases using mvn clean install or mvn jacoco:report I am not getting -- Skipping JaCoCo execution due to missing execution data file.
Update--
when I removed this param in maven-surefire-plugin
<argLine>
--illegal-access=permit
</argLine>
the reports were generated but not sure what is the reason behind it.

How disable maven-surefire-plugin:2.22.2 (default-test) and able maven-failsafe-plugin?

I need disable surefire (default-test of spring) because I'm course and they did the Integrations-Test through failsafe.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement>
</additionalClasspathElements>
<parallel>none</parallel>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
I tried to do with this but did't work
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
when I run the test this is the result, the test is execute in org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test
enter image description here

Running maven-shade-plugin only for package phase

Within my pom.xml I define following plugins - maven-shade, maven-compiler and maven-failsafe:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>shade-main</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>target/assembly/${project.artifactId}.jar</outputFile>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals> <goal>testCompile</goal> </goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
When I run mvn package it nicely compiles the project and builds a jar with shade-plugin. However when I run mvn verify the shade-plugin should not be run, but it does and it takes unnecessary time before the tests are executed.
How can I prevent the shade plugin to run on mvn verify?
EDIT:
After some comments I want to clarify I still need to do the package phase, I just want to skip the execution of shade plugin.

Have integration tests in jacoco-it directory

I have integration tests and they are executed fine, but Jacoco considers them as unit tests. How to tell Jacoco to see them as integration tests and display their graph coverage, not in the jacoco-ut directory, but it the jacoco-it directory ?
Here in the Maven configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<excludes>
<exclude>**/it/java/*.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-it-resources</id>
<phase>pre-integration-test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/it-classes</outputDirectory>
<resources>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<testSourceDirectory>src/it/java</testSourceDirectory>
<testClassesDirectory>${project.build.directory}/it-classes</testClassesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<skip>${maven.test.skip}</skip>
<output>file</output>
<append>true</append>
<excludes>
<exclude>**/config/*</exclude>
<exclude>**/dialect/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<phase>process-test-classes</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
<includes><include>**/ut/*</include></includes>
</configuration>
</execution>
<execution>
<id>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>
<execution>
<id>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>failsafeArgLine</propertyName>
<includes><include>**/it/*</include></includes>
</configuration>
</execution>
<execution>
<id>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>
UPDATE: I was missing this bit in the maven-failsafe-plugin plugin:
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
After having added it as in:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<includes>
<include>**/it/**</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
the integration tests report shows the integration tests.
The final full configuration is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<includes>
<include>**/ut/**</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>process-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<includes>
<include>**/it/**</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<configuration>
<skip>${maven.test.skip}</skip>
<output>file</output>
<append>true</append>
<excludes>
<exclude>**/config/*</exclude>
<exclude>**/dialect/*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<phase>process-test-classes</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>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>
<execution>
<id>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>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>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>
You misunderstood the pattern for the include tag of maven-failsafe-plugin. The pattern is used for the package name of the classes within the test classpath (see also https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html).
The build-helper-maven-plugin includes all classes in src/it/java to the test classpath. Then the pattern of include tag in both test plugins (surefire, failsafe) filters the classes within this test classpath. These filtered classes are executed by the test plugins.
So the workflow is
Build-Helper-Plugin extends the set of the test classes that have to be compiled and executed. Therefore the source directive is used. The path location defined in this source directive is related to the Maven project path.
Compiler Plugin compiles the java classes.
Jacoco-Plugin should measure the coverage in the production classes with unit tests. Therefore, the normal agent has to be prepared before the tests are executed. You can specify which production code should be included or excluded in the coverage measurment (include/exclude directive). The pattern in these directives are package-based (shlashes are used instead of dots).
Surefire-Plugin executes the tests whose full-qualified class name match the pattern of the include directive. The pattern uses slashes instead of dots.
Now the Jacoco-Plugin should measure the coverage in the production classes with integration tests. Therefore, the integration agent has to be prepared before the tests are executed. And again, you can specify which production code should be included or excluded in the coverage measurment (include/exclude directive). The pattern in these directives are package-based (shlashes are used instead of dots).
The same for the Failsafe-Plugin. It executes the tests whose full-qualified class name match the pattern of the include directive. The pattern uses slashes instead of dots.

Attaching only a contents of the folder to the jar

I am using maven-jar-plugin to generate a sources jar.
I have the following folder structure:
folder/foo/baz/A.java
folder/bar/baz/B.java
I want the sources jar to contain the following:
baz/A.java
baz/B.java
I am using the following configuration:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>folder</classesDirectory>
<includes>
<include>foo/**</include>
<include>bar/**</include>
</includes>
<finalName>sources</finalName>
</configuration>
</execution>
</executions>
</plugin>
But this creates the jar like this:
folder/foo/baz/A.java
folder/bar/baz/B.java
How can i modify the code to get my desired structure in the jar?
I could not solve this with the maven-jar-plugin, i had to use maven-resources-plugin, too:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/sources/</outputDirectory>
<resources>
<resource>
<directory>folder/foo/</directory>
</resource>
<resource>
<directory>folder/bar/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.directory}/sources</classesDirectory>
<includes>
<include>**/*</include>
</includes>
<finalName>sources</finalName>
</configuration>
</execution>
</executions>
</plugin>

Resources