Daniel Man
Daniel Man

Reputation: 21

Maven plugin/extension to dynamically update dependency version

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:

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?

Upvotes: 1

Views: 1396

Answers (1)

Daniel Man
Daniel Man

Reputation: 21

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 ;-)

Upvotes: 1

Related Questions