views:

31

answers:

1

Trying to create a multi module maven project in eclipse.

Parent 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/maven-v4_0_0.xsd"&gt;
  <modelVersion>4.0.0</modelVersion>
  <groupId>parent</groupId>
  <artifactId>proj1</artifactId>
  <version>${myversion}</version>
  <packaging>pom</packaging>
  <properties>
   <myversion>1.0.0</myversion>
  </properties>
  <modules>
   <module>child1</module>
   <module>child2</module>
  </modules>
</project>

Child-Module 1 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/maven-v4_0_0.xsd"&gt;
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>proj1</artifactId>
    <groupId>parent</groupId>
    <version>${myversion}</version>
  </parent>
  <groupId>parent</groupId>
  <artifactId>child1</artifactId>
  <version>${myversion}</version>
</project>

Child Module 2 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/maven-v4_0_0.xsd"&gt;
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>proj1</artifactId>
    <groupId>parent</groupId>
    <version>${myversion}</version>
  </parent>
  <groupId>parent</groupId>
  <artifactId>child2</artifactId>
  <version>${myversion}</version>
  <dependencies>
   <dependency>
    <groupId>parent</groupId>
    <artifactId>child1</artifactId>
    <version>${myversion}</version>
    <type>jar</type>
    <scope>compile</scope>
   </dependency>
  </dependencies>
</project>

If I use mvn clean install on command line from the parent folder I can build find. All the projects are build successfully.

But in eclipse I keep getting an error for Child Module 2 pom.xml

Description Resource Path Location Type
parent:child1:1.0.0:jar Missing:
----------
1) parent:proj1:pom:${myversion}
----------
1 required artifact is missing.

for artifact: 
  parent:proj1:pom:${myversion}

from the specified remote repositories:
  central (http://repo1.maven.org/maven2, releases=true, snapshots=false)
 pom.xml /child2 line 1 Maven Problem

I need to achieve 2 things 1. have a maven property define the version in the parent pom file. 2. use this property to define the version in all the child modules.

What should I do? I am stuck. Please help.

I am using Eclipse Galileo with m2Eclipse

+1  A: 

This can't work, you can't get the version of the parent to use from the parent (and actually, properties don't get substituted in the parent element, see MNG-624). So you need to hard code the version and your parent POM should be:

<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"&gt;
  <modelVersion>4.0.0</modelVersion>
  <groupId>parent</groupId>
  <artifactId>proj1</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <modules>
   <module>child1</module>
   <module>child2</module>
  </modules>
</project>

And in your child POM 1. you need to hardcode the version in /project/parent/version 2. you don't need to specify the <version> (you inherit it) 3. use the ${project.version} property in dependencies.

<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"&gt;
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>proj1</artifactId>
    <groupId>parent</groupId>
    <version>1.0.0</version>
  </parent>
  <groupId>parent</groupId>
  <artifactId>child2</artifactId>
  <!--version>${myversion}</version--> <!-- Inherited -->
  <dependencies>
   <dependency>
    <groupId>parent</groupId>
    <artifactId>child1</artifactId>
    <version>${project.version}</version> <!-- use the built-in property here -->
    <!--type>jar</type--> <!-- This is a default, you can omit it -->
    <!--scope>compile</scope--> <!-- This is a default, you can omit it -->
   </dependency>
  </dependencies>
</project>

Versionless parent elements will be supported in Maven 3.1.

See also

Pascal Thivent
What Pascal said, you cannot use a property reference in /project/parent/version it just isn't supported.
tobrien
Hmm. I have seen this type of setup being used before. Strange you guys say it's not supported. I found this. What is this bug referring to? http://jira.codehaus.org/browse/MNG-2446
andthereitgoes
@andthereitgoes: MGN-2446 is closed, I posted the relevant issue in my answer, and I don't have anything more to add.
Pascal Thivent