Maven: Excluding tests from build - java

I have some classes I'm using as tests in my src/test/java folder of my project. When I run maven using the standard maven compile plugin. Those items are compiled into .class files and are included in the jar where the compiled code is packaged.
I've created these tests for myself to run within eclipse, prior to running maven and building my release. They are just sanity tests and should not be included in the build. I'd rather not put them in a seperate project, because, to me, they make sense here. How can I tell maven that I do not want it to compile/include the files in that directory?
I beleive the maven compiler plugin is generating the jar as follows:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

I understand from your comment on this answer that the "tests" aren't unit tests, but just ordinary classes that you want excluded from the final artifact? As such, your best option is to make use of the <exclude> tag with the maven-jar-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/yoursortoftestpackage/YourSortOfTestClass*</exclude>
</excludes>
</configuration>
</plugin>
Hope that helps!
Cheers,

Use the option of -Dmaven.test.skip will skip both compilation and execution of the tests. ( use -DskipTests just skips the test execution, the tests are still compiled)
The reference link
mvn clean install -Dmaven.test.skip

Annotation for junit #Ignore will ignore the class while building in maven.
Edit:
You can configure maven-surefire-plugin.
<project>
<build>
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>

Put your custom test files in a folder src/localtest/java i think, maven will not know it is there.

Related

How to compile a Maven JAR file with JVM args use a plugin?

I'm looking for a Maven plugin that could execute by itself the good JVM arguments.
For now, I've tried using the maven-compiler-plugin and the maven-surefire-plugin (the pom of my project is down below).
the type of argument I want the jvm to execute is this one:
--module-path="C:\Program Files\Java\javafx-sdk-11\lib" --add-modules=javafx.controls
in order to launch a javafx app.
the pom.xml with the plugin tests.
<build>
<plugins>
//tests with the surefire plugin (without tests).
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skipTests>true</skipTests>
<argLine>--module-path="C:\Program Files\Java\javafx-sdk-11\lib" --add-modules=javafx.controls</argLine>
</configuration>
</plugin>
</plugins>
</build>

how to skip integration tests with maven release plugin

I want to skip integration tests while running maven release plugin with command
mvn -B -DskipITs release:prepare release:perform
It does not seem to work this way. The same option -DskipITs works for mvn install/deploy. I don't want to use -Dmaven.test.skip=true since only integration tests need to be ignored, not unit tests. What is the best way to accomplish this?
EDIT:
-Darguments=-DskipITs works for release:prepare, but surprising it does NOT work for release:perform. Tried -Darguments=-Dmaven.test.skip=true, does not work either.
Tried to add <arguments>skipITs</arguments> for release plugin in the pom, but it would ignore all other -Darguments provided in command line. I can't have everything configured in plugin config, since some of the options takes environment variables on the fly.
According to how to make maven release plugin skip tests, it seems you need both -DskipITs and -Darguments=-DskipITs. One is to skip compiling ITs the other is for skipping running ITs.
Use Maven Profiles.
Add the following to your pom:
<profiles>
<profile>
<id>ItsReleaseTime</id>
<build>
<plugins>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
And invoke the command :
mvn -B -P ItsReleaseTime release:prepare release:perform
You can add some setting in your pm.xml file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

how can measure on test code using javancss maven plugin

I’m using javancss maven plugin for measuring lines of codes and cyclomatic complexity.
I can not find the way of configuring the plugin in order to analyze test code as well.
This is a fragment of pom.xml file:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javancss-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<includes>
<include>**/Exam.java</include>
<include>**/Topic.java</include>
<include>**/TopicSequenceTest.java</include>
<include>**/TestWithMockObjects.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</reporting>
This is the project structure:
src
main
...
test
...
This is the way I use it:
mvn javadoc:javadoc javancss:report jxr:jxr jxr:test-jxr
I think it doesn’t work because ** is representing src/main, then it can not include any java file under src/test
But I cannot find the solution.
Thank you in advance

Execute TestNG.xml from Jenkins (Maven Project)

My Project dosnt have a bin folder
Have tried the following:
D:>java -cp "Pathtolibfolder\lib*;Pathtobinfolder\bin"
org.testng.TestNG testng.xml
Any Ideas?
If you are using Maven then i would simply use the surefire plugin and run tests as part of the build:
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>RELEASE</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
...
</plugins>
Just specify the path to your testng.xml and take advantage of it if you can use this kind of configuration.
It allows for tons of parametrization and i have used it extensively in my projects.
Check out this tutorial to get a hang of it: http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

how to exclude java sources from a third party jar?

I have one jar dependency in my java project that contains sources as well and when I run mvn compile, these java sources appear as class files in my compiled maven output :(...
How can I exclude these files.. (I only want my own compiled files in the compiled output)
I tried something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1-SNAPSHOT</version>
<configuration>
<excludes>
<exclude>**/bv/**/*.java</exclude>
</excludes>
</configuration>
</plugin>
Played with it but they keep appearing in my maven compiled output :( ..
Any idea's ?
My understanding is that this is a normal behavior of javac that searches the whole classpath for source files to compile unless the -sourcepath option is given (and this would be the solution here).
Unfortunately, there is a Jira issue about -sourcepath not being passed to javac by the Maven Compiler Plugin (see MCOMPILER-98) but there is a workaround. So, could you please try this:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<sourcepath>${project.basedir}/src/main/java</sourcepath>
</compilerArguments>
</configuration>
</plugin>
Would the provided scope work?
From: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html:
This is much like compile, but
indicates you expect the JDK or a
container to provide the dependency at
runtime.
You can pass the -implicit:none parameter to the compiler
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-implicit:none</compilerArgument>
</configuration>
</plugin>

Categories

Resources