In maven multimodule project is it recommended to use package or install - maven

I have a maven multimodule project
<modules>
<module>A</module>
<module>B</module>
<module>C</module>
</modules>
Module C depends on Module A. In this project structure can you let me know it is recommended to use package or install. I do not have any other requirement to share this project with other projects.

When launching a Maven command on multiple modules, if a dependency specified in one module is available as a project being built in the same command, Maven will use the output of the current build as dependency, instead of the JAR present in the local repository.
So it is not necessary to use install to make the changes of a module available to modules depending on it, as long as these modules are always built together.

Related

Maven multi module, dependency between modules

I created a maven multi modules project with 2 modules: core and service.
Service module is using core module (core is declared as a dependency on service module).
The parent pom version is 1.0-SNAPSHOT (so the modules too).
During the maven clean package phase, maven is not fetching core module neither from my local repo nor from remote repo (nexus). It is always building with core module within the project and not fetching any more recent SNAPSHOT version from repo (local/remote).
Is that the expected behaviour?
If you build the project, it will build everything. The command clean package will resolve the inter-module dependencies inside the project.
Everything else would probably lead to surprising results.
If you really just want to build just one module, use the -pl parameter.

Maven multi project

I have a several maven module (3 jar and 2 war, modules are interconnected).
My purpose is combine this modules to one ear file.
Example: in ear maven project I run in lifecycle "package" goal, when all maven project, who include in global project are execute "package" goal and when creating global ear, who include jar (war) from other maven project.
How do this?
I create global project as <packaging>pom</packaging>, it execute "package" goal from dependency project
<modules>
<module>...</module>
</modules>
but how create ear?
You should use <packaging>ear</packaging> instead of pom.
It will force maven to use maven-ear-plugin on package phase (so you don't need to provide any executions of this plugin).
You could find simple example here: Maven EAR Plugin Example

Maven multi module project build: modules in local repo

Suppose we have a multimodule project:
<modules>
<module>first</module>
<module>second</module>
</modules>
where second depends on first through dependencies.
If I run mvn cleanpackage, does maven or reactor will put any of the modules to local repo anyway?
The answer is no.
Here are the maven lifecycle phases
As you can see, package comes before install, so install wont be executed - nothing will go to your local repo, unless it was there already.

Maven build order

I have a multi-module maven build and I need one particular module (lets call it project-A) to be build at the end. It depends on a module (lets call it project-B) that holds native code that gets compiled to a dll and installed into the maven repository as a zip file using some maven trickery. As it doesn't depends on it directly because the native code is not a java jar, I use Maven Dependency Plugin to unpack the zip file and place the native dll in my build directory. Everything is working fine except for the building order. It builds first project-A in spite of being declared the other way around in the tag in the parent. I would like to tell maven that project-A depends on project-B. I tried to add project-B as a dependency, but as it builds no jar it throws an ERROR, also this seemed hacky to me. Any help would be appreciated.
Just declare dependency in project A to project B and it will work fine. It does not matter if the project B is a native rather than a java project. Just make sure you declare the dependency correctly taking the packaging into account as type.. (which is probably pom so you would have
<dependency>
<groupId>...</groupId>
<artifactId>B</artifactId>
<version>...</version>
<type>pom</type>
</dependency>
in Project A)
The order in which you specify the modules in the parent Pom is also relevant. Maven actually builds in this order unless it has to build a module out of sequence due to direct dependencies.

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