Maven Modules with Variable as Version number - java

I got a Maven project with the following structure:
Module A (parent), Submodule B and Submodule C
In the parent pom.xml I am using a variable for settings the version of all projects:
...
<version>${revision}</version>
...
<properties>
<revision>1.1</revision>
</properties>
...
<modules>
<module>moduleB</module>
<module>moduleC</module>
</modules>
</project>
Module C is my distribution package which uses the shade plugin for packaging everything into one single jar.
In the submodules I set the parent like this:
<parent>
<groupId>group</groupId>
<artifactId>moduleA</artifactId>
<version>${revision}</version>
</parent>
Now I want to use Module C in another project, however I only get the following error when doing so:
Failed to execute goal on project newProject: Could not resolve dependencies for project group:newProject:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at group:moduleC:jar:1.1: Failed to read artifact descriptor for group:moduleC:jar:1.1: Could not find artifact group:parent:pom:${revision} in nexus (NEXUS_URL)
I assume the problem is, that the variable value is not filled in when referencing Module C as dependency. How can I solve this issue? I already tried to clean the project before building and forcing to update all artifacts without success.

Maven expects all modules to have a hard version.
To avoid editing lots of poms, use the versions plugin, example:
mvn versions:set -DnewVersion=1.0.1
If you run the above command on the parent it will fix all the child poms.
See here for documentation.

Thanks to the comment of hadu.mansouri I could fix it. I used flatten-maven-plugin for flattening the pom.xml files. However, it seems to have a problem with the shade plugin, as the shaded module was the only module where it did not work. Thus, in the released shade module pom it said:
<version>${revision}</version>
for referencing the parent. I used the approach of the article linked in the comment. There, this extension was used: https://github.com/jcgay/unique-revision-maven-filtering
Using this instead of the flatten-maven-plugin, Maven builds the multi module project correctly with the single version property, and I can also use the shaded module in other projects properly.

Related

Parent POM dependency tree discovery for sub modules (Maven)

I have created a maven goal that outputs a dependency tree for a given maven project to a YAML file. Using the m2eclipse plugin for Eclipse Photon.
Currently when I run the goal against a parent project (using type pom when setting up dependency), the only dependency returned is on the pom for the specific parent. Is there any way to prevent me needing to run this goal against all of the projects underneath the parent?
Might need a little more information on this, but are your children poms of the parent specified as modules within the parent pom?
Example:
<parentPom>
<modules>
<module>/path/to/childA</module>
</modules>
</parentPom>

How to include the modules in a top-level POM in another project?

I've googled and stack-overflowed everything, but maybe not enough as I'm still not clear how one adds a module of a Maven project as a dependency in another.
So for example let's say we've got:
MajorPager
|___ POM.xml
|___ chuck-commons-core
| |____POM.xml
|____rajni-commons-core
|____POM.xml
Now I want to add chuck-commons-core but not rajni-commons-core. If I do it directly, it can't find the module. So I ran across the following discussions on stack-overflow and my old friend Guggal:
In summary, the below discussions talk of how to create multi-module projects but not really how to include the sub-modules in a top-level POM into another project.
Useful discussions for context
SO Adding a reference to a parent POM project
SO How to add a parent POM as dependency to a different maven project
SO Adding a reference to Parent POM
SO Maven Parent POM vs Modules POM
SO Depend on multi-module aggregator project
SO How to add dependency in Eclipse?
SO How do I configure Maven multi-module-dependency on sub project with different package?
SO How to add a module in parent projects POM as dependency?
SO How to add a dependency in Maven?
Maven dependency mechanism
Maven POM aggregation
Baeldung Multi-module project
Codetab Maven multi-module
Howtodoinjava Maven parent child POM example
Mastertheboss Maven multi-module tutorial
Codetab Multi-module hierarchical project
Sonatype How to share resources across projects
Spring Multi-module project
Concretely, I'd just like a summary from an expert how they add a sub-module of a Parent POM as a dependency to another project.
As of now, I think this is the best solution - to add the following ccodee to the POM.xml of the MajorPager.
Code block 1 incoming
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.teachme.how</groupId>
<artifactId>MajorPager</artifactId>
<version>0.6-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Code block 2 incoming
<dependencies>
...
<dependency>
<groupId>com.teachme.how</groupId>
<artifactId>norris-commons-core</artifactId>
<version>0.6</version>
</dependency>
...
</dependencies>
When you do so enables Maven to find out where the dependency are - and so Maven picks them up with mvn clean -U install or mvn package -U -DskipTests. And I should like to be educated if this isn't the most effective pattern. Or if any of the fields above are optional that Maven doesn't want me to specify because it can derive it on it's own (say for example, version tag?) - please let me know if that's case as well.

