Skip one maven test does not work - java

I try to skip one test with my maven command (doc: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/):
mvn -B clean install -T2C -pl sub-tests -Dtest=!com.xx.xx.test.xx.xx.canMakeCall*
But I get some error (it seems it tries to skip all tests? I don't know):
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) # sub-tests ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project sub-tests: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]
What am I doing wrong?

According to Maven Surefire Plugin - Running a Single Test :
Running a Set of Methods in a Single Test Class
As of Surefire 2.7.3, you can also run only a subset of the tests in a
test class.
NOTE : This feature is supported only for Junit 4.x and TestNG. Use syntax e.g. "foo/MyTest.java", "/MyTest.java", "MyTest" for "test"
parameter (see includes/excludes).**
You should use the following syntax:
mvn -Dtest=TestCircle#mytest test
So I think your command should be:
mvn -B clean install -T2C -pl sub-tests -Dtest=!com.xx.xx.test.xx.xx#canMakeCall*
It will ignore all tests methods starting with canMakeCall of test com.xx.xx.test.xx.xx.

Related

mvn -pl <name> -am command

I have the following command, which I'm using to build a specific microservice (<ms-name>):
./mvnw -DskipTests clean install -pl <ms-name> -am
clean, instal, and -DskipTest are clear. But I don't understand what -pl and -am are doing.
logs:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] <utils-1> [jar]
[INFO] <utils-2> [jar]
[INFO] <ms-name> [jar]
and after it starts executing clean install on , , and in the end <ms-name>
I have checked the pom.xml but could not see the relations somewhere.
My question is: What are -pl and -am and where I can find this 'order' building relation between <ms-name>, <utils-1>, and <utils-2>?
From documentation,
-pl is,
Comma-delimited list of specified reactor projects to build instead of
all projects. A project can be specified by [groupId]:artifactId or
by its relative path
-am is,
If project list is specified, also build projects required by the list
where I can find this 'order' building relation ..?
It is handled by reactor (and internal program within maven) which collects all the available modules to build, sorts the projects into the correct build order and builds the selected projects in order.
To get understand more about maven reactor, here is a StackOverflow answer which is more detailed enough.
What is the "reactor" in Maven?

Create Jacoco report for external repository

I have a Spring-Boot maven repository. I can't alter pom.xml to add jacoco-maven-plugin. But I need a test coverage report for this repository.
So my idea was to execute the following command:
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent org.jacoco:jacoco-maven-plugin:report
But in the logs I see:
[INFO] --- jacoco-maven-plugin:0.8.6:prepare-agent (default-cli) # backend ---
[INFO] argLine set to -javaagent:C:\\Users\\admin\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.6\\org.jacoco.agent-0.8.6-runtime.jar=destfile=C:\\projects\\my-project\\backend\\target\\jacoco.exec
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.6:report (default-cli) # backend ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
and no report is generated.
What should I do to add "missing execution data file"? And is it even possible to generate Jacoco report without adding anything to the pom.xml?
I figured this thing out. Turns out I needed to change the goal's order a bit.
Instead of:
mvn clean verify org.jacoco:jacoco-maven-plugin:prepare-agent org.jacoco:jacoco-maven-plugin:report
Clean.
Verify.
Prepare Jacoco Agent.
Build report.
I need to do:
Clean
Prepare Jacoco Agent
Verify
Build report.
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent verify org.jacoco:jacoco-maven-plugin:report

Maven Jenkins job skips all modules if one fails to build

In Jenkins, I have a Maven project with the following structure:
x proftaakmaven
- AutosimulatieSysteem
- LandenMonitoringSysteem
- PolitieSysteem
x Verplaatsingssysteem
- VerplaatsingREST
- VerplaatsingSOAP
- VerplaatsingCommon
- VerplaatsingenRabbitMQ
- RabbitMQ-Proof-of-Concept
- VerplaatsingenRabbitMQTestClient
The Maven reactor constructs this building order:
[INFO] Reactor Build Order:
[INFO]
[INFO] AutoSimulatie
[INFO] LandenMonitorSysteem
[INFO] PolitieSysteem
[INFO] VerplaatsingenSysteem
[INFO] VerplaatsingenCommon
[INFO] VerplaatsingenREST
[INFO] VerplaatsingenSOAP
[INFO] RabbitMQ-Proof-of-Concept
[INFO] VerplaatsingenRabbitMQ
[INFO] VerplaatsingenRabbitMQTestClient
[INFO] proftaakmaven
However, due to a current failure in 'LandenMonitorSysteem' source code, Maven fails on building the other modules as well. This makes the Jenkins job fail.
I have tried running Maven with --fail-never and --fail-at-end. But neither seem to have any effect.
How would I be able to continue building all the modules, even if one fails?
Thanks.
--fail-at-end should be the thing to use.
If that doesn't work you could use -pl to specify the list of working projects.
If you use the -am flag as well you can specify the target you are interested in building and Maven will calculate the dependency tree for you.
I.E. mvn clean install -pl VerplaatsingenRabbitMQTestClient -am
I have found the solution. The problem was the way that I provided the argument. In Jenkins 2.0, the job should be configured like this :
The settings inside the job

