How to obtain the artifactId of the pom (bom) listed in a maven project dependencyManagement block

Viewed 95

I am working on a maven lifecycle extension class and have need to examine the coordinate details of the pom (bom) listed in a given project's dependencyManagement block. I need to be able to do this in the afterProjectsRead method i am overriding in the extension class. I understand the <scope>import</scope> tag causes the dependencyManagement dependencies section to be replaced by the dependencies listed in the imported pom (bom). But I need to look at the coordinate details of the pom (bom) in my extension class, but it appears that import has already happened and those details are unavailable when i land in my afterProjectsRead extension method.

to be clear, the dependencyManagement block in a given maven project looks like this:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>some.project</groupId>
            <artifactId>some-project-bom</artifactId>
            <version>0.1-SNAPSHOT</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

And my maven lifecycle extension class is attempting to do this:

@Override
public void afterProjectsRead(MavenSession session) {

    MavenProject proj = session.getCurrentProject();
    DependencyManagement depMgmt = proj.getDependencyManagement();
    for (Dependency dep : depMgmt.getDependencies()) {
        System.out.println(dep.getArtifactId() + ":" + dep.getVersion());
    }
}   

in order to obtain the coordinate details of the referenced pom (bom)

I was hoping to obtain the groupId, artifactId and version of the pom (bom) itself at that point, but that information is already replaced by the dependencies actually listed in some-project-bom.xml. And so I am retrieving the coordinate details of the dependencies specified in the pom (bom) xml file.

Is this possible? maybe via the MavenSession object?

1 Answers

I found the solution to this - from the Lifecycle Extension Class I am developing, I was able to retrieve the MavenProject via The passed in MavenSession object. From the MavenProject, I was able to obtain the original Model object and from the Model object I retrieved the DependencyManagement object of the pom prior to import and substitution.

@Override
public void afterProjectsRead(MavenSession session) {
    MavenProject proj = session.getProject();
    depMgmt = proj.getOriginalModel().getDependencyManagement();
    for (Dependency dep : depMgmt.getDependencies()) {
        System.out.println("found this artifact in dependencyMgmt section-> "
                    + dep.getArtifactId() + ":" + dep.getVersion() + "\n");
    }
}
Related