1

I have a maven project which I do get the new project version from user with:

Current Version = 1.0.0
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>versions-maven-plugin</artifactId>
    <version>2.7</version>
    <executions>
        <execution>
            <phase>validate</phase>
                    <goals>
                        <goal>set</goal>
                    </goals>
            </execution>
        </executions>
    </plugin>
Current Version = 2.0.0

and after that I called my own custom plugin, which runs set of calculations and appends an String to the version

Current Version = 2.0.0
<groupId>mygroup</groupId>
    <artifactId>my artifact</artifactId>
    <version>1.0.0</version>
    <executions>
       <execution>
        <phase>process-sources</phase>
            <goals>
                <goal>validate</goal>
            </goals>
        </execution>
    </executions>
Current Version = 2.0.0-AddedString

but when I run other plugins, for example:

<groupId>com.github.ekryd.echo-maven-plugin</groupId>
    <artifactId>echo-maven-plugin</artifactId>
        <executions>
        <execution>
            <id>end</id>
            <goals>
            <goal>echo</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
            <message>${project.version}</message>
            </configuration>
     </execution>
</executions> 

which gives me the result of : "1.0.0" which should be "2.0.0-AddedString"

but why? and how to fix this? I need all plugins use the new version and work with that.

frdsprtrdr
  • 384
  • 1
  • 2
  • 11

1 Answers1

1

You need separate Maven runs for that.

If you run something like mvn versions:set -DnewVersion=2.0.0 my:plugin, then my:plugin will see the version as it was before starting the command, not the 2.0.0 that was set in between.

So when you have goals that change the POM, you need to call them in separate Maven runs, i.e. first mvn versions:set -DnewVersion=2.0.0 and then mvn my:plugin.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks JF, is there anyway to run a sequence of "maven runs"? for example putting all my maven runs in a file or a solution that I can run multiple "maven run"s. – frdsprtrdr Feb 17 '20 at 08:00
  • Usually, you use a build server like Jenkins for that. – J Fabian Meier Feb 17 '20 at 08:39
  • I have exactly this problem, I do not have any build server and it makes the task a bit complicated, do you think it is wise to have multiple runs in a shell script and run it by exec plugin?(I guess not because the exec plugin is also running in current run) – frdsprtrdr Feb 17 '20 at 08:42
  • can I use GitLab CI instead of Jenkins(i think it is), I want to see how is it possible and what do I need to do then, any links maybe? and is it feasible if I am not doing continues development and I do not have git flow and I am not tagging my versions in git, I just commit t5he code in git and artifacts in Archivia – frdsprtrdr Feb 17 '20 at 08:46
  • 1
    Probably you can do that, but there are different things to consider. 1. If you do this project professionally and not just as a private project, it is really useful to set up a build server first. 2. You should think hard if you really need that custom logic that you put in your custom plugin. More often than not, you try to reinvent the wheel or you are trying to do something against the Maven philosophy. – J Fabian Meier Feb 17 '20 at 08:49
  • 1
    Sorry, saw your last comment after I wrote mine. Gitlab should be able to build the pipelines you need for your Maven build, but I have never used it. – J Fabian Meier Feb 17 '20 at 08:49
  • yes it is a industrial project and I try to find best possible solution, then my missing chain in this problem is build server, yes? – frdsprtrdr Feb 17 '20 at 09:33
  • IMHO yes. With a build server, you can automate your build in a standardized environment. Jenkins is the most popular, but others, including Gitlab, are also feasible. – J Fabian Meier Feb 17 '20 at 10:41