I have forked a maven project (webservice) having a parent module and 3 child modules. One of the 3 modules is responsible for running the others and uses tomee plugin to run.
The documentation says: use mvn clean package tomee:run to run the web service.
If I run this command in the root directory, I get:
No plugin found for prefix 'tomee' in the current project and in the plugin groups
since the plugin is the child module pom.xml
And if I run the command in the child (runner) directory, I get:
Failure to find **Another child module war file** in https://repo.maven.apache.org/maven2 was cached in the local repository.
From where I should run the command, and how it fix it? I don't think the pom.xml files structure have bugs. But I don't know how to run a project with multi modules.
I used to run:
mvn clean install and mvn clean package
In the parent module directory, then in the 2 child (not the runner) and finally in the runner child module I run:
mvn clean install and mvn clean package tomee:run
And it works.
For the future, I only run
mvn clean package tomee:run
In the runner child directory to start my project.
Related
I have a java maven project which has 1 parent pom and 3 child modules associated with it. One of the child module has a custom pom.xml alongwith the default pom.xml.
If I run mvn clean install on Parent pom, all the 3 child modules (with default pom.xml) gets executed.
I wanted to run custom pom.xml instead of default pom.xml for one of the child module.
I tried below:
mvn -pl module1,module2/custom.xml,module3 clean install
But it didn't work and I got below error:
[ERROR] [ERROR] Could not find the selected project in the reactor:module2/custom.xml
Instead of having custom pom for one module, you can use maven profile to achieve custom behavior in some build.
You can run maven build with:
mvn -P profile_name clean install
My project structure (multi module) is like this
parent
projectA
projectB
... other modules
parent also actually has a parent (Spring Boot).
I have set up a Jenkins jobs to compile & test on every commit, so it runs:
mvn -f pom.xml clean install
And that all works fine. ProjectB depends on ProjectA (which is like a common classes type of project) and is a Spring boot application. So the dependency information is the regular:
<dependency>
<groupId>Group</groupId>
<artifactId>ProjectA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
ProjectB has a separate job in Jenkins to build the deployable jar file and deploy it to server. So the command there is:
mvn -f ProjectB/pom.xml clean install antrun:run
This fails with a message like:
[WARNING] The POM for Group:ProjectB:1.0-SNAPSHOT is missing, no dependency information available
...
[ERROR] Failed to execute goal on project host-api: Could not resolve dependencies for project Group:ProjectB:1.0-SNAPSHOT: The following artifacts could not be resolved: Group:ProjectA:jar:1.0-SNAPSHOT...
Now I can resolve this by doing a mvn install in the ProjectA directory - I've tested this and it does resolve the issue.
But my question is why should I have to? Shouldn't Maven figure out it should be installing the jar in the local repo?
Thanks in advance
TL;DR Tell maven about the structure of your project.
When you run the command
mvn -f pom.xml clean install
Then maven uses the reactor to work out the order of the modules, something like the following is output:
[INFO] Reactor build order:
[INFO] ProjectA
[INFO] ProjectB
So Maven first builds project A, then builds project B.
When you run the command:
mvn -f ProjectB/pom.xml clean install antrun:run
Then you have a problem; maven isn't starting from the parent - it's starting from a child. It's not told about the hierarchy of projects needed to be built first.
If you want to build a single project from a maven multimodule project, along with dependencies you should use:
mvn -f pom.xml -pl ProjectB -am install antrun:run
Where:
-pl ProjectB is the "project list" option, it tells maven to build these specific projects
-am is the "also make" option, it tells maven to build any projects that the projects in pl are dependant on
Specify the dependencies build part when you run the build:
Guide to Working with Multiple Modules
So I suppose the rule is, always build the parent project first and then run goals on subprojects afterwards.
The fix for me was to run clean install on the parent project first and then have a second build configuration in Jenkins that ran -f ProjectB/pom.xml antrun:run
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.
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.
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.