Finalize support for compatibility packages

Now our resolvers will correctly handle multiple packages providing identical
GAs provided that just one has unversioned jar. Packages wanting to use
non-defaul GAV will have to either specify that exactly, or ask given package to
add additional compat version mapping manually (-v switch for add_maven_depmap)
This commit is contained in:
Stanislav Ochotnicky 2012-10-18 15:51:02 +02:00
parent 9b93db7e10
commit ab2f71a525
2 changed files with 159 additions and 126 deletions

View File

@ -1,6 +1,7 @@
package org.apache.maven.artifact.resolver; package org.apache.maven.artifact.resolver;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -14,7 +15,7 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
private WorkspaceRepository workspaceRepository; private WorkspaceRepository workspaceRepository;
private static final char GROUP_SEPARATOR = '.'; private static final char GROUP_SEPARATOR = '.';
private static final char PATH_SEPARATOR = '/'; private static final char PATH_SEPARATOR = File.separatorChar;
public JavadirWorkspaceReader() { public JavadirWorkspaceReader() {
workspaceRepository = new WorkspaceRepository("javadir-workspace"); workspaceRepository = new WorkspaceRepository("javadir-workspace");
@ -24,8 +25,10 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
return workspaceRepository; return workspaceRepository;
} }
private static final String LOG_FILE = System.getProperty("maven.resolver.logfile"); private static final String LOG_FILE = System
private static final java.util.concurrent.Semaphore LOG_SEMAPHORE = new java.util.concurrent.Semaphore(1); .getProperty("maven.resolver.logfile");
private static final java.util.concurrent.Semaphore LOG_SEMAPHORE = new java.util.concurrent.Semaphore(
1);
public File findArtifact(Artifact artifact) { public File findArtifact(Artifact artifact) {
File f = findArtifactImpl(artifact); File f = findArtifactImpl(artifact);
@ -33,14 +36,14 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
LOG_SEMAPHORE.acquireUninterruptibly(); LOG_SEMAPHORE.acquireUninterruptibly();
try { try {
if (LOG_FILE != null && f != null) { if (LOG_FILE != null && f != null) {
java.io.FileOutputStream fos = new java.io.FileOutputStream(LOG_FILE, true); java.io.FileOutputStream fos = new java.io.FileOutputStream(
LOG_FILE, true);
java.io.PrintStream ps = new java.io.PrintStream(fos); java.io.PrintStream ps = new java.io.PrintStream(fos);
ps.println(f.getAbsolutePath()); ps.println(f.getAbsolutePath());
ps.close(); ps.close();
} }
} } catch (Exception _) {
catch (Exception _) {} } finally {
finally {
LOG_SEMAPHORE.release(); LOG_SEMAPHORE.release();
return f; return f;
} }
@ -59,9 +62,9 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
// let's check out local repo first // let's check out local repo first
String m2_path = System.getProperty("maven.repo.local"); String m2_path = System.getProperty("maven.repo.local");
String gid_path = groupId.replace(".", File.separator); String gid_path = groupId.replace(".", File.separator);
String art_path = m2_path + File.separator + gid_path + File.separator + artifactId + String art_path = m2_path + File.separator + gid_path + File.separator
File.separator + version + File.separator + artifactId + "-" + + artifactId + File.separator + version + File.separator
version + "." + artifact.getExtension(); + artifactId + "-" + version + "." + artifact.getExtension();
ret = new File(art_path); ret = new File(art_path);
if (ret.isFile()) { if (ret.isFile()) {
@ -69,19 +72,36 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
return ret; return ret;
} }
// maven.repo.local does not have needed GAV (that's normal), so let's
// just continue with system packages
MavenJPackageDepmap.debug("Wanted GROUPID=" + groupId); MavenJPackageDepmap.debug("Wanted GROUPID=" + groupId);
MavenJPackageDepmap.debug("Wanted ARTIFACTID=" + artifactId); MavenJPackageDepmap.debug("Wanted ARTIFACTID=" + artifactId);
MavenJPackageDepmap.debug("Wanted VERSION=" + version); MavenJPackageDepmap.debug("Wanted VERSION=" + version);
ArrayList<Hashtable<String, String>> maps = new ArrayList<Hashtable<String, String>>();
if (!groupId.startsWith("JPP")) { if (!groupId.startsWith("JPP")) {
MavenJPackageDepmap map = MavenJPackageDepmap.getInstance(); MavenJPackageDepmap map = MavenJPackageDepmap.getInstance();
// let's try to get exact GAV first
Hashtable<String, String> newInfo = map.getMappedInfo(groupId, Hashtable<String, String> newInfo = map.getMappedInfo(groupId,
artifactId, version); artifactId, version);
if (newInfo == null) {
// exact GAV does not exist in our mapping so let's just get all
// GAs and iterate until we find one that exists. Note that only
// one non-versioned jar/pom for given GA should exist in order
// to ensure reproducibility
groupId = (String) newInfo.get("group"); maps = map.getUnversionedMappedInfo(groupId, artifactId,
artifactId = (String) newInfo.get("artifact"); version);
version = (String) newInfo.get("version"); } else {
maps.add(newInfo);
} }
}
for (Hashtable<String, String> map : maps) {
groupId = map.get("group");
artifactId = map.get("artifact");
version = map.get("version");
MavenJPackageDepmap.debug("Resolved GROUPID=" + groupId); MavenJPackageDepmap.debug("Resolved GROUPID=" + groupId);
MavenJPackageDepmap.debug("Resolved ARTIFACTID=" + artifactId); MavenJPackageDepmap.debug("Resolved ARTIFACTID=" + artifactId);
MavenJPackageDepmap.debug("Resolved VERSION=" + version); MavenJPackageDepmap.debug("Resolved VERSION=" + version);
@ -89,12 +109,16 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
if (artifact.getExtension().equals("pom")) { if (artifact.getExtension().equals("pom")) {
path = getPOMPath(groupId, artifactId, version); path = getPOMPath(groupId, artifactId, version);
ret = new File(path.toString()); ret = new File(path.toString());
if (ret.isFile()) {
MavenJPackageDepmap.debug("Returning " + path.toString());
return ret;
}
} else { } else {
String repos[] = { "/usr/share/maven/repository/", String repos[] = { "/usr/share/maven/repository/",
"/usr/share/maven/repository-java-jni/", "/usr/share/maven/repository-java-jni/",
"/usr/share/maven/repository-jni/" }; "/usr/share/maven/repository-jni/" };
String verRelativeArtifactPath = groupId + "/" + artifactId + "-" String verRelativeArtifactPath = groupId + "/" + artifactId
+ wantedVersion + "." + artifact.getExtension(); + "-" + wantedVersion + "." + artifact.getExtension();
String relativeArtifactPath = groupId + "/" + artifactId + "." String relativeArtifactPath = groupId + "/" + artifactId + "."
+ artifact.getExtension(); + artifact.getExtension();
for (String repo : repos) { for (String repo : repos) {
@ -116,17 +140,13 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
} }
} }
}
// if file doesn't exist return null to delegate to other // if file doesn't exist return null to delegate to other
// resolvers (reactor/local repo) // resolvers (reactor/local repo)
if (ret.isFile()) {
MavenJPackageDepmap.debug("Returning " + path.toString());
return ret;
} else {
MavenJPackageDepmap.debug("Returning null for gid:aid =>" + groupId MavenJPackageDepmap.debug("Returning null for gid:aid =>" + groupId
+ ":" + artifactId); + ":" + artifactId);
return null; return null;
} }
}
public List<String> findVersions(Artifact artifact) { public List<String> findVersions(Artifact artifact) {
List<String> ret = new LinkedList<String>(); List<String> ret = new LinkedList<String>();
@ -134,11 +154,12 @@ public class JavadirWorkspaceReader implements WorkspaceReader {
return ret; return ret;
} }
private StringBuffer getPOMPath(String groupId, String artifactId, String version) { private StringBuffer getPOMPath(String groupId, String artifactId,
String version) {
String fName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR) + "-" String fName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR) + "-"
+ artifactId + ".pom"; + artifactId + ".pom";
String verfName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR) + "-" String verfName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR)
+ artifactId + "-" + version + ".pom"; + "-" + artifactId + "-" + version + ".pom";
File f; File f;
String[] pomRepos = { "/usr/share/maven2/poms/", String[] pomRepos = { "/usr/share/maven2/poms/",
"/usr/share/maven/poms/", "/usr/share/maven-poms/" }; "/usr/share/maven/poms/", "/usr/share/maven-poms/" };