Replacing variables when packaging the pom.xml into JAR

I have a Java Maven project with several modules. When referencing to the parent module from the individual modules, the following information is added to project A\module-sample-app\pom.xml:
<groupId>org.apache.maven</groupId>
<artifactId>module-sample-app</artifactId>
<packaging>jar</packaging>
<name>module-sample-app</name>
<parent>
<groupId>org.apache.maven</groupId>
<artifactId>sample-app-parent</artifactId>
<version>1.0-${buildLifeId}</version>
</parent>
The parent application A has the following entry in A\pom.xml:
<artifactId>A</artifactId>
<packaging>jar</packaging>
<name>A</name>
<version>1.0-${buildLifeId}</version>
To run the build, the following is executed on the parent directory of project A:
Example: mvn clean install -DbuildLifeId=1001
The JAR file generated has an embedded pom.xml with string ${buildLifeId} and not the value 1001.
When trying to use on the module sample-app in another application, B, as the runtime variable was not replaced with the actual value, it is failing to fetch the dependency. Just to highlight, B is not a module of A.
In B\pom.xml:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>module-sample-app</artifactId>
<version>1.0-1001</version>
</dependency>
With this dependency, as the pom.xml embedded within the module-sample-app-1.0-1001.jar would have the parent version defined as <version>1.0-${buildLifeId}</version>, the dependency fails to load.
So my questions is - does Maven support a plugin that can replace the run time variables before packaging the pom.xml into the JAR? Or is there a workaround. Can I use Ant plugins to replace the buildLiefeId variable in the version node to actual value when JAR is built?
I was already using ${revision} - I just had to use the Maven Flatten plugin in both the parent and child poms to fix this issue.

Maven multi-module project and Jenkins

I have the following projects organized in a flat structured way:
parentProject
+-pom.xml
projectWeb <depends on libraryA and libraryB>
+-pom.xml
libraryA
+-pom.xml
libraryB
+-pom.xml
The pom.xml inside the parentProject has references to the other modules and its used for inheritance and dependencyManagement, here is a snippet:
<project>
....
<modules>
<module>../projectWeb</module>
<module>../libraryA</module>
<module>../libraryB</module>
</modules>
<dependencyManagement>
...
</dependencyManagement>
<build>
...
</build>
....
</project>
In Jenkins I have one maven job for each project, and it works fine when I build the parentProject, ie. builds every project referenced in the modules section.
The problem that I have is when I commit to the SVN a change in libraryA, I would expect that after building libraryA, a rebuild to projectWeb to be launched, but that didn't happen.
Anyone knows what am I doing wrong?
Thanks in advance.
EDIT
When I remove the modules section from parentProject\pom.xml, it works as espected, but I loose the aggregation advantage of having a parent pom.
It looks like you're asking your parent POM to do two things:
Set up dependency management stuff
Aggregate your build
It's generally better if you split this out into two poms - a parent pom for #1 and an aggregate pom for #2. You'd then have something like..
[root dir] aggregate pom.xml
+ /parent
+ /web
+ /libA
+ /libB
See this answer for more details: https://stackoverflow.com/a/3301162/211993
You'd then configure Jenkins to check out the root dir and run "mvn clean install"
This should be configured in jenkins job. See "libraryA job"/"Configuration"/"Build Triggers"/"Build after other projects are built"

Having a maven project build its own dependencies?

