package hudson.maven;

import org.apache.maven.project.MavenProject;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.Extension;

import java.io.Serializable;

/**
 * group id + artifact id + version.
 *
 * @author Kohsuke Kawaguchi
 * @see ModuleName
 */
public final class ModuleDependency implements Serializable {
    public final String groupId;
    public final String artifactId;
    public final String version;

    public ModuleDependency(String groupId, String artifactId, String version) {
        this.groupId = groupId;
        this.artifactId = artifactId;
        if(version==null)   version=UNKNOWN;
        this.version = version;
    }

    public ModuleDependency(ModuleName name, String version) {
        this(name.groupId,name.artifactId,version);
    }

    public ModuleDependency(org.apache.maven.model.Dependency dep) {
        this(dep.getGroupId(),dep.getArtifactId(),dep.getVersion());
    }

    public ModuleDependency(MavenProject project) {
        this(project.getGroupId(),project.getArtifactId(),project.getVersion());
    }

    public ModuleDependency(Plugin p) {
        this(p.getGroupId(),p.getArtifactId(),p.getVersion());
    }

    public ModuleDependency(ReportPlugin p) {
        this(p.getGroupId(),p.getArtifactId(),p.getVersion());
    }

    public ModuleDependency(Extension ext) {
        this(ext.getGroupId(),ext.getArtifactId(),ext.getVersion());
    }

    public ModuleName getName() {
        return new ModuleName(groupId,artifactId);
    }

    /**
     * Returns groupId+artifactId plus unknown version.
     */
    public ModuleDependency withUnknownVersion() {
        return new ModuleDependency(groupId,artifactId,UNKNOWN);
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ModuleDependency that = (ModuleDependency) o;

        return this.artifactId.equals(that.artifactId)
            && this.groupId.equals(that.groupId)
            && this.version.equals(that.version);
    }

    public int hashCode() {
        int result;
        result = groupId.hashCode();
        result = 31 * result + artifactId.hashCode();
        result = 31 * result + version.hashCode();
        return result;
    }

    /**
     * For compatibility reason, this value may be used in the verion field
     * to indicate that the version is unknown.
     */
    public static final String UNKNOWN = "*";

    private static final long serialVersionUID = 1L;
}

@@ -216,16 +216,42 @@
 zable;%0A%0A
+import hudson.Functions;%0A%0A
 /**%0A * g
@@ -1223,32 +1223,53 @@
 getArtifactId(),
+ Functions.defaulted(
 p.getVersion());
@@ -1258,32 +1258,38 @@
 d(p.getVersion()
+,NONE)
 );%0A    %7D%0A%0A    pu
@@ -2516,32 +2516,32 @@
 nknown.%0A     */%0A
-
     public stati
@@ -2571,16 +2571,516 @@
 = %22*%22;%0A%0A
+    /**%0A     * When a plugin dependency is specified without giving a version,%0A     * the semantics of that is the latest released plugin.%0A     * In this case, we don't want the %7B@link ModuleDependency%7D version to become%0A     * %7B@link #UNKNOWN%7D, which would match any builds of the plugin.%0A     *%0A     * %3Cp%3E%0A     * So we use this constant to indicate a version, and this will not match%0A     * anything.%0A     *%0A     * @see #ModuleDependency(Plugin)%0A     */%0A    public static final String NONE = %22-%22;%0A%0A
     priv

handle the version-less plugin dependency correctly: http://www.nabble.com/upstream-downstream-trouble-with-1.206-maven-integration-tp16640405p16640405.html