maven exclude tests with surefire excludeTests option - java

I have a large project with sub-projects and want to use excludesFile to exclude test failures, and ignore them until the tests are fixed.
I believe there might be a way of achieving this with surefire plugin excludesFile option here.
I am very new to maven and would like get some examples or pointers on how this can be achieved.

To use excludesFile option,
Create a file containing file name patterns of your test files that you want to exclude.Example:Create a file at src/main/resources/exclude.txt with below 2 lines.
**/*1.java
**/*2.java
Add following in your maven-surefile-plugin configuration.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<excludesFile>src/test/resources/exclude.txt</excludesFile>
</configuration>
</plugin>
</plugins>
You are done. All test files ending with 1.java and 2.java will be excluded.
There is other way also to exclude any tests from execution by using configuration in maven-surefire-plugin.
Example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
More information can be found here.

Related

Running Testng.xml from POM.xml is not running tests in sequence

I am running testng.xml file using POM.xml by adding compiler and surefire plugins. It runs test but the sequence of tests is not as expected.
I have 10 classes mentioned in testng.xml and it runs in that sequence when i run through testng.xml. But when running through POM.xml the sequence goes like; first it runs all the 0 priority tests mentioned in all classes, then 1 priority tests and so on. It should run tests according to the classes sequence mentioned in testng.xml.
Any quick help will be much appreciated.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${jdk.level}</source>
<target>${jdk.level}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
under testng dependency the scope tag needed to be changed to compile from test and it resolved the issue as it needed to compile the build after adding plugins.
It resolved the issue.

why maven-compiler-plugin exclude disable

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/Application.java</exclude>
</excludes>
</configuration>
</plugin>
this is my pom.xml , maven-compiler-plugin.version is 3.8.1 .
but i see the Applciation.class still in my jar package by maven
You are looking at the wrong location. From what I see in the screenshot, you've found some Application file from the External Libraries. What the maven-compiler-plugin does is to generate the target folder. That's where the class file should be excluded from. Check the existence of the file class under:
target/classes/...
And don't forget to run mvn clean install before (with emphasis on clean - this will wipe out your target folder)
In a project, I had to do a similar thing, due I need to exclude the module-info.java. I resolved using this configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<excludes>
<exclude>**/module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
If you want the entire project you can get it from GitHub. I hope this helps.

maven: Some input files use unchecked or unsafe operations [duplicate]

In NetBeans 7.2, I'm having trouble finding how to compile using -Xlint:unchecked in a Maven project. Under an Ant project, you can change compiler flags by going to Project Properties -> Compiling, but Maven projects don't seem to have any such option.
Is there any way to configure the IDE to compile with such flags using Maven?
I guess you can set compiler arguments in your pom.xml. Please refer this http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html
<compilerArgument>-Xlint:unchecked</compilerArgument>
I want to elaborate on #Nishant's answer. The compilerArgument tag needs to go inside plugin/configuration tag. Here is a full example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
</plugins>
This works for me...
<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>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
<compilerArgs>
<arg>-Xlint:unchecked</arg> <-------this right here ---->
</compilerArgs>
</configuration>
</plugin>
</plugins>
The pom file information is spot on. I had the additional challenge of building someone else's Maven project in Jenkins and not having access to the pom file repository.
I created a pre-build step to insert the compiler parameter into the pom file after downloading it from git, for example
sed -i 's|/target> *$|/target>\n<compilerArgument>\n-Xlint:deprecation\n</compilerArgument>|' $WORKSPACE/pom.xml

Is there a way to specify in Maven which JUnit tests run by package

I'm wondering if there is a way to specify in Maven which collection of JUnit tests run based on package names. An 'exclude' would be ideal, since I have fewer that I need not run than I have those that need to run.
Yes you can configure surefire plugin to include certain tests only
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<includes>
<include>com/abc/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

How to exclude java classes from being compiled in maven with annotation

I already have a working solution where I can specify with maven which classes to not compile when using a particular maven profile.
But I would like to use a general solution and use an annotation instead
The current solution that I have is like
<plugin>
<!-- Exclude some web services used only for internal testing -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<optimize>true</optimize>
<excludes>
<exclude>**/something/*ClassPattern.java</exclude>
</excludes>
<testExcludes>
<exclude>**/something/*ClassPatternTest.java</exclude>
</testExcludes>
</configuration>
</plugin>
But Some thing like
#NotCompiledForProduction
would be rather nice on top of a class.
It seems to me that this might be hard (or impossible to do) without changing maven's behaviour. That is not the scope here. And this kind of annotation
You cannot (I assume) use an annotation to determine what source code gets presented to the java compiler, because you need to compile the source code in the first place to process the annotation.
It seems like you need to create different modules in your maven project: one that generates a jar file with the production code, and one module that generates a jar file with testing implementation with a dependency on the production artifact.
If the code really does need to be in the same maven module, then the code should always be compiled. You can however use maven-jar-plugin to create multiple artifacts at the package phase: the default artifactId.jar, and an artifactId-test-lib.jar artifact. You can do this by specifying multiple executions for the plugin, and using <includes> and <excludes> to split the jar files as required.
you can try this...
<build> <plugins>
<!-- Run annotation processors on src/main/java sources -->
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<!-- Disable annotation processors during normal compilation -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
</plugins> </build>

Categories