Maven 3 - Variables in packed Submodules - maven-3

Dear stackoverflow community
I have a problem with a maven 3 project. Basically i have a Parent Pom X, a child Pom Y and a module Z within the Y Pom. POM Y and Module Z are packed compiled and stored in my local maven repository. Z points to the parent version with a variable defined in the Parent Pom X. So everthing compiles and installs fine with the right version number defined in Parent Pom X
Now my Problem: if i use Z as dependency in a POM B (which is also a child of parent Pom X) with the version variable defined in Parent Pom X it fails because Module Z does not resolve the version variable during maven build.
Error:
Failed to read artifact descriptor for Z:jar:someProjectVersion: Failure to find Y:pom:${someProjectVersionVariable}
Update: Here are the (simplified) poms:
Parent Pom:
<groupId>someProject</groupId>
<artifactId>someProject-parent</artifactId>
<version>5.3.3-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<child.version>5.3.3-SNAPSHOT</child.version>
</properties>
Child Y POM
<parent>
<groupId>someProject</groupId>
<artifactId>someProject-parent</artifactId>
<version>5.3.3-SNAPSHOT</version>
<relativePath>../someProject-parent/pom.xml</relativePath>
</parent>
<modules>
<module>moduleZ</module>
</module>
</modules>
<artifactId>someProject-Child</artifactId>
<name>someProject Child Name</name>
<version>${child.version}</version>
<packaging>pom</packaging>
Module Z POM:
<parent>
<groupId>someProject</groupId>
<artifactId>someProject-child</artifactId>
<version>${child.version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>someProjectModule</artifactId>
<packaging>jar</packaging>
<name>Module Z</name>
Child Bs POM:
<parent>
<groupId>someProject</groupId>
<artifactId>someProject-parent</artifactId>
<version>5.3.3-SNAPSHOT</version>
<relativePath>../someProject-parent/pom.xml</relativePath>
</parent>
<artifactId>someProject-Child-B</artifactId>
<name>someProject Child Name B</name>
<version>${child.version}</version>
<packaging>jar</packaging>
<dependency>
<groupId>someProject</groupId>
<artifactId>someProjectModule</artifactId>
<version>${child.version}</version>
<type>jar</type>
</dependency>
Now building Child Y is no Problem, with no errors
Building Child B results in:
Failed to read artifact descriptor for someProjectModule:jar:5.3.3-SNAPSHOT: Failure to find someProject-Child:pom:${child.version}

Related

adding a "outdated " maven dependency from different url (repository)

let say we would like to add this maven dependency in our POM.xml file from http://maven.jahia.org/maven2/ to a exiting POM file. What are some good(industry) practices of adding com.sun(or tools) dependency super pom ? new to maven, example or short reasonings are appreciated.
<!-- https://mvnrepository.com/artifact/com.sun/tools -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
</dependency>
<!-- Note: (http://maven.jahia.org/maven2/) -->
The existing POM file like this:
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>example-common</artifactId>
<packaging>jar</packaging>
<description>example</description>
<properties>
<example-version>7.2.0</example-version>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-sdk</artifactId>
<version>${example-version}</version>
</dependency>
<dependencies>
</project>
This is a bit of an old question, but I had the same issue, and this worked.
Add into your project pom (also possible in maven settings if you need it for several projects), the Jahia repo.
<project>
...
<repositories>
<repository>
<id>jahia</id>
<name>jahia</name>
<url>http://maven.jahia.org/maven2/</url>
</repository>
</repositories>
...
</project>
If it's in the maven settings, then you put it inside of a profile but need to activate the profile when running maven (-Pprofilename) or via activation by default in settings.xml:
<profile>
...
<activation>
<activeByDefault>true
...
</profile>

Java Maven pom.xml - dependencies issue

I have a J2EE web application setup using maven build based project.
i have pom.xml, where i have dependencies for my application.
Let's say my App only needed example hadoop-common.jar, log4j.jar becuase of pom.xml which downloads its dependencies jar.
Process downloads all jars in to .m2/repository - locally.
When I bundle war the then WEB-INF/lib has many jars along with hadoop-common.jar, log4j.jar.
How do i ensure only hadoop-common.jar, log4j.jar to be included as part of my war not its dependency in the myWebApp.war
<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>it.cvc.pcam_team</groupId>
<artifactId>pcamapplication</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>PCAMApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
...
Are these jars added along with the adoop-common? If hadoop-common is dependent on other jars, these may be added transitively. You can exclude specific jars from being included in that way.
https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html
Depending on what you're excluding hadoop-common could stop working though, if you exclude something that's needed for it to run.

Parent POM is taking the same version which I am giving in the module version

I have a multi module maven project for e.g.
A
B
C
D
E
Currently this project is working fine and have a single job to build all the modules and upload to the artifactory with some version for e.g. 4.0.0-.They are using versions:set -DnewVersion=4.0.0-${BUILD_NUMBER} from Jenkins job.Now my next task is to split this project into module so they dev team can build each module independetly but my issue is some modules is having the dependecy on other modules for e.g
Module B is having dependecy on module A and Module C.if I build the module A first then it generate the number 4.0.0-00001 and upload it to the artifactory and then I build the module C then it generate the build 4.0.0-00005.Now the question comes how could I build the module B which is having the dependency on module A and C.In my opinion I need to define the version of module A and C explicitly in the dependency section.
<dependency>
<groupId>com.xyz.engine</groupId>
<artifactId>A</artifactId>
<version>4.0.0-00005</version>
</dependency>
From my module POM I am calling my parent POM and In jenkins job I am giving
versions:set -DnewVersion=4.0.0-${BUILD_NUMBER} for versioning purpose if I explicity define the version of A module then it is also passing the same value to the Parent POM and searching for it which is not avilable.Below is my module POM file
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.truvenhealth.analyticsengine</groupId>
<artifactId>AnalyticsEngine</artifactId>
<version>4.0.0-00002</version>
</parent>
<artifactId>LicenseVerifier</artifactId>
<name>LicenseVerifier</name>
<packaging>jar</packaging>
<dependencies>
<!-- Modules dependencies -->
<dependency>
<groupId>com.xyz.engine</groupId>
<artifactId>Common</artifactId>
<version>4.0.0-00007</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</dependency>
<!-- External dependencies -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.verhas</groupId>
<artifactId>license3j</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<!-- Plugin configurations inherited from the parent POM -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
it is taking the same value for Parent POM which I assigned to Common module .I am keeping the Parent POM in separate repository it should not take the same value it should only take that value which I am defining for the Parent POM and it should download it from their and provide all the values to module POM and the build should be created for module LicenseVerifier with different version.
If you have a multi module build which looks like this:
root (pom.xml parent of all modules)
+---- module-a (pom.xml)
+---- module-b (pom.xml)
+---- module-c (pom.xml)
+---- module-d (pom.xml)
To build a module separately you can do this via Maven like this:
mvn -pl module-a clean package
This will build the module-a only and get the dependencies of other modules from the remote repository. Or you can enhance that like this:
mvn -pl module-a -amd clean package
where the option -amd means --also-make-dependents. If a developer needs a particular state you can do this by a mvn install first and afterwards only build the module you would like to build.
A very important thing in relationship with multi module builds is to have the same version for all modules and the parent. So dependencies between those modules is no problem.
Starting with Maven 3.2.1 you can define the version via properties.
A simple change to prevent Maven from emitting warnings about versions
with property expressions. Allowed property expressions in versions
include ${revision}, ${changelist}, and ${sha1}. These properties can
be set externally, but eventually a mechanism will be created in Maven
where these properties can be injected in a standard way. For example
you may want to glean the current Git revision and inject that value
into ${sha1}. This is by no means a complete solution for continuous
delivery but is a step in the right direction.
Furthermore during development i would prefer the SNAPSHOT versions which the cleanup in the repository manager simpler. So in essence i don't any need to separate the modules which logicaly belong together.
Apart from that if you use the same version within your multimodule build you can use things like this: ${project.version} to define the version of a dependency which is part of the reactor.

In multimodule maven project, how to execute proper goal for each module?

I have three maven projects in my prototype:
bookman (main project)
|-- bookman-back-lend (module, simple service app)
\-- bookman-front-web (module, simple web app)
This is very simple example book lending app (for library or something) to learn various technologies and all of that.
Problem is that I can't make parent pom to execute goals in module poms.
Calling mvn clean package wildfly:deploy in any module individually works without problem. It compiles, deploys, war is replaced, Wildfly 8 does its thing, etc. But if I call parent POM with mvn clean package (I'm not even sure what to call...), it doesn't do much - certainly it does not deploy to wildfly any of modules. Wildfly does not budge. In parent POM, calling mvn clean package wildfly:deploy does not work, of course.
Here is parent pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Main BookMan superproject.</name>
<description>Main BookMan superproject.</description>
<modules>
<module>bookman-front-web</module>
<module>bookman-back-lend</module>
</modules>
</project>
And module POMs, with some stuff cut out for brevity (I assume they aren't related to my problem).
bookman-front-web/pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bookman-front-web</artifactId>
<packaging>war</packaging>
<name>Frontend project - webpage.</name>
<description>Web page to handle lending books.</description>
<parent>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
...
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
...
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package wildfly:deploy</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn clean package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
And bookman-back-lend/pom.xml:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bookman-back-lend</artifactId>
<packaging>war</packaging>
<name>Backend project - lending.</name>
<description>Business logic to handle books, users and act of lending.</description>
<parent>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
...
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
...
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package wildfly:deploy</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn clean package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
It should be possible to have customized goals to execute for each module individually, right? Right? Otherwise those aggregate POMs are rather useless.
Unfortunately, questions like this suggest that maven goal executed on parent POM is exactly same goal to execute on child POMs. Who thinks up something like that? What if I need completely different goals in each child module?
All answers I found are few years old, maybe... just maybe... sanity prevailed and maven now allows something like that? After all, all information neccessary to do this should be accessible, like defaultGoal tag.
Problem was solved in radical way. Maven is dead, long live Gradle.
I was tinkering with it last few days and I already moved prototype to point where it actually works. While there is still tons of work left, I already know I will not be looking back.
So, yeah... if you have requirements that cannot be done easily or at all with Maven for various reasons, then consider Gradle. In fact, consider Gradle anyway.

Updating versions in multi-module Maven project

I am trying to release our multi-module Maven project and I can't figure out how to properly update the version numbers in our POMs.
The maven release plugin doesn't update versions in the parent POM dependencyManagement section - that seems to be a known bug.
I tried to use the versions plugin, but I have two issues with that:
the plugin only updates my toplevel POM; probably because the multi-module project POM is not the paremt POM of the modules's POMs
more significantly, how can I mimic the behavior of the release plugin to automatically compute the versions to set (e.g. 1.0.0) from the current version in the POMs (e.g. 1.0.0-SNAPSHOT)... without giong into perl scripting?
Any suggestion that doesn't involve scripting, and doesn't require me to declare dependency versions outside of dependencyManagement would be very appreciated!
Here are the 4 POMs (module POMs in their module subdirectory):
============ pom-parent.xml ==================
<?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.acme</groupId>
<artifactId>dummy-parent</artifactId>
<version>2.5</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
============= multi-module pom: pom.xml ================
<?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>
<parent>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>multi-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<scm>
<developerConnection>scm:svn:https://svnserver.fairisaac.com:8443/nowhere</developerConnection>
</scm>
</project>
============== module1 POM: module1/pom.xml ================
<?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>
<parent>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>../pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
============= Module 2 POM: module2/pom.xml =============
<?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>
<parent>
<groupId>com.acme</groupId>
<artifactId>dummy-parent</artifactId>
<relativePath>../pom-parent.xml</relativePath>
<version>2.5</version>
</parent>
<groupId>com.acme</groupId>
<artifactId>module2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
</dependency>
</dependencies>
</project>
========= Edit ==============
... and it gets better, if I replace the explicit version in the dependencyManagement section with
<version>${project.version}</version>
... maven 3.0.4 fails with an NPE:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.478s
[INFO] Finished at: Fri Apr 12 12:09:08 CEST 2013
[INFO] Final Memory: 9M/120M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare (default-cli) on project multi-
module: Execution default-cli of goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare failed. NullPointerExcep
tion -> [Help 1]
This is not a bug, the first pom.xml you showed is outside your multi-module project. The maven-release-plugin will consider it an external dependency, thus it won't change the version.
But there is a flaw in your design, you have to move the
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
to your multi-module pom.
I'm assuming your idea is to declare the module version in one place and share it among all modules. So, the module version goes into the multi-module pom dependencyManagement.
Usually external poms like this are used to put company information, repository paths, things like this, and they are shared by many different projects.

Resources