How to configure/override Maven property used as parent version in dependencies? - maven

Suppose there are two 3rd-party Maven artifacts with the following POM.XMLs:
artifact1 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>group1</groupId>
<artifactId>artifact1</artifactId>
<version>${artifact1.version}</version>
<packaging>pom</packaging>
<properties>
<artifact1.version>0.0.1-SNAPSHOT</artifact1.version>
</properties>
<modules>
<module>../artifact2</module>
</modules>
artifact2 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>artifact2</artifactId>
<parent>
<groupId>group1</groupId>
<artifactId>artifact1</artifactId>
<version>${artifact1.version}</version>
<relativePath>../artifact1</relativePath>
</parent>
They are built normally using
mvn clean install
Now, if I try to reuse artifact2 in a 3rd 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>group2</groupId>
<artifactId>artifact3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
After executing...
mvn clean install
...the build fails trying to locate group1:artifact1:pom:${artifact1.version}
[ERROR] Failed to execute goal on project artifact3: Could not resolve dependencies for project group2:artifact3:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at group1:artifact2:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for group1:artifact2:jar:0.0.1-SNAPSHOT: Could not find artifact group1:artifact1:pom:${artifact1.version} in central (https://repo.maven.apache.org/maven2) -> [Help 1]
And setting the property in the command-line doesn't work either:
mvn clean install -Dartifact1.version=0.0.1-SNAPSHOT
How can I use artifact2 as a dependency?

<parent> doesn't allow variables. Because in order to get a list of variables Maven first has to build Effective POM and for this it needs to resolve parents.
Even if there is some version of Maven that allowed such module to be installed it's not a working solution in general. Maven's ideology is that all the dependencies (including parent) could be downloaded from a repo and are not necessarily present on local FS/in the reactor. With properties in the parent dependency definition you violate this rule.

Related

Maven build is failing with error could not find artifact

I have a project demo-parent which contains 2 subprojects child1 and child2.
demo-parent is building a pom.
child1 and child2 are building one jar each. And all these are getting uploaded to the artifactory.
demo-parent(also used flatten plugin)
<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.demos</groupId>
<artifactId>demo-parent</artifactId>
<packaging>pom</packaging>
<version>${revision}</version>
<properties>
<revision>1.0-SNAPSHOT</revision>
</properties>
...
</project>
child1 and child 2
<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.demos</groupId>
<artifactId>child1</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.demos</groupId>
<artifactId>demo-parent</artifactId>
<version>${revision}</version>
</parent>
<properties>
<revision>1.0-SNAPSHOT</revision>
</properties>
...
</project>
This project is building and uploading fine. Now when I use this child1 or child2 as a dependency in other projects, it fails with the error:
Failed to execute goal on project maven-webapp: could not resolve dependencies for the project com.demos:maven-webapp:war:1.01: Failed to collect dependencies at com.demos:child1:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for com.demos:child1:jar:1.0-SNAPSHOT: Could not find artifact com.demos:demp-parent:pom:{revision} in artifactory .
I am not getting why it is searching for com.demos:demo-parent:pom:{revision}, even if searching why revision is not getting replaced by 1.0-SNAPSHOT as I have used <revision>1.0-SNAPSHOT</revision> in child1.

Maven multi-module non-resolvable parent

i'm facing a build error in maven, which i'm unable to explain, my project layout is:
root
|--parent
|--project
where root (aggregator) and project both use "parent" as their parent.
i used this layout in another project, which maven is able to build.
however, in this project, if i'm trying to build, i get a failure on non-resolvable parent pom:
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[FATAL] Non-resolvable parent POM for maven.test:maven-test-project:9.9.9-SNAPSHOT: Could not find artifact maven.test:maven-test-parent:pom:9.9.9-SNAPSHOT and 'parent.relativePath' points at wrong local POM # line 11, column 10
if i remove the parent node from the root (aggregator) pom, maven is able to build the project successfully.
if i use an older version of maven (3.3 instead of 3.6) the project is built successfully as well.
here are the poms for all 3 projects:
root
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maven.test</groupId>
<artifactId>maven-test</artifactId>
<version>9.9.9-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Maven Test</name>
<parent>
<groupId>maven.test</groupId>
<artifactId>maven-test-parent</artifactId>
<version>9.9.9-SNAPSHOT</version>
<relativePath>parent/pom.xml</relativePath>
</parent>
<modules>
<module>parent</module>
<module>project</module>
</modules>
</project>
parent
<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>maven.test</groupId>
<artifactId>maven-test-parent</artifactId>
<version>9.9.9-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Maven Parent</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
</project>
project
<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>maven-test-project</artifactId>
<version>9.9.9-SNAPSHOT</version>
<name>Maven Project</name>
<parent>
<groupId>maven.test</groupId>
<artifactId>maven-test-parent</artifactId>
<version>9.9.9-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
</project>
There's a conception issue.
"root" cannot be at the same time a parent and child of the same Maven module "parent".

Versions Maven Plugin update-parent checks online maven

I have 2 maven projects in- parent and child, both having 0.0.1-SNAPSHOT.
The pom of parent:
<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>com.tmn</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
The pom of child:
<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>com.tmn</groupId>
<artifactId>child</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.tmn</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<name>child</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
I updated the version of the parent pom to some release version, say 1.0.0 and did mvn clean install on my parent. So, my maven local repo contains the released pom. Now, I want to update the parent.version and the version of my child pom. I run
mvn versions:update-parent -DparentVersion=1.0.0 -DgenerateBackupPoms=false -Dmaven.repo.local=/path/to/repo
I want it to blindly update the parent.version to the parentversion I supply in my command. But I get this WARNING and it is not updated.
artifact com.tmn:parent: checking for updates
[WARNING] Not updating version: could not resolve any versions
There is a similar issue logged here but I didn't understand the solution to this: https://github.com/mojohaus/versions-maven-plugin/issues/121

Maven insist using local parent rather than remote WITHOUT relativePath mentioned

I have a test project which is based on a parent project. This parent is build beforehand and is available on local artifactory server. The parent project is not available locally and this should stay this way.
As you see I am not using the relativePath element.
Still, when running "mvn clean install -U" I get an error about missing parent.
[ERROR] Non-resolvable parent POM for com.test.example:test:0.0.1-SNAPSHOT: Could not find artifact com.test:projects-parent:pom:0.0.1-SNAPSHOT and 'parent.relativePath' points at wrong local POM # line 6, column 10 -> [Help 2]
The test project pom.xml is:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<parent>
<groupId>com.test</groupId>
<artifactId>projects-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
...
</dependencies>
<build>
...
</build>
</project>
Here is the parent 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<groupId>com.test</groupId>
<artifactId>projects-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
</project>
According to this, the default value for <relativePath> is ../pom.xml, so Maven will look locally first.
If you want to force the lookup to occur in your repository manager, use this trick:
<parent>
<groupId>com.test</groupId>
<artifactId>projects-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!— This forces a lookup against the repo —>
</parent>

Maven hierarchical dependency search

So I have a problem, that is - Maven seems to ignore dependencies, even though they should be visible in hierarchy.
I have following project hierarchy:
parent
--projA
--sub-parent1
----projB
----projC
All levels are linked via <parent> tag.
sub-parent1 has projB and projC declared as modules and no dependencies declared. But projB has a dependency on projA. And building entire sub-parent1 module will not build projA, which is strange, because projB is aware of sub-parent1 and sub-parent1 is aware of parent and it is aware of dependency (projA). But maven do not build it whenever I build entire sub-parent1 or, for example, -pl projB -am clean install.
Any help appreciated.
Edit:
I've created a structure, representing this structure. Try to build sub-parent: dropmefiles.com/5l42j
Edit:
parent pom:
<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>test</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>projA</module>
<module>sub-parent1</module>
</modules>
</project>
projA:
<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>test</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>projA</artifactId>
<version>1.0</version>
</project>
sub-parent1:
<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>test</groupId>
<artifactId>parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>sub-parent1</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>projB</module>
</modules>
</project>
projB:
<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>test</groupId>
<artifactId>sub-parent1</artifactId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>projB</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>projA</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Edit:
Although I had the same solution as in Maven multi module project cannot find sibling module - this problem is different as it deals with not only with multimodule, but with multilevel (several levels of modules) project and person there did not told maven explicitly to build dependencies aswell.
Solution:
So it seems the only solution is - build such multilevel structure from the upper-most level pom, in my case it will be parent (so parent is current dir) and address module you need to build by relative path from the parent. That results in:
mvn -pl sub-parent1/projB -am clean install
The order will be:
[INFO] Reactor Build Order:
[INFO]
[INFO] parent
[INFO] projA
[INFO] sub-parent1
[INFO] projB
So it seems the only solution is - build such multilevel structure from the upper-most level pom, in my case it will be parent (so parent is current dir) and address module you need to build by relative path from the parent. That results in:
mvn -pl sub-parent1/projB -am clean install
The order will be:
[INFO] Reactor Build Order:
[INFO]
[INFO] parent
[INFO] projA
[INFO] sub-parent1
[INFO] projB

Resources