View File

@ -5,7 +5,9 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -71,9 +73,11 @@ public class MavenJPackageDepmap {
private static MavenJPackageDepmap instance; private static MavenJPackageDepmap instance;
private static Hashtable<String, String> jppArtifactMap; private static Hashtable<String, String> jppArtifactMap;
private static Hashtable<String, ArrayList<String>> jppUnversionedArtifactMap;
private MavenJPackageDepmap() { private MavenJPackageDepmap() {
jppArtifactMap = new Hashtable<String, String>(); jppArtifactMap = new Hashtable<String, String>();
jppUnversionedArtifactMap = new Hashtable<String, ArrayList<String>>();
buildJppArtifactMap(); buildJppArtifactMap();
} }
@ -85,71 +89,78 @@ public class MavenJPackageDepmap {
return instance; return instance;
} }
public Hashtable<String, String> getMappedInfo( /**
Hashtable<String, String> mavenDep) { * This function can be used to query exact version of an artifact.
return getMappedInfo((String) mavenDep.get("group"), *
(String) mavenDep.get("artifact"), * @param groupId
(String) mavenDep.get("version")); * @param artifactId
} * @param version
* @return Hashtable mapping for groupId, artifactId and version or null if
* exact mapping not found
*/
public Hashtable<String, String> getMappedInfo(String groupId, public Hashtable<String, String> getMappedInfo(String groupId,
String artifactId, String version) { String artifactId, String version) {
Hashtable<String, String> jppDep; Hashtable<String, String> jppDep;
String idToCheck, jppCombination; String idToCheck, jppCombination;
if (System.getProperty("maven.ignore.versions") == null
&& System.getProperty("maven.local.mode") == null) {
idToCheck = groupId + "," + artifactId + "," + version; idToCheck = groupId + "," + artifactId + "," + version;
} else {
idToCheck = groupId + "," + artifactId;
}
jppCombination = (String) jppArtifactMap.get(idToCheck); jppCombination = (String) jppArtifactMap.get(idToCheck);
jppDep = new Hashtable<String, String>(); jppDep = null;
if (jppCombination != null && jppCombination != "") { if (jppCombination != null && jppCombination != "") {
StringTokenizer st = new StringTokenizer(jppCombination, ","); StringTokenizer st = new StringTokenizer(jppCombination, ",");
jppDep = new Hashtable<String, String>();
jppDep.put("group", st.nextToken()); jppDep.put("group", st.nextToken());
jppDep.put("artifact", st.nextToken()); jppDep.put("artifact", st.nextToken());
jppDep.put("version", st.nextToken()); jppDep.put("version", st.nextToken());
} else {
jppDep.put("group", groupId);
jppDep.put("artifact", artifactId);
jppDep.put("version", version);
} }
return jppDep; return jppDep;
} }
/** /**
* Returns whether or not the given dependency should be dropped. * This function can be used to query for all possible artifact resolutions.
* It works with multiple duplicate gid:aid mappings, but only one should
* have unversioned files (default version) to work properly later
*
* @param groupId
* @param artifactId
* @param version
* @return
*/ */
public boolean shouldEliminate(String groupId, String artifactId, public ArrayList<Hashtable<String, String>> getUnversionedMappedInfo(
String version) { String groupId, String artifactId, String version) {
Hashtable<String, String> jppDep;
String idToCheck; String idToCheck;
List<String> maps;
if (System.getProperty("maven.ignore.versions") == null
&& System.getProperty("maven.local.mode") == null) {
idToCheck = groupId + "," + artifactId + "," + version;
} else {
idToCheck = groupId + "," + artifactId; idToCheck = groupId + "," + artifactId;
maps = jppUnversionedArtifactMap.get(idToCheck);
ArrayList<Hashtable<String, String>> ret = new ArrayList<Hashtable<String, String>>();
if (maps != null) {
for (String jppPart : maps) {
jppDep = new Hashtable<String, String>();
StringTokenizer st = new StringTokenizer(jppPart, ",");
jppDep.put("group", st.nextToken());
jppDep.put("artifact", st.nextToken());
jppDep.put("version", st.nextToken());
// we add to index 0 to make it reversed order for compatibility
// with older code
ret.add(0, jppDep);
} }
}
return jppArtifactMap.get(idToCheck) != null return ret;
&& jppArtifactMap.get(idToCheck).equals("");
} }
private static void buildJppArtifactMap() { private static void buildJppArtifactMap() {
if (System.getProperty("maven.ignore.versions") != null
|| System.getProperty("maven.local.mode") != null) {
debug("Processing file: /usr/share/java-utils/xml/maven2-versionless-depmap.xml");
processDepmapFile("/etc/maven/maven2-versionless-depmap.xml"); processDepmapFile("/etc/maven/maven2-versionless-depmap.xml");
}
// process fragments in etc // process fragments in etc
File fragmentDir = new File("/etc/maven/fragments"); File fragmentDir = new File("/etc/maven/fragments");
@ -173,7 +184,6 @@ public class MavenJPackageDepmap {
String customFileName = System.getProperty("maven.local.depmap.file", String customFileName = System.getProperty("maven.local.depmap.file",
null); null);
if (customFileName != null) { if (customFileName != null) {
debug("Processing file: " + customFileName);
processDepmapFile(customFileName); processDepmapFile(customFileName);
} }
@ -233,32 +243,34 @@ public class MavenJPackageDepmap {
if (jppNodeList.getLength() == 1) { if (jppNodeList.getLength() == 1) {
jppAD = getArtifactDefinition((Element) jppNodeList.item(0)); jppAD = getArtifactDefinition((Element) jppNodeList.item(0));
if (System.getProperty("maven.ignore.versions") == null
&& System.getProperty("maven.local.mode") == null) {
debug("*** Adding: " + mavenAD.groupId + ","
+ mavenAD.artifactId + "," + mavenAD.version
+ " => " + jppAD.groupId + "," + jppAD.artifactId
+ "," + jppAD.version + " to map...");
jppArtifactMap.put(mavenAD.groupId + ","
+ mavenAD.artifactId + "," + mavenAD.version,
jppAD.groupId + "," + jppAD.artifactId + ","
+ jppAD.version);
} else {
debug("*** Adding: " + mavenAD.groupId + "," debug("*** Adding: " + mavenAD.groupId + ","
+ mavenAD.artifactId + " => " + jppAD.groupId + "," + mavenAD.artifactId + " => " + jppAD.groupId + ","
+ jppAD.artifactId + "," + jppAD.version + jppAD.artifactId + "," + jppAD.version + " to map...");
+ " to map...");
jppArtifactMap.put(mavenAD.groupId + "," jppArtifactMap.put(mavenAD.groupId + "," + mavenAD.artifactId
+ mavenAD.artifactId, jppAD.groupId + "," + "," + mavenAD.version, jppAD.groupId + ","
+ jppAD.artifactId + "," + jppAD.version); + jppAD.artifactId + "," + jppAD.version);
ArrayList<String> maps = jppUnversionedArtifactMap
.get(mavenAD.groupId + "," + mavenAD.artifactId);
if (maps == null) {
maps = new ArrayList<String>();
} }
maps.add(jppAD.groupId + "," + jppAD.artifactId + ","
+ jppAD.version);
jppUnversionedArtifactMap.put(mavenAD.groupId + ","
+ mavenAD.artifactId, maps);
} else { } else {
debug("Number of jpp sub-elements is not 1. Dropping dependency for " debug("Number of jpp sub-elements is not 1. Dropping dependency for "
+ mavenAD.groupId + ":" + mavenAD.artifactId); + mavenAD.groupId + ":" + mavenAD.artifactId);
jppArtifactMap.put(mavenAD.groupId + "," + mavenAD.artifactId, jppArtifactMap.put(mavenAD.groupId + "," + mavenAD.artifactId
"JPP/maven,empty-dep," + mavenAD.version); + "," + mavenAD.version, "JPP/maven,empty-dep,"
+ mavenAD.version);
ArrayList<String> maps = new ArrayList<String>();
maps.add("JPP/maven,empty-dep," + mavenAD.version);
jppUnversionedArtifactMap.put(mavenAD.groupId + ","
+ mavenAD.artifactId, maps);
} }
} }
} }