1

I would like to dynamically assign versions of dependencies of my Maven project (please do not ask why - I know it is not a preferable pattern). As far as I understand I have to create Maven Extension to achieve this as regular plug-in is invoked too late.

So, I have tried to catch Maven events in EventSpy, I have also tried AbstractMavenLifecycleParticipant. Using these I am able to be notified, but how to actually do the change itself - how to make Maven to start working with this new updated version? I guess I somehow need to change dependency version in maven's reactor....but how?

I know there's solution in maven-version-plugin but that will require have it as a 2-step job: first manipulate pom.xml and then run the actual build. But I need to have it done within one maven run.

Any idea, please? Thanks, in advance.

EDIT

Example pom.xml files to illustrate situation

Library module:

  • my-lib pom.xml

    <groupId>foo.bar</groupId>
    <artifactId>my-lib</artifactId>
    <version>1.0.5</version>
    
  • my-app pom.xml

    <groupId>foo.bar</groupId>
    <artifactId>my-app</artifactId>
    <version>2.5.0</version>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        ...
        <dependency>
            <groupId>foo.bar</groupId>
            <artifactId>my-lib</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    <dependencies>
    

Now when I build my-app I need to dynamically assign my-lib version according to the latest released version I find in nexus (I know how to get correct version - let's say it is 1.0.5). But how to do some kind of pre-processing that will change the version in reactor so Maven will then use 1.0.5 version?

Daniel Man
  • 21
  • 5
  • 1
    Welcome to Stack Overflow! Please be a bit more specific when asking a question: *What have you tried so far with a code example? ([I downvoted because there is no code](http://idownvotedbecau.se/nocode/))* / *What do you expect?* / *What error do you get?* **For Help take a look at "[How to ask](https://stackoverflow.com/help/how-to-ask)"** – Hille Dec 18 '18 at 14:21
  • Hey Dan - can you provide an example? How/what would decide what version of the dependency is required? – Rich Duncan Dec 18 '18 at 14:49
  • Sorry Rich. Added more information. Thanks! – Daniel Man Dec 18 '18 at 15:37
  • I would suggest to take a look at: https://maven.apache.org/maven-ci-friendly.html – khmarbaise Dec 18 '18 at 19:46

1 Answers1

1

OK. It seems like I got it ;-) Thanks to khmarbaise for comment. I have already seen this page but haven't given an attention to this.

So I have created MavenLifecycleParticipant where I compute desired version and set it as user property:

@Component(role = AbstractMavenLifecycleParticipant.class, hint = "my-dep")
public class DependencyLifecycleParticipant extends AbstractMavenLifecycleParticipant 
{

@Override
public void afterSessionStart(MavenSession session) throws MavenExecutionException {
    session.getUserProperties().setProperty("MyVersion", "1.2.3");
}

}

Now in my pom.xml, I can use something like:

<version>${MyVersion}</version>

in both dependencies and project version (that was nice-to-have for me ;-)

Daniel Man
  • 21
  • 5