With maven is it possible to have a top-level project who's packaging type is "war" which will build itself and all of its dependent modules (packaged as jar) and have the build generate a project.war file?
Much of the documentation examples and other examples I've seen often use a top-level project with packaging type of "pom" and the project only serves the purpose of tying the modules together. Can I avoid this?
So basically I need something which is effectively like declaring a <module>my-module</module> for maven to build, and in that same POM, declaring a <dependency>...my-module's artifact...</dependency> on that same module which needs to be built. Maybe a plugin as someone already suggested?
Update: In other words (to simplify the problem): If I have project A and project B, where project A depends on project B - is there a way for me to execute a build on project A and also have it automatically build project B (and include project B as its dependency - creating projectA.war which contains projectB.jar)?
super_aardvark suggested correct way but,
For requirement I would suggest following structure It is suitable and good structure also :
Consedering ProjectA as project-webapp , ProjectB as project-core
You can have following structure :
Your Grand Project :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.project</groupId>
<artifactId>project</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Project Repository System</name>
<description>Project Repository System R2</description>
<modules>
<module>project-core</module>
<module>project-webapp</module>
</modules>
</project>
Your WebApp Project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>com.mycompany.project</groupId>
<artifactId>project</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>project-webapp</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Project Web Application</name>
<description>Project Repository</description>
<dependency>
<groupId>com.mycompany.project</groupId>
<artifactId>project-core</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</project>
Your Core Project:
<project>
<parent>
<groupId>com.mycompany.project</groupId>
<artifactId>project</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>project-core</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Project Core</name>
<description>ProjectCore</description>
</project>
Your Directory structure should look like:
-------Grand Parent.pom
|
|--------project-webapp
| |
| project-webapp.pom
|
| -------project-core.pom
|
project-core.pom
From parent pom execute mvn clean install it will build both the web-app and core project
That's not really what a top-level project is for. Your WAR project has dependencies, which are the artifacts (e.g. jars) that will be included in the WAR (in WEB-INF/lib) when you run 'mvn package'. Your WAR project pom can have the top-level project as its parent, but it shouldn't be the parent of its dependencies. You may want to have that top-level project be the parent of both the WAR project and of the JAR projects that are dependencies in the WAR.
This is not possible in Maven 1, 2 or 3.
I'd recommend to give up this idea, because Maven's whole purpose is to enforce standardized development process. Don't fight the structure, just create a parent POM module and make the WAR module and other dependencies underneath it.
When you have a multi-module project and you're doing work in several modules simultaneously it can be tedious and error-prone to make sure all the necessary dependencies are updated.
In my situation, I would like my build system to detect changes and only build the modules that are necessary. One way this might be possible with maven is for someone to write a custom plugin that does this, which doesn't seem insurmountable given there are already complex plugins available, like the maven release plugin.
Others have already mentioned the aggregation pom concept, which is repeatable and does produce the necessary artifacts. But sometimes you end up building more than you really need to.
Maven profiles can help and here's a good article in that regard:
Using Aggregate and Parent POMs
Also note in the article the concept of the batch pom, which I was not previously aware of.
Remember, mvn clean install will push your artifact into your local repo. So if module A depends on module B, as long as your local repo has the latest build of module B then you should be all set. So, if there were an external tool that was watching for changes to module B and automatically built it when there were and pushed those changes into the local repo then when module A was rebuilt it would pick up those changes. There are continuous integration (CI) tools that can do this, like Jenkins. But you would need a local install to have this work directly with your local repo. It's still an option, though.
Another option would be for the CI environment to push your builds to an external maven repo (or even one you setup locally with something like Nexus). Then you setup your CI builds to pull from that location as well.
So, there are solutions that rely on other tools or potential plugins to do what you want - just depends how much time and effort you want to invest to get it all setup. But, once you get over that hurdle you'll have a system (and knowledge and experience) that you can use on all your projects, not to mention you'll be familiar with how many development shops/teams work.
I would recommend researching continuous integration and continuous delivery for more information and ideas.
In parent pom, you have to define a sequential order of modules to be compiled. You can add a war packing module to the last in that list. It will simply aggregate all previous compiled code together.
Not really - (Well, I can think of a couple of ways, but I'd not use them as they're convoluted and go against the basic ethos/practices of Maven).
Don't forget that the other purpose of the top-level pom is to provide a single point to set common details such the particular versions of dependencies used in the modules of the project.
NetBeans has an option that allows you to do exactly this with Maven projects but I don't know any pure Maven solutions. I think that the task is more suited for an IDE, because it knows for what depended projects you have the code (based of what projects you have opened in the workspace). How would Maven itself differentiate between a dependency that you want to build and one that needs to be fetched from the repository. And for those that need to be built, where should it look for the source code?
Anyway, another solution to the problem, that I used successfully a few times, is to create a simple shell script that navigates to your projects folders and starts the build then it waits for it to finish then proceeds to the next project and so on.

Categories