Propagating properties from parent pom.xml to grandchild - java

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.

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.

How to get the module name from pom in java

I have a requirement in which based on module names, I need to do some operations.
How to fetch the modules name from pom.xml in a java class.
My pom.xml has like below :
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>name1</module>
<module>name2</module>
</modules>
I saw we can get artifactId and version as below from a property file. Here module is a list how to get the modules names from the list. Any help would be appreciated.
version=${project.version}
artifactId=${project.artifactId}
Thanks
You could
Define the names as Maven properties.
Define a text file in resources which uses these properties.
Process this resource file with Maven.
Read from the resource file in your jar/war.

Maven multi module jenkins project

I've a multi-module maven application that we need build through Jenkins.
Project structure is like:
a.xml
<artifactId>parent-1</artifactId>
<modules>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
lookup-1.xml
<artifactId>lookup-1</artifactId>
<parent>
<groupId>com.lookup</groupId>
<artifactId>parent-1</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<name>lookup-1</name>
lookup-2.xml
<artifactId>lookup-2</artifactId>
<parent>
<groupId>com.lookup</groupId>
<artifactId>parent-1</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<name>lookup-2</name>
now,
mvn clean install -f a.xml works fine. For this I created one Jenkins job.
Now, same as a.xml, I've another project with b.xml. b.xml has the same code as of a.xml except different Ids. So, I've created another jenkins job for b.xml.
Both jobs work fine. But, now I want to build both these project from single Jenkins job based on which project we commit in Git. For, this I want to have a new project(pom.xml) and where I want to put both a and b under modules tag. Like this:
pom.xml
<name>combined_project</name>
<artifactId>combined_project</artifactId>
<modules>
<module>a</module>
<module>b</module>
</modules>
a.xml
<artifactId>parent-1</artifactId>
<name>a</name>
<parent>
<artifactId>combined_project</artifactId>
</parent>
<modules>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
But, its not working for me. I'm getting following exception in maven:
Child module D:\....\a of D:\....\pom.xml does not exist
Maven not able to find child module.
I've following project structure:
project
|_ lookup-1
|_ lookup-1.xml
|_ lookup-2
|_ lookup-2.xml
|_ a.xml
|_ b.xml
|_ pom.xml
Any hint?
a.xml has 2 sub modules lookup-1 and lookup-2. By default maven is looking for the sub modules in the nested maps. So you need to have the following directory structure:
<parent>
|
+- a/pom.xml
| |
| +- lookup-1/pom.xml
| |
| +- lookup-2/pom.xml
|
+- b/pom.xml
|
+- pom.xml
Alternative is to change the parent into
<name>combined_project</name>
<artifactId>combined_project</artifactId>
<modules>
<module>a</module>
<module>b</module>
<module>lookup-1</module>
<module>lookup-2</module>
</modules>
Maven follows a project structure and each <module> will contain a pom.xml file. As you have explicitly stated the pom file in command line that file will be selected as the pom.xml for that project. However, for every submodule that is listed maven will search for the exact file named "pom.xml" for each submodule.
From the folder structure provided, it can be inferred that a.xml can act as a pom file when explicitly specified in command-line but it cannot act as a module as it does not have a dedicated folder with pom.xml.
A maven project in general has the following structure:
archetype-id(folder)
pom.xml(packaging: pom)(with one module module-artifact-id)
module-artifact-id(folder)
pom.xml
If you are attempting to build only the module that has been updated then the module that is not checked in recently will not be part of the newly created archive. But if you still would want to do it for some "special" reason. The following method might be the one you are looking for:
(I wouldn't recommend it -- as it is messy and goes against conventions)
Have two sub-modules in a single project and also Have two .xml files (pom files to be specified in commandline). In one xml file lets say "a.xml" "module-1" is to be specified as a module and in "b.xml" "module-2" is to be specified as a module.
Using the currently modified module invoke the build on that pom file. Example, if module-1 is modified specify a.xml in the command line as the pom.xml.
Please note that I have not attempted it but if it is possible this is one of the ways I can think of by which we can accomplish what you are looking for.
Reference for passing parameters : Pass a dynamic parameter in Jenkins build
Two possibilities :
Either you need to fix the path of your submodules in the modules section of the parent pom.
While creating a jenkins job you will have to tell jenkins to clone submodules as well. when I started with jenkins, I faced similar problem. The solution was to enable "Recursively update submodules" checkbox under Advanced scm behaviors in Jenkins UI.

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