tsegismont
tsegismont

Reputation: 9128

Resolve the latest version of an artifact inside a Mojo class?

In my Mojo class, I have the following code to resolve the org.rhq.helpers:rhq-pluginGen:jar:3.0.4 JAR:

    private static final String PLUGIN_GENERATOR_MODULE_GROUP_ID = "org.rhq.helpers";
    private static final String PLUGIN_GENERATOR_MODULE_ARTIFACT_ID = "rhq-pluginGen";
    private static final String PLUGIN_GENERATOR_MAIN_CLASS = "org.rhq.helpers.pluginGen.PluginGen";
    private static final String PLUGIN_GENERATOR_VERSION = "3.0.4";

    //------

    Artifact dummyOriginatingArtifact = artifactFactory
            .createBuildArtifact("org.apache.maven.plugins",
                    "maven-downloader-plugin", "1.0", "jar");
    Artifact pluginContainerArtifact = this.artifactFactory.createArtifact(
            PLUGIN_GENERATOR_MODULE_GROUP_ID,
            PLUGIN_GENERATOR_MODULE_ARTIFACT_ID, PLUGIN_GENERATOR_VERSION, null, "jar");
    ArtifactResolutionResult artifactResolutionResult = artifactResolver
            .resolveTransitively(
                    Collections.singleton(pluginContainerArtifact),
                    dummyOriginatingArtifact, localRepository,
                    remoteRepositories, artifactMetadataSource, null);

Now I'd like to resolve the latest version of the same artifact. It looks like it's not possible with an ArtifactResolver.

Upvotes: 2

Views: 359

Answers (1)

tsegismont
tsegismont

Reputation: 9128

Found out a solution looking at the Maven Version plugin code.

ArtifactResolver cannot resolve versions, but ArtifactMetadataSource can:

    private static final String PLUGIN_GENERATOR_VERSION = "[1.0.0,)";

    // ------------------

    Artifact pluginContainerArtifact = this.artifactFactory.createArtifact(
            PLUGIN_GENERATOR_MODULE_GROUP_ID,
            PLUGIN_GENERATOR_MODULE_ARTIFACT_ID, PLUGIN_GENERATOR_VERSION, null, "jar");

    @SuppressWarnings("unchecked")
    List<ArtifactVersion> availableVersions = artifactMetadataSource.retrieveAvailableVersions
            (pluginContainerArtifact, localRepository,
            remoteRepositories);
    Collections.sort(availableVersions);
    ArtifactVersion latestVersion = availableVersions.get(availableVersions.size() - 1);
    if (getLog().isDebugEnabled()) {
        getLog().debug(
                PLUGIN_GENERATOR_MODULE_GROUP_ID + ":" + PLUGIN_GENERATOR_MODULE_ARTIFACT_ID + ", " +
                        "latestVersion = " + latestVersion);
    }

Upvotes: 1

Related Questions