maven dependency for multi-module project - java

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.

Related

Maven test for only provided package

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.

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)

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.

How are "mvn clean package" and "mvn clean install" different?

What exactly are the differences between mvn clean package and mvn clean install? When I run both of these commands, they both seem to do the same thing.
Well, both will clean. That means they'll remove the target folder. The real question is what's the difference between package and install?
package will compile your code and also package it. For example, if your pom says the project is a jar, it will create a jar for you when you package it and put it somewhere in the target directory (by default).
install will compile and package, but it will also put the package in your local repository. This will make it so other projects can refer to it and grab it from your local repository.
Documentation
What clean does (common in both the commands) - removes all files generated by the previous build
Coming to the difference between the commands package and install, you first need to understand the lifecycle of a maven project
These are the default life cycle phases in maven
validate - validate the project is correct and all necessary information is available
compile - compile the source code of the project
test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
package - take the compiled code and package it in its distributable format, such as a JAR.
verify - run any checks on results of integration tests to ensure quality criteria are met
install - install the package into the local repository, for use as a dependency in other projects locally
deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
How Maven works is, if you run a command for any of the lifecycle phases, it executes each default life cycle phase in order, before executing the command itself.
order of execution
validate >> compile >> test (optional) >> package >> verify >> install >> deploy
So when you run the command mvn package, it runs the commands for all lifecycle phases till package
validate >> compile >> test (optional) >> package
And as for mvn install, it runs the commands for all lifecycle phases till install, which includes package as well
validate >> compile >> test (optional) >> package >> verify >> install
So, effectively what it means is, install commands does everything that package command does and some more (install the package into the local repository, for use as a dependency in other projects locally)
Source: Maven lifecycle reference
package will generate Jar/war as per POM file.
install will install generated jar file to the local repository for other dependencies if any.
install phase comes after package phase
package will add packaged jar or war to your target folder, We can check it when, we empty the target folder (using mvn clean) and then run mvn package.
install will do all the things that package does, additionally it will add packaged jar or war in local repository as well. We can confirm it by checking in your .m2 folder.
Package & install are various phases in maven build lifecycle. package phase will execute all phases prior to that & it will stop with packaging the project as a jar. Similarly install phase will execute all prior phases & finally install the project locally for other dependent projects.
For understanding maven build lifecycle please go through the following link https://ayolajayamaha.blogspot.in/2014/05/difference-between-mvn-clean-install.html
mvn package command will compile source code and also package it as a jar or war as per pom file and put it into the target folder(by default).
mvn install command will compile and package, but it will also put the package in your local repository. So that other projects can refer to it and grab it from your local repository.
mvn install command is mostly used when you wants to compile a project(library) which other projects in your repository are depending on.

Testing Java project with many modules

I have a Maven Java project with many modules and one meta-modul. And I want to run test. I use: mvn test command in console. But my test fails when compilation, because classes in other modules are not found. But in IDE Eclipse no errors. How can I fix that?
if you have well defined dependencies, use mvn install instead of mvn test. Running tests is included in install phase too and you'll get the modules you need for compilation into local maven repository.
Try to run mvn install. This compile, packages and runs tests. Then when everything is compiled you can probably run mvn test only. But you should not (IMHO) because only when you are running the full process you are sure that the newest versions of your classes are being tested. Do not worry about efficiency: maven does not compile classes if it already have them compiled. Only mvn clean install will rebuild everything from scratch.
Your models need to be free of dependency cycles!
Try to run mvn test from the folder where the parent pom is located.
(details)
If you do not have a parent pom with sub modules. Then you must first run mvn install for all the other of your modules where the module you want to test depends from.
(Eclipse does not need this, because it can resolve dependencies to other open projects directly)
But if all the modules belong to an single release cycle (all the modules will be released togeter with the same version) then it is may a better approach to use parent and child modules/pom -- because then you can run mvn test or mvn install for the parent pom, and maven will do it for all the childs in the right order. -- After you have installed all other modules, you can run mvn test on a single module until you update an other modul. -- Then you will need to install this updated modul too, or better run install for the parent.

Categories