Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven version with a property

I have big Maven (Tycho) project witch about 400 plug-ins.

We have specified version of application in each POM file.

Is there a way how to specify the version for all POM:s only on one place?

I would expect some think like:

<properties> <buildVersion>1.1.2-SNAPSHOT</buildVersion> </properties>  ....  <version>${buildVersion}</version> 

We have parent pom.xml:

<modelVersion>4.0.0</modelVersion> <groupId>company</groupId> <artifactId>build.parent</artifactId> <version>1.1.2-SNAPSHOT</version> <packaging>pom</packaging> 

Then in each POM is reference to parent POM:

<parent>   <artifactId>build.parent</artifactId>   <groupId>company</groupId>   <relativePath>../build.parent/pom.xml</relativePath>   <version>1.1.2-SNAPSHOT</version> </parent>  <modelVersion>4.0.0</modelVersion> <groupId>company</groupId> <artifactId>artifact</artifactId> <version>1.1.2-SNAPSHOT</version> <packaging>eclipse-plugin</packaging> 
like image 728
Jan Pešta Avatar asked Oct 01 '13 18:10

Jan Pešta


People also ask

What are Maven properties?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

What happens if you don't specify a version in Maven?

Maven won't allow any other either. Build will fail if version is not found.

Where are Maven properties defined?

Maven Settings Properties. You can also reference any properties in the Maven Local Settings file which is usually stored in ~/. m2/settings. xml.


1 Answers

If you have a parent project you can set the version in the parent pom and in the children you can reference sibling libs with the ${project.version} or ${version} properties.

If you want to avoid to repeat the version of the parent in each children: you can do this:

<modelVersion>4.0.0</modelVersion> <groupId>company</groupId> <artifactId>build.parent</artifactId> <version>${my.version}</version> <packaging>pom</packaging>  <properties> <my.version>1.1.2-SNAPSHOT</my.version> </properties> 

And then in your children pom you have to do:

    <parent>       <artifactId>build.parent</artifactId>       <groupId>company</groupId>       <relativePath>../build.parent/pom.xml</relativePath>       <version>${my.version}</version>     </parent>      <modelVersion>4.0.0</modelVersion>     <groupId>company</groupId>     <artifactId>artifact</artifactId>     <packaging>eclipse-plugin</packaging>      <dependencies>         <dependency>             <groupId>company</groupId>            <artifactId>otherartifact</artifactId>               <version>${my.version}</version> or            <version>${project.version}</version>         </dependency>     </dependencies> 

hth

like image 132
Frederic Close Avatar answered Sep 20 '22 17:09

Frederic Close