Can you inherit the version from the parent POM in Maven? [duplicate] - java

This question already has answers here:
Maven project version inheritance - do I have to specify the parent version?
(11 answers)
Closed 9 years ago.
I have a bunch of projects like:
project1
project2
project3
........
project111
Each project compiled in jar: project-1.1.1.1.jar, ....
Does it possible in parent folder add pom.xml so I can define version 1 time for all projects?

If you omit <version/> it inherits from the parent. However, the <parent/> element must contain a <version/> for the parent, so the version must occur in every single POM, but only once.

You can do this:
parent pom.xml:
<project>
...
<version>${my-project-version}</version>
...
<properties>
<my-project-version>1.1.1</my-project-version>
</properties>
</project>
child pom.xml:
<project>
...
<parent>
<relativePath>../parent/pom.xml</relativePath>
<version>${my-project-version}</version>
</parent>
...
</project>
At least, this works for me.

Using a property and referencing it works... unless you are using the release plugin to do releases in which case it removes "-SNAPSHOT" from the version and automatically replaces all instances with the real version number - which overwrites any replacement variables you've set. You may be better off just setting it in each POM and using the release plugin to tag, increment, and release your project.

Related

Maven Reference Pom in Child Module By Name

We are using a parent pom that has a child module where we have 2 pom files - the one named pom.xml and other being images-pom.xml.
This is the situation because we are doing some naming changes and for the time being we want to have them both.
In our parent pom we have the following code
<modules>
<module>child</module>
</modules>
<packaging>pom</packaging>
By default it seems that this is looking and trying to build the pom.xml - but in reality we want to use the images-pom.xml Is there any way to achieve this without creating a new module and using profiles?

How does nexus-staging-maven-plugin use <scm> information?

I have a single Git repository that contains several Maven modules, using Maven inheritance and Maven aggregation. That is, in the root directory, there is a parent POM, that defines some modules, each of which use that root POM as their parent.
<project>
…
<groupId>io.example</groupId>
<artifactId>parent</artifactId>
<version>1.2.3-SNAPSHOT</version>
<packaging>pom</packaging>
…
<scm>
<connection>scm:git:https://bitbucket.org/example/foobar.git</connection>
<developerConnection>scm:git:https://bitbucket.org/example/foobar.git</developerConnection>
<url>https://bitbucket.org/example/foobar</url>
</scm>
…
<modules>
<module>foo</module>
<module>bar</module>
</modules>
…
I recently found out that Maven will append the module path to the <scm><url> value for each module (foo and bar above). For example, the foo submodule would get an SCM URL of https://bitbucket.org/example/foobar/foo.
So should each of my modules redeclare the <scm> section, so that the submodule POMs have the same SCM URL as the parent POM? How does the Nexus Staging Maven Plugin use this SCM information, anyway?
I have also cross-posted this at Sonatype.
Regarding your initial question in the title: It does not use it given the code provided at github. When you search for 'scm' in all .java files you have zero hits. Of course they could do some weird tricks like building the String like "s" + "c" + "m" or it is hidden in some third party dependency or...
Still I think besides an offical answer, this is the best any outsider can tell.

Why sometimes dependency not contain version property in pom.xml?

I am a newbie of Maven, currently reading Hadoop source code, and found something interesting in some pom.xml files:
Some of the dependency node do not contain version node at all.
Question: why is it like this?
for instance, this pom.xml.
Because specific version of dependency in parent pom.xml file
https://github.com/apache/hadoop/blob/trunk/pom.xml
Reference: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
As I commented at first, a pom file can have a parent (via inheritance) and such a parent may provide some governance and harmonization across all of its children. A classic case is to provide versioning for certain dependencies via a dependencyManagement section.
is used by POMs to help manage dependency information across all of its children. If the my-parent project uses dependencyManagement to define a dependency on junit:junit:4.0, then POMs inheriting from this one can set their dependency giving the groupId=junit and artifactId=junit only, then Maven will fill in the version set by the parent. The benefits of this method are obvious. Dependency details can be set in one central location, which will propagate to all inheriting POMs.
The mentioned pom has indeed a parent pom:
<parent>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-project-dist</artifactId>
<version>3.0.0-SNAPSHOT</version>
<relativePath>../../hadoop-project-dist</relativePath>
</parent>
Which in chain has another parent pom file which defines several dependencies as part of its dependencies management section.
If you really want to check the effective (merged) pom your build is using, you could run:
mvn help:effective-pom -Doutput=effective-pom.xml
And the maven-help-plugin will produce an additional pom as specified by the command above, merging the current pom file and all of its anchestors.
In Maven you can inherit from parents folder in order to merge or inherit some properties. This can be the version of the modules. Usually you have a "super" POM in the root folder of your project and you put there all the commons dependencies in order to controll them in an easier way. I.e. If you must change one module version, you only need to change in the "super" POM and not in each POM inside each subfolder that need it. If you need more information about POM inheritance the documentation has a couple of useful examples.
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance

Propagating properties from parent pom.xml to grandchild

I have a project whose structure goes like this
sw
|-aim-pom.xml
|-website
|-pom.xml
|-package
|-pom.xml
My aim-pom.xml looks like this
<packaging>pom</packaging>
<properties>
<product>aim</product>
</properties>
<modules>
<module>website/package</module>
</modules>
Basically I cant seem to get the value of the property 'product' in website/package/pom.xml where I am doing some conditional logic based on it. What am I doing wrong here?
Thanks in Advance.
Do a "mvn help:effective-pom" in the "package" module to see if the property is there. If not, make sure that that module specifies aim-pom.xml (or one of its children) as its parent POM. Remember that in Maven, aggregation (modules build sub-modules via the reactor) and inheritance (POMs have other POMs as parents) are separate concepts.

Specify different groupId for parent POM than children

I would like to create a Maven Parent POM file with a groupId of com.company.maven, which gives its children a default groupId of com.company. Is this possible? If so, how can I accomplish this?
Just create the parent pom like:
<project...>
<groupId>com.company.maven</groupId>
<artifactId>parent</artifactId>
<version>..</version>
...
</project>
and define in the child pom:
<project...>
<parent>
<groupId>com.company.maven</groupId>
<artifactId>parent</artifactId>
<version>..</version>
</parent>
<groupId>com.company</groupId>
</project>
In other words it's possible to do so but i wouldn't do it cause i would have named the parent "com.company" whereas the child "com.company.maven". I would compare the groupId with the java package name which represent folders so your idea of setting the groupId doesn't make sense.
I don't think this is possible. Also, it flies in the face of the Maven Guide to Naming Conventions, which recommends that (if anything), the child modules should append to the parent groupId, not vice versa.
Like a java class, that doesn't know "its" subclasses, a parent pom does not know its child poms. So the parent pom will not be able to set or overwrite attributes of it's children.

Categories

Resources