Running custom pom.xml in child module of Maven Project? - java

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

Related

How to get access to a file from one child module into another child module in maven?

This is my maven project Structure:
Parent Module
|
|---Child Module A
|
|--target/xyz.jar (generated during mvn clean install)
|---Child Module B
|
|--target/abc.jar (generated during mvn clean install)
|--rpm-maven-plugin (in pom.xml file)
Reactor Build Order:
1. Parent
2. Child A
3. Child B
I want to access xyz.jar in mappings sources so that the rpm contains/bundles both abc.jar and xyz.jar inside a single rpm. I tried doing
./Child A/target/xyz.jar
and
Child A/target/xyz.jar
but it says source location does not exists. I am not sure how maven can traverse from one child module to another child module.
Thanks.
You do not access other artifacts through relative paths, but you use the Maven coordinates. After clean install, the respective artifact is in your local repository, so you can get it through its GAV. If you need to copy it, look at the dependency:copy goal.

Maven - parent project requires child plugin

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.

Why Maven doesn't install dependencies in multimodule project?

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

Jenkins: Maven project with modules that won't be build: no pom.xml found

I've got 3 maven projects.
Project A defines B and C as module in it's pom.xml
Now I want to use Jenkins.
I defined to checkout/build project A and I think I have to manually checkout/build B and C as well, right?
The project didn't find the pom.xml of the other projects.
I defined them with relative path.
It works fine on my local machine, but it crashes on Jenkins:
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM: Failure to find de.saenger:xyz:pom:1.0.0-SNAPSHOT
Now ... how to let the projects knowing each other?
Run mvn build on parent pom. It will build modules automatically.
You're running mvn build on module and mvn searches your local repository for parent pom to resolve dependencies but can't find the pom because you haven't built it yet.

Why does maven site:site fail in a multi-module project?

I have a Maven-3 multi-module project:
/root
pom.xml
/bar
pom.xml
/foo
pom.xml
Module foo depends on module bar, and they both have the same parent: root. Everything works fine, until I decided to clean my local repository and run mvn site:site. Build fails with a message:
Failed to resolve artifact.
Missing:
1) com.XXX:bar:jar:1.0-SNAPSHOT
It is a known bug or I'm doing something wrong? I didn't configure maven-project-info-reports-plugin anyhow in any pom.xml yet.
If you do site:site, you are running the specific site goal and not the build cycle, thus the project will not build, and since you just cleaned the repository, the artifact will not be there anywhere to use.
See following links for more information about build lifecycles in maven:
http://www.sonatype.com/books/mvnref-book/reference/lifecycle.html
http://www.sonatype.com/books/mvnref-book/reference/lifecycle-sect-package-specific.html
Perhaps you should try goal site:stage on the root/master-pom.
I didn't try this with src/site in the childs, cause i only have a src/site in the master.
But this work fine for me with Maven 3.0 and a menu ref="reports" in roots site.xml and
putting maven-project-info-reports-plugin in the childs pom.xml

Categories