Maven version not getting updated in the .POM file - maven

I am using Maven for building my code. I created module based maven structure like below
Parent-POM
Sub-Parent1
SP1_Child1
SP1_Child2
SP1_Child3
Sub-Parent2
SP2_Child1
SP2_Child2
SP2_Child3
All my module versions, and external dependency versions are maintained in the Parent POM. Everything works fine when I do a complete mvn install, but when I try to build one sub module like SP1_Child1, then the build fails, because it is not able to identify the version of its dependencies. I checked the maven repository in my local machine, and all my modules were installed, but the .POM files do not have the version numbers. This is probably because the where the mvn install on the Parent POM is not replacing the ${module.version} with the actual version for the child modules.
Parent-POM
<project ..>
<groupId>the.company.project</groupId>
<artifactId>Parent-POM</artifactId>
<version>1.0-SNAPSHOT</version>
...
<properties>
<module.version>1.0</module.version>
</properties>
</project>
SP1_Child1
<project ..>
<parent>
...
</parent>
<groupId>the.company.project</groupId>
<artifactId>SP1_Child1</artifactId>
<version>${module.version}</version>
...
</project>
How how can my mvn install update the versions in the .POM files in the maven repository? Or how can I run one of my sub-modules without any version errors?

The default layout of a child pom should look like this.
<project ..>
<parent>
<groupId>the.company.project</groupId>
<artifactId>SP1_Child1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>the.company.project</groupId>
<artifactId>SP1_Child1</artifactId>
...
</project>
But you child should not define version separately only in the parent element without using a property. The version is automatically inherited to the child module from the parent. If you have the same group you also don't need to define the group in child. You can use it like this:
<project ..>
<parent>
<groupId>the.company.project</groupId>
<artifactId>SP1_Child1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>SP1_Child1</artifactId>
...
</project>

Taking the SP1_Child1 version from parent will be very annoying for you because it will force you to install a new version of the parent for any new version of the SP1_Child1 project.
There are 2 different possible situations :
You want to be able to manage different project, with different lifecycles.
Then you specify a version in the SP1_Child1 project, and you specify the version of SP1_Child1 to be used by other projects in the parent POM (in this case, the 2 values can be different).
Your application is monolithic, even if it is organized in different modules for convenience. Then in this case, the best is to do what khmarbaise advises, keep one version for all your projects, and inherit the version from the parent.

Related

Maven release plugin and using a single version in the parent pom