How are maven plugin aliases mapped

I am trying to understand what mvn clean:clean actually does.
mvn -B help:describe -Dcmd=clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-one 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:describe (default-cli) # sample-one ---
[INFO] 'clean' is a lifecycle with the following phases:
* pre-clean: Not defined
* clean: org.apache.maven.plugins:maven-clean-plugin:2.5:clean
* post-clean: Not defined
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.689 s
[INFO] Finished at: 2015-12-10T10:20:16-08:00
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------
It appears to me that mvn clean:clean is same as doing mvn org.apache.maven.plugins:maven-clean-plugin:2.5:clean. Therefore I am assuming the first clean in mvn clean:clean is just an alias for org.apache.maven.plugins:maven-clean-plugin:2.5. Similarly mvn maven-surefire-plugin:2.12.4:test is same as mvn surefire:test.
So somehow, maven-surefire-plugin:2.12.4 seems to refer to surefire and org.apache.maven.plugins:maven-clean-plugin:2.5 to clean.
When I look at the effective-pom, I see the following
maven-surefire-plugin
2.12.4
default-test
test
test
maven-clean-plugin
2.5
default-clean
clean
clean
As you can see, the pom doesnt seem to define alias. So following are my questions
Is my understanding about plugin aliases correct
If my understanding about aliases is correct - a) how and where are they defined? b) Is there a way to list all aliases.
From official Maven documentation about plugins development:
Shortening the Command Line
There are several ways to reduce the amount of required typing:
If you need to run the latest version of a plugin installed in your local repository, you can omit its version number. So just use mvn sample.plugin:hello-maven-plugin:sayhi to run your plugin.
You can assign a shortened prefix to your plugin, such as mvn hello:sayhi. This is done automatically if you follow the convention of using ${prefix}-maven-plugin (or maven-${prefix}-plugin if the plugin is part of the Apache Maven project). You may also assign one through additional configuration - for more information see Introduction to Plugin Prefix Mapping.
Finally, you can also add your plugin's groupId to the list of groupIds searched by default. To do this, you need to add the following to your ${user.home}/.m2/settings.xml file:
<pluginGroups>
<pluginGroup>sample.plugin</pluginGroup>
</pluginGroups>
At this point, you can run the mojo with mvn hello:sayhi.
So, alias are not defined in the pom file but part of built-in mechanism of maven. Further details are also provided in the official documentation about Plugin Prefix Resolution.

Run JUnit tests in Maven without building and copying files [duplicate]

This question already has answers here:
How can I atomically run mvn tests (without rebuilding source code)?
(3 answers)
Closed 6 years ago.
I have a large Maven project which has several modules in it. When I want to run a JUnit test from one module I run 'mvn -Dtest=NameOfTest test' in the directory that contains all the modules. When I run this command Maven goes through each module and tries to compile it (though it's already compiled), which involves copying a bunch of files and adds to the total time of the test. It seems that the 'test' command for the Maven surefire plugin executes all the steps up to the test. I was wondering if there is a way to execute only the test step and not bother with all the attempted compilation and copying of files.
Here is some output from before the test starts:
[INFO]
[INFO] --- build-helper-maven-plugin:1.5:add-test-source (add-test-source) # module1 ---
[INFO] Test Source directory: <directory in module1 with some generated sources> added.
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) # module1 ---
[debug] execute contextualize
[INFO] Copying 108 resources
[INFO] Copying 1113 resources
[INFO] Copying 1 resource
[INFO]
It repeats this for each of the other modules. All told it takes a minute or two before it actually starts the test. Does anyone know a way to get the test to run without bothering with all the compilation beforehand? Please let me know if there's any more information I should provide.
If what you would like to do is to run just the test phase of the lifecycle without running all of the previous phases you could also call the goal that is bound to the test phase:
mvn surefire:test
or if you want to run just one test
mvn -Dtest=NameOfTest surefire:test
What is wrong with simply running the test from within the module the test resides in? That way, Maven will not attempt to build the other modules which you are not interested in.

Categories