user1638152
user1638152

Reputation: 597

Overriding Maven Properties on command line (Maven 2.2.1)

I have found that when running mvn install and overriding properties defined in the POM on the same command line call, does not update the value of the defined property in the POM that is installed to the local repository which causes the packaged artifact to out of sync with the POM file. For example, consider two modules, module-one and module-two with the following POMs

module-one 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.0</modelVersion>
<groupId>org.myorg.test</groupId>
<artifactId>module-one</artifactId>
<version>1.0</version>

And module-two 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.0</modelVersion>
<groupId>org.myorg.test</groupId>
<artifactId>module-two</artifactId>
<version>1.0</version>
<properties>
    <module.one.version>1.0</module.one.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                </manifest>
              </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.myorg.test</groupId>
        <artifactId>module-one</artifactId>
        <version>${module.one.version}</version>
    </dependency>
</dependencies>

Running mvn install on module-one POM will install module-one:1.0 to your local respository. Now if you edit module-one POM and set the version to 2.0 and run mvn install will install module-one:2.0 to your local repository.

Now running mvn install -Dmodule.one.version=2.0 on module-two pom will result in installing module-two:1.0 to your local repository. However, inspecting the built JAR and opening the manifest file will show the following

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Build-Jdk: 1.6.0_26
Class-Path: module-one-2.0.jar

But the POM that goes with this built jar in the local repository will still reference a dependency on module-one:1.0 as the property will stay as it was originally defined

<properties>
<module.one.version>1.0</module.one.version>
</properties>
...
<dependencies>
<dependency>
        <groupId>org.myorg.test</groupId>
    <artifactId>module-one</artifactId>
    <version>${module.one.version}</version>
</dependency>
 </dependencies>

Is this correct behaviour? Or should the property module.one.version be parsed in a different way?

Thanks

Upvotes: 2

Views: 2688

Answers (1)

kodstark
kodstark

Reputation: 490

Yes, this is how maven works. Because of that I always define default values of properties in pom and override them in settings.xml or command line or profiles. Thanks to that pom in repository has bigger chance to work smoothly for other developer in team.

Upvotes: 1

Related Questions