Problem passing version on Maven via Property

Asked

Viewed 82 times

0

I am trying to make a modification to a project to allow you to pass a Property and generate the package with the version that was passed. Ex: mvn package install -Dversion.app=0.0.1 But I have a problem when it comes to generating project B that has two dependencies of project A (two modules). Project A generates the artifacts and installs in the . m2, but when I run the same command for project B it cannot find the dependencies. In fact, for what I identified, he tries to look the wrong way (/.m2/Repository/br/project/${version.app}), recognizing the Property as a string, but in the project A works perfectly.

Anyway, my problem is this, I need to use this command in both projects and manage to pass the value of the Property: mvn package install -Dversion.app=0.0.1

Project A:

<groupId>br.projeto</groupId>
<artifactId>projeto-a</artifactId>
<packaging>pom</packaging>
<version>${version.app}</version>
<name>Projeto A - Parent</name>

<modules>
    <module>core-model</module>
    <module>core-ejb</module>
    <module>web-api</module>
</modules>

Project B:

<groupId>br.projeto</groupId>
<artifactId>projeto-b</artifactId>
<packaging>pom</packaging>
<version>${version.app}</version>
<name>Projeto B - Parent</name>

<modules>
    <module>outro-modulo</module>
    <module>outroweb-api</module>
</modules>

 <dependencies>
    <dependency>
        <groupId>br.projeto</groupId>
        <artifactId>core-model</artifactId>
        <version>${version.app}</version>
    </dependency>
    <dependency>
        <groupId>br.projeto</groupId>
        <artifactId>core-ejb</artifactId>
        <version>${version.app}</version>
    </dependency>
</dependencies>

Module - outroweb-api of Project B:

<artifactId>outroweb-api</artifactId>
<parent>
    <groupId>br.projeto</groupId>
    <artifactId>projeto-b</artifactId>
    <relativePath>../pom.xml</relativePath>
    <version>${version.app}</version><!--Isso não é permitido-->
</parent>

<packaging>war</packaging>

    <dependencies>
    <dependency>
        <groupId>br.projeto</groupId>
        <artifactId>core-model</artifactId>
        <version>${version.app}</version>
    </dependency>
    <dependency>
        <groupId>br.projeto</groupId>
        <artifactId>core-ejb</artifactId>
        <version>${version.app}</version>
    </dependency>
  <dependencies>

Finally,

1 answer

1

You could use the release commands Maven to update the versions, the way you’re doing it won’t work.

The reason for not compiling and error is that only with the variable it is not possible to locate the parent version of your artifact.

To update both the parent project and the sub-modules, you can use this command:

mvn release:update-versions -DautoVersionSubmodules=true

With this a prompt will be presented for input of the new version.

In case you’re running this way non-interactive, you can also specify the version via command line:

mvn --batch-mode release:update-versions -DdevelopmentVersion=1.2.0-SNAPSHOT

Remembering that you can also specify the release version using the -DreleaseVersion=1.2 for example.

More details in the documentation.


I forgot to mention, using these commands you do not need variables, Maven already understands where are the versions in pom.xml and exchange without any problem :)


Since you want to have a dynamic Parent version, you can use a revision since Maven 3.5.0, which is called Maven CI Friendly Versions.

With this feature you can execute the command as follows:

mvn -Drevision=1.0.0-SNAPSHOT clean package

And in the pom of its modules would be like this:

<parent>
  <groupId>meu.lindo.modulo</groupId>
  <artifactId>modulo-pai</artifactId>
  <version>${revision}</version>
 </parent>

If still with this feature you are not able to accomplish the expected, the only solution would be to create a script or tool to interpret the pom.xml and exchange as you wish.

  • Can you explain to me why only with the variable it is not possible to find the parent pom in the case of project B? The goal is that I do not do what the command did (scanner all pom and replace), but rather be able to do as I did in the first project a, pass to Property/variable and generate both Ear and submodules in the informed version.

  • @L. Luan And what would that help? The goal of versioning is to keep all modules in the same version, the release command performs exactly this, changes the version of the parent module, through the desired number, updates the dependencies of the submodules to the desired version, and also updates the related artifacts in pom to the new version

  • I added a few more details in the reply, another way since you want to have the version of pom variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.