Create Jacoco report for external repository - java

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

Related

Skip one maven test does not work

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.

What does "[ERROR] file doesn't exist" means when doing maven install?

When I execute maven install in my gitlab build, I get the following error message:
12338 [INFO] Installing com.mycompany:myartifact:1.0.0-SNAPSHOT at end
12338 [INFO] 12338 [INFO] --- maven-bundle-plugin:3.3.0:install
(default-install) # myartifact --- 12411 [ERROR] file doesn't exist:
file:/root/.m2/repository/com/mycompany/myartifact/1.0.0-SNAPSHOT/myartifact-1.0.0-SNAPSHOT.jar
I don't understand what does it mean. The file obviously doesn't exist because it was never installed in that environment, so it's strange that maven would complain about it.
I suppose this error hides some other error, like no write rights? But I see not other error in log. The build seems to be successful.
Try to make a clean install, maybe it's just a local repository issue
mvn clean install
When you do maven install, It usually looks into your .m2 folder and searches for the existence of the dependencies(mentioned in pom.xml). If it doesn't exist, maven will automatically download it. So the build will be successful.

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.

Maven error: "You don't have a SNAPSHOT project in the reactor projects list."

What on earth does this mean? Cant find any help via google.
> mvn release:prepare
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Base 1.0.5
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-release-plugin:2.3.2:prepare (default-cli) # base ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.386s
[INFO] Finished at: Tue Oct 08 08:22:46 EST 2013
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli) on project base: You don't have a SNAPSHOT project in the reactor projects list. -> [Help 1]
release:prepare command is supposed to prepare your snapshot project for the release. It sounds like you don't have such a snapshot project.
Here's the full details what it'll do: http://maven.apache.org/maven-release/maven-release-plugin/examples/prepare-release.html
If you're sure you should be releasing, you should be working on a maven module that has version ending with -SNAPSHOT.
Update: like noted by #khmarbaise in the comments, if your release has failed, you should do release:rollback to go back to previous state. Note though that it is not supported if you release through jenkins (jenkins issue), and it won't rollback the tags.
Don't needed manually edit pom.xml.
You can use "mvn versions:set" for batch update, something like this:
mvn versions:set -DnewVersion=1.0.3-SNAPSHOT
I've had the same error with Jenkins. In a previous release, Jenkins updated the version of the POM to a non-snapshot version, but the build failed before Jenkins could set the version to a -SNAPSHOT version again. Afterwards, making a release resulted in the error described above.
Fixing this is easy: just manually change the version of your app in pom.xml to a -SNAPSHOT version.
I know this is an old question but I had this issue recently and I found 2 solutions that others may find useful. I am using bamboo as my CI tool. The issue was that there was an error in the bamboo build leaving bamboo in an incorrect state. It had locally updated my project pom.xml with the new release version but had not checked this into SVN. The two solution that worked for me were:
Either
Delete the bamboo build-dir directory for the project and run the release again: rm -rf /opt/bamboo-home/xml-data/build-dir/PROJECT_NAME-RELEASE-JOB1
OR
Run the maven release from the command line using the following commands:
mvn release:prepare -DignoreSnapshots -Dresume=false
mvn release:perform
No need to update versions manually as that is time consuming, if version change is all you need, there is a different command that only updates the pom versions, just like updating them manually:
mvn versions:set -DgenerateBackupPoms=false -DnewVersion=1.0.XX-SNAPSHOT

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