Maven build parent pom last - maven

I've got a parent pom building a bunch of modules, looks like this:
pom.xml
| project-A / pom.xml
| project-B / pom.xml
| project-C / pom.xml
Which results in a build order of
Parent
A
B
C
When I install, I want to run a bunch of ant tasks that are in the parent pom, basically moving the jars in them modules into another directory, which requires the modules to be built first and the the parent to do it's thing. The problem is that the parent pom builds first then builds the modules, which isn't want I want.
The main requirement though is that I have to have it done in the install phase, if thats possible.
How can I make it so that when I run clean install, it runs the ant tasks AFTER the modules have been built and the module's jars are in their target directory?

Just create a "packager module" that depends on all of the other modules and run your ant tasks there.

In Linux the order is A,B,C,Parent. That's also a problem for me. It seems Linux and Windows has different fixed order.

Related

How to copy all the dependencies in Maven project modules to a directory?

I have a big local maven project that contains multiple modules which are inturn maven projects and are dependent on one another.
Ex.
parent pom.xml
<pom>
<module1> #jar
<module2> #dependent_on_module1.jar
<module3> #
</pom>
I have mentioned the sequence to build those modules in the parent pom.xml .
I also mentioned where to place the artifacts when they're built in groudId and artifactId.
But in the dependencies for all those modules, I have mentioned a common local system path for all those modules.
Is there any way to copy all the artifacts which are being created for modules when maven build is performed on the parent pom to a specific directory that can be dynamically mentioned when the maven command is run.
I have searched for maven copy command. But looks like it's not going to do what I want.
Any suggestions?

How to run maven plugin for only root of multimodule project [duplicate]

I have maven multi-modules project. At the parent level, i have some java files. And in the parent pom.xml, at the package phase i do some stuff.
Usually, when i run mvn package at parent level, the package phase of parent pom will be run and all the modules will be packaged as well.
I am looking for a way that allow me to do these (when i run mvn package):
allow me to run only paren pom.xml (the script at the package phase), not the modules. This is the 1st priority.
allow me to run paren pom.xml and some particular modules (like module 1, module 2 BUT not module 3 , module 4).
Can i use profile for those issue?
Thanks.
While I agree with the fact that you may not have optimal project structure, the answer is that Maven 2.2.1 has an option "--non-recursive" which satisfies your first requirement:
-N,--non-recursive Do not recurse into sub-projects
So something like this:
mvn --non-recursive clean compile
Why do you want to have java code on the top level? In my opinion this is not a very good idea. Have your code in the subprojects and let the top-level project be responsible for holding the general information and configuration of the entire project.
If you have some base-library code in the top-level project now, you can put it in a sub-project and set up dependencies between the projects.
Take a look at Maven parent pom vs modules pom
The nature of your question indicates that your project structure may not be optimal.

Can Maven automatically build a dependency that is not found?

I have three separate projects I am working on (A, B, and C).
Project B and C rely on a jar that project A generates.
Does Maven have the ability to automatically build project A if the dependency is not found?
The answers I've found so far are indicative of making the other 2 projects modules (which I believe to mean repository layout and incorporate them into project A) and create a parent / child pom.
A just plain "no" was also one of my conclusions as well.
It seems as though if I make a module of project A in B and C, maven doesn't really like that. Can Maven see projects during build time that are outside of the scope of the current project? Sorry if that's a little wordy.
The scenario works fine if A, B and C are modules of a common container project.
From root pom.xml:
<modules>
<module>project-a</module>
<module>project-b</module>
<module>project-c</module>
</modules>
where "project-a" etc. are names of maven project folders inside the parent folder.
The parent project must have <packaging>pom</packaging> for this to work.
Then you can build the parent project and it will build all children in order, or you can use one of the advanced Maven reactor flags.
e.g. mvn clean install -pl project-b will only build project B.
For more info, execute mvn --help and read the multi modules chapter from the Maven By Example book.
But to this question:
Does Maven have the ability to automatically build project A if the
dependency is not found?
... the answer is always no. Maven fails if the dependency is not found, but it never decides which projects to build. You are in charge of which projects need building.

