1

I have a parent pom like below.

<?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.parent.pom</groupId>
    <artifactId>parent-pom</artifactId>
    <version>1.0.0</version>
    <name>parentpom</name>
    <packaging>pom</packaging>
    <properties>
        <parentversion>1.0.0</parentversion>
    </properties>
</project>

And my child pom is like .

<?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.parent.pom</groupId>
    <artifactId>parent-pom</artifactId>
    <version>${parentversion}</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
    <groupId>com.child.pom</groupId>
    <artifactId>child-pom</artifactId>
    <version>2.0.0</version>
    <name>Childpom</name>
    <packaging>pom</packaging>
</project>

My folder structure is like

POM-test(root folder having parent pom) --- child(having the child pom)

when i ran mvn clean install in my child am getting the exception like below.

ERROR] The build could not read 1 project -> [Help 1]
ERROR]
ERROR]   The project com.child.pom:child-pom:2.0.0 (C:\Users\kk\Desktop\POM-Test\child\pom.xml) has 1 error
ERROR]     Non-resolvable parent POM: Could not transfer artifact com.parent.pom:parent-pom:pom:${parentversion} from/to central (http://repo.maven.apache.org/
maven2): Illegal character in path at index 63: http://repo.maven.apache.org/maven2/com/parent/pom/parent-pom/${parentversion}/parent-pom-${parentversion}.pom 
and 'parent.relativePath' points at wrong local POM @ line 5, column 9 -> [Help 2

Why I am not able to install this pom ?

robin
  • 1,893
  • 1
  • 18
  • 38
  • Post the full error message with the stacktrace. Post the Maven command you ran. Post your directory structure also. – Tunaki Jan 29 '16 at 10:55
  • This should work code-wise, maybe your folder structure or file naming simply isn't right? – user1438038 Jan 29 '16 at 10:58

2 Answers2

1

You can't have a property in <parent> section

Maven need to load parent to set all properties before processing the build (for project Inheritance, etc)

Each project is identified by groupId, artifactId and version. In parent section, you need to set these 3 properties to identify the parent explicitly.

For updating the version easily over all your pom, consider using release:update-versions plugin or versions:set goal

Edit: See this other thread about similar problem: Maven project version inheritance - do I have to specify the parent version?

Community
  • 1
  • 1
Prim
  • 2,880
  • 2
  • 15
  • 29
  • If am using the relativePath then it should work ideally – robin Jan 29 '16 at 11:12
  • @robin It works or not, depending on maven version (worked on older version), because maven is not designed to support such a configuration – Prim Jan 29 '16 at 11:16
1

Starting with Maven 3.2.1 you can use properties ${revision}, ${sha1} and ${changelist} in your versions if you really like.

So you can use it like this:

  <groupId>com.soebes.examples.j2ee</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}</version>
  <packaging>pom</packaging>

  <modules>
    <module>child</module>
  </modules>

and you can use it in childs like this:

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.soebes.examples.j2ee</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>
  </parent>

  <artifactId>child</artifactId>

Now you have to call Maven like this:

mvn -Drevision=1.2.3-SNAPSHOT clean package
mvn -Drevision=1.2.3 clean package

You can take a look for a full example here.. If you need to change your version i would recommend to use versions-maven-plugin to change the versions within your build to whatever version you like (for example in your CI solution) or in making releases you can use the maven-release-plugin to handle this. And for development you should use something like: 1.2.3.45-SNAPSHOT (emphasize -SNAPSHOT) to identify being under current development.

Furthermore the given <relativePath>../pom.xml</relatvePath> is not needed cause it's the default so it can be omitted.

Very important. If you have a structure like you described the child should always have the same version as the parent (in case of a multi module build like the given example) or the parent (corporate pom) should be a separate project which means also a separate git repository. And furthermore for an other explanation take a look here: https://stackoverflow.com/a/35057833/296328

Community
  • 1
  • 1
khmarbaise
  • 92,914
  • 28
  • 189
  • 235