I have a JUnit 4.12 SlowTests test suite that I want to exclude from execution unless specifically requested on the Maven command line.
I have added the following to my pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<excludedGroups>com.Example.SlowTests</excludedGroups>
<includes>
<include>**/*TestSuite.class</include>
</includes>
<excludes>
<exclude></exclude>
</excludes>
</configuration>
</plugin>
I have defined the category as SlowTests and applied it to the MySlowTests class.
I have annotated the test suite as follows:
#RunWith(Categories.class)
#IncludeCategory(SlowTests.class)
#SuiteClasses({ MySlowTests.class })
public class MySlowTestSuite
When I run mvn package all the unit tests except MySlowTests are executed.
However, looking at various answers such as https://stackoverflow.com/a/25639372/820657 and https://stackoverflow.com/a/21830866/820657 I expected the following command:
mvn package -Dgroups=com.Example.MySlowTests
to run the excluded MySlowTests tests but they don't run. In fact no tests run.
What am I doing wrong?
The Maven Surefire plugin has some issues w.r.t categories in versions < 2.13 (IIRC) but as long as you are using Surefire >= 2.13 the following will run any classes annotated with #Category(com.yourcompany.SlowTests.class):
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>com.yourcompany.SlowTests</groups>
</configuration>
</plugin>
This approach is often used with profiles, the following configuration ...
<profiles>
<profile>
<id>slow</id>
<properties>
<testCategories>com.yourcompany.SlowTests</testCategories>
</properties>
</profile>
<profile>
<id>fast</id>
<properties>
<testCategories>com.yourcompany.FastTests</testCategories>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>${testCategories}</groups>
</configuration>
</plugin>
</plugins>
</build>
... can be used to run:
mvn install -P slow: runs the slow tests only
mvn install -P fast: runs the fast tests only
mvn install -P fast,slow: runs the fast and slow tests
Update 1: for this question: "Is there a way to use this approach so I can run all fast tests by default?"
You can define two properties:
<properties>
<includedCategories></includedCategories>
<excludedCategories>com.yourcompany.SlowTests</excludedCategories>
</properties>
Then update your surefire plugin definition like so:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>${includedCategories}</groups>
<excludedGroups>${excludedCategories}</excludedGroups>
</configuration>
</plugin>
And, finally, add this profile:
<profile>
<id>slow</id>
<properties>
<includedCategories>com.yourcompany.SlowTests</includedCategories>
<excludedCategories></excludedCategories>
</properties>
</profile>
This just toggles the includedCategories and excludedCategories properties. By default, you include everything except those tests annotated with com.yourcompany.SlowTests (i.e. everything except your 'slow' tests). When you run with -P slow you exclude everything except those tests annotated with com.yourcompany.SlowTests (i.e. everything except your 'slow' tests).
Note: what I said in my original answer about Surefire versions < 2.13 misbehaving with Categories still stands so to make this work you need to be using a version of the Maven Surefire plugin >= 2.13.
Hi I have a problem with the encoding of my project.
When I run JUnit tests from eclipse, there are no failures. The problem is when I do maven > clean maven > install, one of the tests fails.
I have this string: "ADMINISTRACIÓN", and it's fine when i run the JUnit from eclipse, but I've printed the variable and when maven does the tests, the value of this string is: "ADMINISTRACI�N".
I've changed every property I could find of encoding in eclipse to UTF-8.
-Configured the pom this way:
(...)
<project>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
(...)
</properties>
</project>
(...)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
(...)
But the output is the same. I have a coworker that has the same project than me, and the same eclipse client and config, and her maven tests print accents with no trouble.
Any further ideas?
Thanks a lot!
Try run your build with:
mvn -DargLine=-Dfile.encoding=UTF-8 clean insall
if help, you can configure surefire in project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
</configuration>
</plugin>
Problem may occur because System.out use default system encoding, you can change this be setting file.encoding java property.
I tried all settings mentioned in this post to build my project successfully however that didn't work for me. At last I was able to build my project successfully with mvn -DargLine=-Dfile.encoding=UTF-8 clean insall command.
Something like the following.
I would like a way to skip my dao tests in surefire. Trying to avoid overhead of defining Suites.
With CI I'd like to have one nightly that runs all tests and another 5 minute poll of SCM that runs only 'fast' tests.
mvn -DskipPattern=**.dao.** test
Let me extend Sean's answer. This is what you set in pom.xml:
<properties>
<exclude.tests>nothing-to-exclude</exclude.tests>
</properties>
<profiles>
<profile>
<id>fast</id>
<properties>
<exclude.tests>**/*Dao*.java</exclude.tests>
</properties>
</profile>
</profiles>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.tests}</exclude>
</excludes>
</configuration>
</plugin>
Then in CI you start them like this:
mvn -Pfast test
That's it.
Sure, no problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<!-- classes that include the name Dao -->
<exclude>**/*Dao*.java</exclude>
<!-- classes in a package whose last segment is named dao -->
<exclude>**/dao/*.java</exclude>
</excludes>
</configuration>
</plugin>
Reference:
Maven Surefire Plugin > Inclusions and Exclusions of Tests
(The excludes can not be configured via command line, so if you want to turn this behavior on conditionally, you will have to define a profile and activate that on the command line)
It is possible to exclude tests using the commandline; using ! to exclude.
Note: I'm not sure but possibly needs 2.19.1 or later version of surefire to work.
Examples:
This will not run TestHCatLoaderEncryption
mvn install '-Dtest=!TestHCatLoaderEncryption'
Exclude a package:
mvn install '-Dtest=!org.apache.hadoop.**'
This can be combined with positive filters as well. The following will run 0 tests:
mvn install '-Dtest=Test*CatLoaderEncryption,!TestHCatLoaderEncryption'
See the Maven Surefire docs.
I am using eclipse as IDE. When I right click on the project and then click maven update my java version change to 1.5. Here is what I did so far, I followed all the steps listed here
http://qussay.com/2013/09/13/solving-dynamic-web-module-3-0-requires-java-1-6-or-newer-in-maven-projects/
I changed "Java build path" to "workspace default jre 1.8.0_25"
Then changed "java compiler" to 1.8
Then changed "project facets">java>1.8
Changed pom.xml java version to 1.8
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.1.3.v20140225</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
After all this when I click on "Maven update" my java version change to 1.5 automatically. Also in above steps, first two step's version also change to 1.5 automatically. How can I fix this?
Open your pom.xml file and add the following lines on it:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Where 1.8 is the Java version of your current JDK/JRE. Another way of doing this is adding a <build> with the maven-compile-plugin as:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version> <!-- or whatever current version -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
If you are looking for a way to make it work with Java versions 9+ please take a look at #JDelorean's answer.
Had the same issue when I installed Java 9. My project would default to J2SE-1.5 Execution Environment. Strangely, Java 9 compliance level is not referenced like previous versions, i.e. "1.8", but as "9". So I had to provide my properties and Maven compiler plugin config accordingly:
<properties>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
and
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
This seems to have solved the problem. Works for versions 9 and above.
The root-cause of this issue is that if for any reason Eclipse's cannot resolve a valid value for the maven.compiler.source property when generating/updating the .classpath file from the pom, it will simply default to using org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5.
As expertly answered by #jorge-campos, there are multiple ways to set that property.
However, Jorge's answer didn't appear to work for me. Here were my settings:
<properties>
<javaVersion>1.8</javaVersion>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
...
Exactly. ${java.version} is never going to resolve to the (completely different) property javaVersion and Eclipse ignored the property and used the default.
Which brings me back to the "for any reason" part I opened with; developer stupidity can be one of those reasons.
Add this lines to your pom.xml, then right click your JRE System Library -> Properties -> Set your correct execution environment to Java 1.8 or version you want to set.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version> <!-- or whatever current version -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
I encounter similar issue on one of my team mate machine. He was using old version of Eclipse, I believe it he was using Keppler. Project after being updated change JRE version to 1.5.
Simple updating Eclipse to latest version solve this problem.
In my case (old JBoss Developer Studio), the issue was the JRE environments did not include 1.8 (only 1.7). When I switched the maven-compiler-plugin version to 1.7 and did maven update project, it updated the Eclipse JRE system library to 1.7.
So the solution is to either get a newer IDE version that includes a built-in JRE environment that is 1.8 or later, or try to install it manually (see https://stackoverflow.com/a/35204314)
I had this problem. In my case the <properties> tag & nested tags Jorge Campos mentions above were in the wrong place. If I put them between the <hostversion> and <dependencies> tags in the pom.xml file, then this behaviour stopped.
That can be picked up in Eclipse if validation of these files is switched on.
I am using Java 11.
This is how the complete pom.xml file looks like after adding
<properties> and <plugin>
<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>com.akshay</groupId>
<artifactId>1000SpringSecurityEg</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>1000SpringSecurityEg Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<javaVersion>11</javaVersion>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<build>
<finalName>1000SpringSecurityEg</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The above code worked for me.
Hope it works for you as well.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
I added these lines in my "pom.xml" file and it worked.
I allow myself to update that subject with Java 11.
I have installed OpenJDK11 on my computer, and I wanted to use it in an app.
I had trouble because Eclipse would always change my JRE to JavaSE-1.5 when I updated my project with Maven.
I had set everything as you said, but I was always directly selecting in my Java Build Path "java-11-openjdk.x86_64" as one of my Alternante JRE. I fixed my problem by selecting in "Execution environment" JavaSE-10 (but you have to double click on it and then choose as a compatible JRE your OpenJDK11 version) as shown on the picture.
Execution environment setup
The project will use Java 11 thanks to that (picture) but you have to write 10 for the java-version in the pom.xml and also set java 10 on the Project Facets.
I've resolved the issue installing the eclipse update "JAVA 12" from the market.
It makes my eclipse pass from Kepler to Luna.
After that, i have been able to set 1.8 as standard JDK, fixing the "maven update" problem.
I experienced with JRE 15.0.1 one must ONLY specify the compiler plugin like
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>15</source>
<target>15</target>
</configuration>
</plugin>
If I also provide the properties like
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
this will again reset to JRE 1.5 on Maven / Update Project
!!!
Check in pom.xml under properties if there is any tag with this maven.enforcer.plugin.version. Delete it and replace that with the below code
<javaVersion>1.8</javaVersion>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
under properties tag.
And under build, replace the plugins with the below code:
<build>
<finalName>1000SpringSecurityEg</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
This solution resolved my issue.
I changed Eclipse from kepler to neon and then updated my project by with Maven -> Update Project.
I don't want to be dependable on a external environment variable to force maven to build my classes with UTF-8. On Mac, I was getting all sorts of problems when building with maven. Only the option below solved the problem:
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
mvn clean install
However I am distributing my project and it does NOT make sense to rely on the user to set this environment variable to build the project correctly.
Tried everything as described here: enabling UTF-8 encoding for clojure source files
Anyone has a light on that awesome Maven issue?
#Joop Eggen gave the right answer here: https://stackoverflow.com/a/10367745/962872
It is not enough to define that property. You MUST pass it inside the appropriate plugins. It won't go by magic inside there.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>...</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>...</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Yes there is, define
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I was running into this problem, but only when running the compile from Emacs. I could not change the project's poms. What worked for me was to put the following in ~/.mavenrc
LANG=en_US.UTF-8