Independently building Maven submodules

After being introduced to Maven in my most recent project, I've been experimenting with it for the past couple of weeks.
I currently have a multi-module project in my development environment. Project "A" and Project "B" are child modules of Project "root", with B having a dependency on A.
I am aware that I can build B independently by using mvn reactor:make... as outlined here. However, I'm curious as to why am I not allowed to build B from inside B's root folder.
To illustrate this, I first built A independently by running a clean install from within A's root directory. However, when I tried doing the same action from B's root directory, Maven reported an error as below -
Could not find artifact org.divesh.mavenrnd:root:pom:1.0 in central
It looks like Maven is not able to resolve the parent's POM file. Any thoughts on why this is happening? From my initial understanding of Maven multi-module projects, the main reason to split a project into sub modules is to share dependencies. This should not, however, prevent me from building a particular module independently from within its folder.
Thanks.
EDIT
Ran an mvn -N clean install to install only the root project's POM in the rep. After this, I was able to successfully build B after building and installing A. There is still one thing I'm not quite clear about -
Assuming I've installed the root project's POM in the repository, if I try to build A, does it refer to the parent root POM directly above it or the POM that is installed in the repository?
That's right. The error you mentioned tells you that maven cannot find parent's pom.
You have 2 options here:
First call mvn -N install in your root project directory to install parent pom to your local repository. Then you can go to B's directory and build the submodule.
Specify <relativePath> in your submodule pom. By default maven will look for parent pom only one level up the file system. If you have other directory structure in your project, you have to specify this option in order for maven to locate your parent pom.
Here's a sample:
<parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<relativePath>../../pom.xml</relativePath>
</parent>
You should use mvn -pl ProjectToBuild lifecycle like from the root of your tree:
mvn -pl module-b package
You shouldn't use mvn reactor:make anymore. Except you are using maven 2.0
If you wan't to be sure that everything is depending on module-b is build as well you should use:
mvn -amd -pl module -b package
The best is having a folder layout which represents the appropriate structure of your project and not using relativePath is necessary.

How can a maven project depend on another local maven project?

I'm having two maven project project/foo/pom.xml and project/bar/pom.xml. I have foo depend on bar, and I want that every timefoo/pom.xmlcompiles, it'll automatically compilebar`.
How can I do that with maven?
Update: I can tuck them both into a parent project, but then, what will I do if I want to run mvn jetty:run on a child project?
Option 1
Setup both builds in Jenkins, which can detect dependencies between projects
Automatic build chaining from module dependencies
Jenkins reads dependencies of your project from your POM, and if they are also built on
Jenkins, triggers are set up in such a way that a new build in one of those dependencies
will automatically start a new build of your project.
Option 2
If the two Maven projects are closely related (released together, sharing the same revision number) then perhaps they're really two modules of the same project?
If that is the case read the following document for guidelines on how to create a parent POM:
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
You can combine them into one project, with two modules.
An example project structure:
parent (pom)
|- foo (jar)
|- bar (jar)
In the parent pom:
<groupId>org.me</groupId>
<artifactId>parent-project</artifactId>
<packaging>pom</packaging>
<modules>
<module>foo</module>
<module>bar</module>
</modules>
In each child pom:
<artifactId>foo</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>org.me</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
To build the project (both modules) with Maven, execute this from the parent directory:
$ mvn install
This is not possible using maven alone. The closest you can get is to make both as modules of an aggregator POM, so you can build both with a single command. Note that a mvn install will already consider if there have been changes since the last build so you're saving a few CPU cycles there.
First, setup a multi-module build as Daniel has shown. Then change to the directory with the parent POM. Then, for example to install foo with automatically installing bar before, too, type:
mvn --also-make --projects foo install
This will check the dependencies within the reactor and then resolve to run the install life cycle on bar before it runs the install life cycle on foo.
This works with any life cycle or goal. Mind you however that if you specify jetty:run, then this goal would be run in both projects, which is probably not quite what you want.
You can find more information about this feature here: http://maven.apache.org/guides/mini/guide-multiple-modules.html

Resources