在Mojo类中解析最新版本的工件?

时间:2014-05-16 20:01:35

标签: maven-plugin

在我的Mojo类中,我有以下代码来解析 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);

现在我想解决同一个工件的最新版本。看起来使用 ArtifactResolver 是不可能的。

1 个答案:

答案 0 :(得分:1)

找到一个查看Maven Version插件代码的解决方案。

ArtifactResolver 无法解析版本,但 ArtifactMetadataSource 可以:

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