Testing Java project with many modules - java

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.

Related

How to Avoid Installing or Deploying a Test Only Module in Maven

In a multimodule Maven project I have some integration tests that run after everything else has built and depend on all other modules. These are in a module called integration-tests. The whole module is just tests though, nothing else. I don't want this installed to the local repository or deployed. I don't even need it packaged.
How can I disable the install and deploy lifecycle phases for this submodule only? (Or make them no-ops with no goals.)
That is, I want to type mvn clean install from the root directory and have the tests execute and the build fail if they fail. However if the build succeeds, I don't want an integration-tests module to be placed in my local repo. All other modules should be installed like normal.
One way I could think of is to set the skip parameter of the install and deploy plugin to true (in the POM of this module).

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.

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.

Running Maven goals in a reactor project without doing "mvn install"

I'm trying to execute a Java file in a subproject in a maven reactor project. This is similar to the question Maven exec:java goal on a multi-module project, but unless I do a mvn install then the exec plugin can't find the class that I'm trying to run in the subproject.
Perhaps I misunderstand the intended workflow of mvn install, but having to do mvn install every time I make changes really complicates the workflow.
When I execute the file from Eclipse, Eclipse sets up the classpath correctly (i.e. module1/target/classes, module2/target/classes) and I want to emulate this behaviour from the command line. I thought doing mvn -pl exec:java -Dexec.mainClass=... would set up the classpath in this way, but the class is not found in this case.
The classpath isn't the problem in that case. But you have to compile your classes (e.g. at least run mvn compile).
If you run your application within Eclipse, Eclipse will do the compile work, on the commandline you have to explicitly call that command.

Categories