I have a multi-module maven project.
I hold the version in one location in the parent pom.xml as a property
and then use this in the parent pom.xml:
<project>
<artifactId>autoav</artifactId>
<version>${autoAvVersion}</version>
...
<properties>
<autoAvVersion>19.3.24-SNAPSHOT</autoAvVersion>
</properties>
</project>
And use this in the child module :
<project>
<groupId>com.mycompany.analysis</groupId>
<version>${autoAvVersion}</version>
<artifactId>analysisCore</artifactId>
<parent>
<groupId>com.mycompany.analysis</groupId>
<version>${autoAvVersion}</version>
<artifactId>autoav</artifactId>
<relativePath>../autoAV</relativePath>
</parent>
...
</project>
Maven release plugin, after running prepare+perform on the child project only (just for testing one module), changed my child pom version from
<version>${autoAvVersion}</version>
to
<version>19.3.25-SNAPSHOT</version>
So i suppose the release plugin did not think of cases using a single point version.
(By the way if i omit the version tag in the child, so it effectively gets it from the parent, the version tag without the property [19.3.25-SNAPSHOT] is still added to the child
Is there any solution to this ?

Update parent version and artefactId

I want to dynamically update my parent version and artefactId.
Let's say I have the following pom.xml
<parent>
<groupId>com.company</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>my-child</artifactId>
<version>1.0-SNAPSHOT</version>
Is there any possibility to update (set) parent artefactId/version.
I.e. I can set a child version via versions plugin
mvn org.codehaus.mojo:versions-maven-plugin:2.7:set -DnewVersion=1.2.3-SNAPSHOT
Is there something like it for parent updating?
You can use versions:update-parent to update your parent pom to the latest version. I never heard of a plugin to change the artifactId.
I would be interested to know why more than parent pom is required.

maven clean install still loads old version of dependency when updating parent pom file

I am using Maven for building my code. I created module based maven structure like below:
Parent POM
Child POM
Parent POM:
<project ..>
<groupId>the.company.project</groupId>
<artifactId>Parent-POM</artifactId>
<version>1.0-SNAPSHOT</version>
...
<properties>
<module.version>1.0</module.version>
</properties>
</project>
Child POM:
<project ..>
<groupId>the.company.project</groupId>
<artifactId>Child-POM</artifactId>
<version>${module.version}</version>
...
</project>
When I update the module version from 1.0 to 2.0 in Parent POM, and I run command mvn clean install -Ponline -Dmaven.test.skip=true, I find it still compiles the 1.0 version of module. However, when I run the mvn command again, I find it starts to use the new 2.0 version, I wonder if there is any cache for mvn?

Does incrementing depending module version should increment parent module version?

I have parent module A that has module B as its dependency. If I increment version of B for just a simple bug fix, that does not change any API or break anything, do I still need to increment version of A?
Example:
A:1.10.0 --> B:2.5.0
fixing B to 2.5.1
In this case, A on Maven depends on older version of B, therefore, it seems it make sense to increate the version of A, too; so to have:
A:1.10.1 --> B:2.5.1
even though we didnt change anything in A.
Is this correct?
Yes, you are correct. If you change version of any of the dependency, you will need to build and release parent project again. This will create the new released version of your parent module A.
(Here, I am assuming that parent module A packaging type is other than pom. If the packaging type is pom, then its a different story altogether.)
Edited:
When you say parent project, you will declare its packaging as pom. It will have it's own version.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>web-project</module>
</modules>
</project>
You can also refer to some modules (child projects) in your parent pom declaration.
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<artifactId>web-project</artifactId>
<packaging>war</packaging>
Then for submodules there is no need to declare any version for it. Parent version will be carried over to the child project. You can override version in the child project but that's not a common practice. So your parent pom version will be incremented automatically when you release it with maven. And this new version will be carried over to child projects (modules) also.
See this - Maven project version inheritance - do I have to specify the parent version?
So parent module A, I was referring to the above case.
And maven repo - can be of two types - local repository and remote repository. Local repo is where maven looks for resolviing dependencies. This is on your machine. If maven doesn't find it there, maven retrieve those depedencies from remote repository.

Warning on using project.parent.version as the version of a module in Maven 3

In maven multi-module projects where I want each of the modules to always keep the same version as the parent, I've typically done something like the following in the module's pom.xml:
<parent>
<groupId>com.groupId</groupId>
<artifactId>parentArtifactId</artifactId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>com.groupId</groupId>
<artifactId>artifactId</artifactId>
<packaging>jar</packaging>
<version>${project.parent.version}</version>
<name>name</name>
Since I started using maven 3.0-alpha-5, I get the following warning for doing so.
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.groupid.artifactId:name:jar:1.1-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. # com.groupid.artifactId:name::${project.parent.version}, /Users/whaley/path/to/project/child/pom.xml
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
I'm curious to know what the real problem with tying a module's version to the parent version is, if any? Or is this a case of a general warning when any expression, regardless of whether it's project.parent.version, is used for the version element.
I'm curious to know what the real problem with tying a module's version to the parent version is, if any? Or is this a case of a general warning when any expression, regardless of whether it's project.parent.version, is used for the version element.
Well, that would be easy to test. Because I was curious, I just did it for you using the following pom:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>com.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mycompany</groupId>
<artifactId>module</artifactId>
<version>${myversion}</version>
<name>module</name>
<url>http://maven.apache.org</url>
<properties>
<myversion>1.0-SNAPSHOT</myversion>
</properties>
...
</project>
And maven is indeed complaining:
[WARNING] 'version' contains an expression but should be a constant. # com.mycompany:module:${myversion}, /home/pascal/Projects/maven-maven3-testcase/module/pom.xml
To be honest, I think that maven is right here, it doesn't make much sense to use a property for the <version> element (at least not for project.version) and it's nice to have maven complaining about it.
And if you want to use the parent pom version in sub-modules, just remove the <version> tag from the child poms, they will inherit the version from the parent. What you are currently doing is unnecessary.
I might be late here to discuss on this. I got a simple solution for this WARNING.
First of all, if you want that all child modules will take same version as parent, then you just remove <version> tag from child POM and as you include <parent> in child POM, that should be there.
In absence of <version> in child POM, it will automatically take Parent POM version.
Now if you want to use property in parent POM version and want to get the same in all child-modules, you can go through as follow.
There is no limitation on using property in <version> part of parent or child POM. But if you use your own xml tag for specifying that or you use your own property, then WARNING comes, (although this is just warning, everything works as expected).
But if you want to get rid of this WARNING, you can follow these steps:
Create <properties> inside POM.xml as below
<properties>
<revision>1.0.0</revision> <!-- Put your version -->
</properties>
In <version> of the POM.xml, put as follow
<version>${revision}</version>
Sample code snippet (for multi-module project):
<groupId>abc.xyz</groupId>
<artifactId>pqr</artifactId>
<!-- <version>1.0.0</version> -->
<version>${revision}</version>
<packaging>pom</packaging>
<description>Parent POM</description>
<properties>
<revision>1.0.0</revision>
</properties>
Note: Instead of <revision>, if you use any other name (for example, <my.version>), you will face that WARNING
Now if you want to pass version during mvn deploy, you can use mvn deploy "-Drevision=1.0.0-SNAPSHOT" and similarly for mvn install also.
Now if above configuration, you want to use as Parent POM, and you want to use same version in all child module, that can also be done. In each child module POM, use below
<parent>
<groupId>abc.xyz</groupId>
<artifactId>Parent</artifactId>
<!-- <version>1.0.0</version> -->
<version>${revision}</version>
</parent>
<groupId>abc.xyz</groupId>
<artifactId>Child</artifactId>
<!-- <version>1.0.0</version> --> <!-- Automatically inherit parent POM version -->
<name>Demo</name>
For reference, you can go through maven multi module setup
It seems that the warning is correct. See MNG-4717: "the pom that gets deployed will not have the property value resolved, so
anyone depending on that pom will pick up the dependency as being the
string uninterpolated with the ${ } and much hilarity will ensue in your
build process." "However, if one uses flatten-maven-plugin the deployed pom gets a resolved value."

Resources