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

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

Related

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.

Maven build parent pom last

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.

Build single module from multimodule pom

Is it possible to do?
The environment: Multimodule pom consists of 3 modules: mm1, mm2, mm3. Module mm2 has mm1 as dependency. It is possible to build parent pom without any errors.
The question: Is it possible to build single module mm2 (i.e., run maven from mm2 base directory) without installing mm1 into local repository?
Thanks.
I'm not sure what you mean exactly by "without installing mm1 into local repository". Do you mean previously to building mm2 or never?
In doubt, maybe one of the new build options announced in the Maven Tips and Tricks: Advanced Reactor Options blog post can help:
Starting with the Maven 2.1 release,
there are new Maven command line
options which allow you to manipulate
the way that Maven will build
multimodule projects. These new
options are:
-rf, --resume-from
Resume reactor from specified project
-pl, --projects
Build specified reactor projects instead of all projects
-am, --also-make
If project list is specified, also build projects required by the list
-amd, --also-make-dependents
If project list is specified, also build projects that depend on projects on the list
I was specifically thinking to the -pl and -am options. To build a subset of the modules, run the following from the root directory
$ mvn --projects mm2 --also-make install
However, I'm not sure this answers your question (which is not totally clear for me).
Without automatic installing not, but it's possible to build only choosen projects. You need to have multi module build (I'm assuming you do). In reactor mode every command need to be run from the root of reactor.
So in your case:
mvn reactor:make -Dmake.folders=mm2
In this case you build mm2 module and modules on which it depends (mm1).
Useful links:
Maven reactor plugin reference
Maven book reactor chapter
From book examples I build only project persist and his dependency project model. Others projects are untouched with
mvn reactor:make -Dmake.folders=sample-persist
alt text http://www.sonatype.com/books/maven-book/reference/figs/web/running_aro-dependencies.png
Other useful command is reactor:make-dependents which build projects that depend on X.
This goes against the principle of dependencies of Maven2. What is the interest of doing that exactly?
However, we can imagine to define the mm1 dependency of mm2 as a system dependency:
<dependency>
<groupId>...</groupId>
<artifactId>mm1</artifactId>
<version>...</version>
<scope>system</scope>
<systemPath>../mm1/target/</systemPath>
</dependency>

Maven - skip parent project build

I know it's mauvais ton to ask twice in a single day but here's another Maven puzzler:
I have a parent POM which defines 5 modules (5 subprojects). Since each module is executed in exactly the same way I pull <profile><build> section into the parent POM to get rid of the duplicate code. Now - if I execute build individually from each module it works, however if I want to build all modules at once and move to the parent directory I got error since the very first thing Maven tries to execute is the parent project itself:
mvn package -P release
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO] DWD Parent project
[INFO] Projects
After that build fails because exec plugin tries to execute something that is not there. Looking at the output it is pretty obvious that reactor plugin is driving the build but how can I configure reactor to skip the parent?
P.S. To prevent confusion - I'm trying to suppress profile execution on parent and enable it on child during the same build
You can't skip the parent build, but you can configure the profile to not be activated with a little hack. This answer shows how to control the activation of a profile by the presence or absence of a file-based <activation> element. This way you can define the profile in the parent, but have it deactivated in that project because of the marker file being present in the parent. Child projects would not have the marker file in their source, so the profile would be activated for those projects.
Update to clarify: On my test project this configuration means the profile is deactivated in the parent project (which has the file in src/main/resources), but activated in all child projects which do not have the file in their resources directories.
<profile>
<id>test</id>
<activation>
<file>
<missing>src/main/resources/test.marker</missing>
</file>
</activation>
...
</profile>
What is wrong by building the parent?
In fact, in Maven, there are two different concepts (which are generally used at the same time):
The parent POM
The modules aggregation
The first one is a definition of everything that is common with all the children. You define a parent as a pom-packaged project, and you can install it in your repository.
When you build a child of this parent, Maven2 will retrieve this parent to merge the parent pom with the child pom. You can have a look to the entire pom.xml by running the command mvn help:effective-pom.
In this case, the parent pom will not be built, it will just be retrieved from the repository.
The second case is a project that contains a modules list of sub-projects. The principle is that every command that you run on this project will also be run on all the sub-modules. The order of the modules will be defined by the Reactor, which will look at inter-modules dependencies to find which module must be built before the others. If there are no dependencies, then it will take the list of the modules as they are defined in the parent pom.xml.
In this case, if you run a command on the root project, Maven2 will first built the root project, and then the sub-modules. You cannot skip the creation of the root project.
Edit, thanks to RichSeller comment
A complete explanation of the differences between multi-modules (aggregation project) and inheritance (parent project) can be found in the Maven book, here.
I want to document that there's partial compromise to my situation (thanks to guys on maven users mailing list for suggestion). Basically you need to chop profile in two pieces. Reusable configuration section of plugin goes to the parent POM, and executions stays in the child POM. Then the profile plugin in the child is marked as inherited and voila - at run time parent profile is not executed since it's missing executions section. This is far from ideal but it does work. Refer to this link for example
I wasn't able to implement "missing" file solution as provided by Rick Seller above. It seems that once set active/non-active state of profile will not be changed even the marker file is missing from the module(s). However here's the exact solution to my problem. Warning: this is only available starting with Maven 2.1+
If I have a parent POM with 2 modules defined: foo and boo then in regular circumstances order of execution will be:
parent
foo
boo
All I need to do to skip parent build is to add this command line switch
mvn install –rf foo
Alternatively you can use --resume-from What it will do is to skip parent and continue from foo module down. Now - I'm investigating if this can be achieved by configuring Reactor plugin (P.S. - no it can't) but even with the switch, the above scenario works fine for me
This is implied by #romaintaz's answer to the same question, Maven - skip parent project build
but just to make this explicit, in the parent pom it's imperative that you specify the packaging element as pom (and not jar or war).
Example:
<packaging>pom</packaging>

Resources