Maven test for only provided package - java

I have a multi-module maven project. I want to test only one package but that package may depend on other packages.
For better explanation, I created an example GitHub repository:
GitHub Example Repository
Running mvn -pl module test can cause problem because of dependencies of module. Instead of that, I must run mvn -pl module -am test but this command run test for my module and all dependents modules (in my example, run test for dependent-module-1 and dependent-module-2).
Performance is crucial for me. I don't want to run test for dependents package when I want to run tests for only one module. So I decide to convert my command to:
mvn clean
mvn -pl module -am compile test-compile (or mvn -pl module -am package)
mvn -pl module test
It doesn't works and give errors because of not provided artifact.
I search a lot and I don't find anything. Please consider that I don't want to use mvn install at all. I don't want to move it to my repository.
Is it possible to help me? I don't know what to do.
Thanks.

Related

Maven enforcer issue when running from reactor level

Maven version used: 3.5.2, 3.5.3
mvn clean package -pl <root-artifact-id>:<module-name>
is failing saying
[WARNING] Rule 3: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
Module parents have been found which could not be found in the reactor.
module: <artifact:id>:<module-name>:war:1.0-SNAPSHOT
But working fine when running the mvn clean package from the module level though. Thats the only warning message in the trace causing the enforcer to fail the package build.
It's a very old reported bug but nobody seems to do anything about it: https://issues.apache.org/jira/browse/MENFORCER-189
Root cause would be that it compares the artifactid (module-name) of the project passed in the -pl paramater with the artifactid (reactor) of its parent. Which would never be the same and thus will always give this error.
For us the fix was to disable the enforcer plugin when using this execution (other executions without the -pl like 'clean install' are fine)
mvn clean install
mvn package -pl module-name -Denforcer.skip=true
Edit:
Another option is to specify the reactor project in the build using '.' (note: this will also package the reactor)
mnv clean package -pl .,module-name
Try including --also-make or -am, for example:
mvn -am -pl <root-artifact-id>:<module-name> clean package
Even if the module you're building doesn't have a dependency on another module within the build, this triggers a Reactor build that includes the given module and the parent POM together, and their relationship is then able to be verified by Enforcer without skipping. (Works with Maven 3.6.2 in my case).

Running maven without pom (but with text input on CLI) [duplicate]

I have multiple questions.
Can I specify the pom.xml in mvn command?
Can I mix the goals of another project while executing mvn command on current project ?
Eg: mvn clean-otherproject comple-otherproject instal-otherproject compile-thisproject
I can do this with multiple mvn commands, but Can I do this in single maven command.
Just mvn --help would have answered the first question:
mvn -f otherPomFile.xml
No. You can simple execute the phases for the current project you are in. You can give multiple phases like
mvn clean install site site:deploy
For the first question, see khmarbaise's answer
If you want to build more than one maven project in one step, you must use modules.
In a multi-module project, if you call mvn install from the top project, all sub modules are built, unless you use the advanced reactor options (e.g. mvn install -pl util -am only builds the module 'util' and it's dependencies)

Is there any Maven cmd that can only build test without the whole project?

I have a big server project, and some related UnitTest file using maven.
When I added the new UnitTest for some java file, I only want to build the current UnitTest and run it, I don't want to build the whole server project from the beginner.
But when I using the:
mvn test -Dtest=NewUnitTest
I still need to wait for a long time to build the whole server project.
Is there a way to only build the new added UnitTest file?
Thanks.
Try the following one:
mvn compiler:testCompile surefire:test -Dtest=NewUnitTest
compiler:testCompile - will compile your tests (if you need it)
surefire:test - will execute your tests
how are you doing this?
If you run a clean and then compile it will delete everything and then compile.
If you simply run mvn test-compile then it should just compile anything that is outdated including any test code
I am assuming that you have multi-module build. Just run (from the root):
mvn clean test -Dtest=NewUnitTest -pl :<the artifactId of the project you want to test>
Obviously, if the project you are trying to test depends on other artifacts in your reactor, you may need to build these too if they can't be fetched from a centralized repository. Just add the "also-make" flag (-am):
mvn clean test -Dtest=NewUnitTest -pl :<the artifactId of the project you want to test> -am

difference between mvn clean and install commands

I am using maven for the build purpose and normally we use the maven command mvn clean -Dmaven.test.skip=true package only to build the web application. I know we can use the mvn install command also to build a web application. But can anyone provide me with the exact difference between these two commands?
I found some notes on the clean and install commands. But i just want to know what's the advantage of using mvn clean command instead of using install command.
The main different between mvn clean -Dmaven.test.skip=true package and mvn install is that the first command line cleans the target directory and packages without running the tests. The second one compiles, tests, packages and installs the JAR or WAR file into the local repository at ~/.m2/repository.
Maven has this concept of Maven Phases. Please go through the Maven Phases of this doc. So when you run a phase (say maven phase x) all the phases up to that phase is executed (that is phase 1 to phase x).
You need mvn clean to clean up artifacts created by prior builds. mvn package will package your code into your specified format in your POM. mvn install will also install the package made by Maven into the local repository.
Also note that clean and site are not part of phases of the default life-cycle. You have to fire it before your package or install command. Needless to say ordering does matter here.
As explained here.
clean is its own action in Maven. mvn clean install tell Maven to do the clean action in each module before running the install action for each module.
What this does is clear any compiled files you have, making sure that you're really compiling each module from scratch.

maven dependency for multi-module project

I have a strange issue with maven.
I'm running a dropwizard project that has multiple modules.
Project
-> ServiceModule1
-> ServiceModule2
-> ModelsModule
-> TestModule
All modules depend on the test module, and all of the service modules depend on the models module.
I'm using a test-jar to distribute the test module since all of the fixtures live in there.
So when I package my project, I do this:
cd testModule
mvn jar:test-jar
cd ..
mvn package
This works fine, except it means every time I want to package my project I have to run all the tests. If I switch to
mvn package -Dmaven.test.skip=true
I get a failure because my modules start to look for their dependency jars in maven central.
This is really frustrating, since the tests depend in a database and I don't want to install a database on every web server.
What should the "correct" setup be?
Just to be clear, if your ServiceModule1 wants to depend on ModelsModule, you can build it in two steps,
$ cd ModelsModule
$ mvn clean install
$ cd ..
$ cd ServiceModule1
$ mvn clean package
Please note install in step2,
In other words, it is not mandatory that all your lib should be in maven central. But it is mandatory that all of them should be in your local repo. install is a goal which can install a lib to your local repo.
Now this link will show you how to install a test jar to your local repo.

Categories