Process fragments directly instead of maven2-depmap.xml

Fragments are also expected in /usr/share/maven-fragments where they
should be moved (backward compat preserved for now).

Big changes are due to reformatting to use spaces (sorry, cleaner
log/diff in separate resolver repo)
This commit is contained in:
Stanislav Ochotnicky 2011-06-10 10:33:20 +02:00
parent 70f23c4306
commit bdbc16d2c1
3 changed files with 283 additions and 190 deletions

View File

@ -11,16 +11,12 @@ import org.sonatype.aether.repository.WorkspaceReader;
import org.sonatype.aether.repository.WorkspaceRepository; import org.sonatype.aether.repository.WorkspaceRepository;
import org.sonatype.aether.artifact.Artifact; import org.sonatype.aether.artifact.Artifact;
public class JavadirWorkspaceReader implements WorkspaceReader {
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 = '/';
public JavadirWorkspaceReader() { public JavadirWorkspaceReader() {
workspaceRepository = new WorkspaceRepository("javadir-workspace"); workspaceRepository = new WorkspaceRepository("javadir-workspace");
} }
@ -29,8 +25,9 @@ public class JavadirWorkspaceReader
return workspaceRepository; return workspaceRepository;
} }
public File findArtifact( Artifact artifact ) { public File findArtifact(Artifact artifact) {
MavenJPackageDepmap.debug("=============JAVADIRREADER-FIND_ARTIFACT: " + artifact.getArtifactId()); MavenJPackageDepmap.debug("=============JAVADIRREADER-FIND_ARTIFACT: "
+ artifact.getArtifactId());
StringBuffer path = new StringBuffer(); StringBuffer path = new StringBuffer();
String artifactId = artifact.getArtifactId(); String artifactId = artifact.getArtifactId();
@ -42,7 +39,8 @@ public class JavadirWorkspaceReader
if (!groupId.startsWith("JPP")) { if (!groupId.startsWith("JPP")) {
MavenJPackageDepmap map = MavenJPackageDepmap.getInstance(); MavenJPackageDepmap map = MavenJPackageDepmap.getInstance();
Hashtable<String,String> newInfo = map.getMappedInfo(groupId, artifactId, version); Hashtable<String, String> newInfo = map.getMappedInfo(groupId,
artifactId, version);
groupId = (String) newInfo.get("group"); groupId = (String) newInfo.get("group");
artifactId = (String) newInfo.get("artifact"); artifactId = (String) newInfo.get("artifact");
@ -54,32 +52,33 @@ public class JavadirWorkspaceReader
path = getPOMPath(groupId, artifactId); path = getPOMPath(groupId, artifactId);
} else if (artifact.getExtension().equals("signature")) { } else if (artifact.getExtension().equals("signature")) {
path.append("/usr/share/maven/repository/"); path.append("/usr/share/maven/repository/");
path.append( groupId ).append( '/' ); path.append(groupId).append('/');
path.append( artifactId ).append( ".signature" ); path.append(artifactId).append(".signature");
} else if (artifact.getExtension().equals("zip")) { } else if (artifact.getExtension().equals("zip")) {
path.append("/usr/share/maven/repository/"); path.append("/usr/share/maven/repository/");
path.append( groupId ).append( '/' ); path.append(groupId).append('/');
path.append( artifactId ).append( ".zip" ); path.append(artifactId).append(".zip");
} else { } else {
path.append("/usr/share/maven/repository/"); path.append("/usr/share/maven/repository/");
path.append( groupId ).append( '/' ); path.append(groupId).append('/');
path.append( artifactId ).append( ".jar" ); path.append(artifactId).append(".jar");
} }
MavenJPackageDepmap.debug("Returning " + path.toString()); MavenJPackageDepmap.debug("Returning " + path.toString());
File ret = new File(path.toString()); File ret = new File(path.toString());
// 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() ) { if (ret.isFile()) {
MavenJPackageDepmap.debug("Returning " + path.toString()); MavenJPackageDepmap.debug("Returning " + path.toString());
return ret; return ret;
} else { } else {
MavenJPackageDepmap.debug("Returning null for gid:aid =>" + groupId + ":" + artifactId); MavenJPackageDepmap.debug("Returning null for gid:aid =>" + groupId
+ ":" + 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>();
ret.add("latest"); ret.add("latest");
return ret; return ret;
@ -87,25 +86,30 @@ public class JavadirWorkspaceReader
private StringBuffer getPOMPath(String groupId, String artifactId) { private StringBuffer getPOMPath(String groupId, String artifactId) {
String fName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR) + "-" + artifactId + ".pom"; String fName = groupId.replace(PATH_SEPARATOR, GROUP_SEPARATOR) + "-"
String m2path = System.getProperty("maven2.local.pom.path", "JPP/maven2/poms") + "/" + fName; + artifactId + ".pom";
String m3path = System.getProperty("maven.local.pom.path", "JPP/maven/poms") + "/" + fName; String m2path = System.getProperty("maven2.local.pom.path",
"JPP/maven2/poms") + "/" + fName;
String m3path = System.getProperty("maven.local.pom.path",
"JPP/maven/poms") + "/" + fName;
File f; File f;
// let's try maven 2 repo first // let's try maven 2 repo first
f = new File(System.getProperty("maven2.local.default.repo", "/usr/share/maven2/repository") + "/" + m2path); f = new File(System.getProperty("maven2.local.default.repo",
if ( f.exists() ) { "/usr/share/maven2/repository") + "/" + m2path);
if (f.exists()) {
return new StringBuffer(f.getPath()); return new StringBuffer(f.getPath());
} }
f = new File(System.getProperty("maven.local.default.repo", "/usr/share/maven/repository") + "/" + m3path); f = new File(System.getProperty("maven.local.default.repo",
if ( f.exists() ) { "/usr/share/maven/repository") + "/" + m3path);
if (f.exists()) {
return new StringBuffer(f.getPath()); return new StringBuffer(f.getPath());
} }
// final fallback to m2 default poms // final fallback to m2 default poms
return new StringBuffer("/usr/share/maven2/repository/" + return new StringBuffer("/usr/share/maven2/repository/"
System.getProperty("maven.local.default.repo", "JPP/maven2/default_poms") + + System.getProperty("maven.local.default.repo",
"/" + fName); "JPP/maven2/default_poms") + "/" + fName);
} }
} }

View File

@ -1,8 +1,10 @@
package org.apache.maven.artifact.repository; package org.apache.maven.artifact.repository;
import java.io.File;
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.util.Hashtable; import java.util.Hashtable;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -10,204 +12,288 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import org.w3c.dom.*;
public class MavenJPackageDepmap { public class MavenJPackageDepmap {
private static class ArtifactDefinition { private static class ArtifactDefinition {
String groupId = null; String groupId = null;
String artifactId = null; String artifactId = null;
String version = null; String version = null;
} }
private static MavenJPackageDepmap instance; /**
private static Hashtable<String, String> jppArtifactMap; *
* @author Stanislav Ochotnicky <sochotnicky@redhat.com>
*
* This class is used to wrap around fragments that are mapping
* artifacts to jar files in our _javadir. These used to be
* processed in a macro after every package installation. Fragments
* themselves are not proper xml files (they have no root element)
* so we have to fix them by wrapping them in one root element.
*/
private static class WrapFragmentStream extends InputStream {
String startTag = "<deps>";
String endTag = "</deps>";
byte fragmentContent[];
int position;
private MavenJPackageDepmap() { WrapFragmentStream(String fragmentPath) throws IOException {
jppArtifactMap = new Hashtable<String,String>(); FileInputStream fin = new FileInputStream(fragmentPath);
buildJppArtifactMap(); int nBytes = fin.available();
} byte tmpContent[] = new byte[nBytes];
fin.read(tmpContent);
fin.close();
byte startBytes[] = startTag.getBytes();
byte endBytes[] = endTag.getBytes();
fragmentContent = new byte[nBytes + startBytes.length
+ endBytes.length];
System.arraycopy(startBytes, 0, fragmentContent, 0,
startBytes.length);
System.arraycopy(tmpContent, 0, fragmentContent, startBytes.length,
tmpContent.length);
System.arraycopy(endBytes, 0, fragmentContent, startBytes.length
+ tmpContent.length, endBytes.length);
position = 0;
}
public static MavenJPackageDepmap getInstance() { public int read() throws IOException {
if (instance == null) { if (position < fragmentContent.length) {
instance = new MavenJPackageDepmap(); return fragmentContent[position++];
} } else {
return -1;
}
}
}
return instance; private static MavenJPackageDepmap instance;
} private static Hashtable<String, String> jppArtifactMap;
public Hashtable<String, String> getMappedInfo(Hashtable<String, String> mavenDep) { private MavenJPackageDepmap() {
return getMappedInfo((String) mavenDep.get("group"), jppArtifactMap = new Hashtable<String, String>();
(String) mavenDep.get("artifact"), buildJppArtifactMap();
(String) mavenDep.get("version")); }
}
public Hashtable<String, String> getMappedInfo(String groupId, String artifactId, String version) { public static MavenJPackageDepmap getInstance() {
if (instance == null) {
instance = new MavenJPackageDepmap();
}
Hashtable<String, String> jppDep; return instance;
String idToCheck, jppCombination; }
if (System.getProperty("maven.ignore.versions") == null && System.getProperty("maven.local.mode") == null) { public Hashtable<String, String> getMappedInfo(
idToCheck = groupId+","+artifactId+","+version; Hashtable<String, String> mavenDep) {
} else { return getMappedInfo((String) mavenDep.get("group"),
idToCheck = groupId+","+artifactId; (String) mavenDep.get("artifact"),
} (String) mavenDep.get("version"));
}
jppCombination = (String) jppArtifactMap.get(idToCheck); public Hashtable<String, String> getMappedInfo(String groupId,
String artifactId, String version) {
//System.err.println("*** " + groupId+","+artifactId+","+version + " => " + jppCombination); Hashtable<String, String> jppDep;
String idToCheck, jppCombination;
jppDep = new Hashtable<String, String>(); if (System.getProperty("maven.ignore.versions") == null
if (jppCombination != null && jppCombination != "") { && System.getProperty("maven.local.mode") == null) {
idToCheck = groupId + "," + artifactId + "," + version;
} else {
idToCheck = groupId + "," + artifactId;
}
StringTokenizer st = new StringTokenizer(jppCombination, ","); jppCombination = (String) jppArtifactMap.get(idToCheck);
jppDep.put("group", st.nextToken()); // System.err.println("*** " + groupId+","+artifactId+","+version +
jppDep.put("artifact",st.nextToken()); // " => " + jppCombination);
jppDep.put("version",st.nextToken());
} else { jppDep = new Hashtable<String, String>();
jppDep.put("group", groupId); if (jppCombination != null && jppCombination != "") {
jppDep.put("artifact", artifactId);
jppDep.put("version", version);
}
return jppDep; StringTokenizer st = new StringTokenizer(jppCombination, ",");
}
jppDep.put("group", st.nextToken());
jppDep.put("artifact", st.nextToken());
jppDep.put("version", st.nextToken());
/** } else {
* Returns whether or not the given dependency should be dropped. jppDep.put("group", groupId);
*/ jppDep.put("artifact", artifactId);
public boolean shouldEliminate(String groupId, String artifactId, String version) { jppDep.put("version", version);
String idToCheck; }
if (System.getProperty("maven.ignore.versions") == null && System.getProperty("maven.local.mode") == null) { return jppDep;
idToCheck = groupId+","+artifactId+","+version; }
} else {
idToCheck = groupId+","+artifactId;
}
return jppArtifactMap.get(idToCheck) != null && jppArtifactMap.get(idToCheck).equals(""); /**
* Returns whether or not the given dependency should be dropped.
*/
public boolean shouldEliminate(String groupId, String artifactId,
String version) {
String idToCheck;
} if (System.getProperty("maven.ignore.versions") == null
&& System.getProperty("maven.local.mode") == null) {
idToCheck = groupId + "," + artifactId + "," + version;
} else {
idToCheck = groupId + "," + artifactId;
}
private static void buildJppArtifactMap() { return jppArtifactMap.get(idToCheck) != null
&& jppArtifactMap.get(idToCheck).equals("");
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");
}
debug("Processing file: /usr/share/java-utils/xml/maven2-depmap.xml"); private static void buildJppArtifactMap() {
processDepmapFile("/etc/maven/maven2-depmap.xml");
String customFileName = System.getProperty("maven.local.depmap.file", null); if (System.getProperty("maven.ignore.versions") != null
if (customFileName != null) { || System.getProperty("maven.local.mode") != null) {
debug("Processing file: " + customFileName); debug("Processing file: /usr/share/java-utils/xml/maven2-versionless-depmap.xml");
processDepmapFile(customFileName); processDepmapFile("/etc/maven/maven2-versionless-depmap.xml");
} }
}
private static void processDepmapFile(String fileName) { // process fragments in etc
File fragmentDir = new File("/etc/maven/fragments");
String flist[] = fragmentDir.list();
if (flist != null)
for (String fragFilename : flist)
processDepmapFile("/etc/maven/fragments/" + fragFilename);
Document mapDocument; // process fragments is usr. Once packages are rebuilt, we can skip
debug("Loading depmap file: " + fileName); // fragments in /etc
try { fragmentDir = new File("/usr/share/maven-fragments");
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); flist = fragmentDir.list();
fact.setNamespaceAware(true); if (flist != null)
DocumentBuilder builder = fact.newDocumentBuilder(); for (String fragFilename : flist)
mapDocument = builder.parse(fileName); processDepmapFile("/usr/share/maven-fragments/" + fragFilename);
} catch (FileNotFoundException fnfe) {
System.err.println("ERROR: Unable to find map file: " + fileName);
fnfe.printStackTrace();
return;
} catch (IOException ioe) {
System.err.println("ERROR: I/O exception occured when opening map file");
ioe.printStackTrace();
return;
} catch (ParserConfigurationException pce) {
System.err.println("ERROR: Parsing of depmap file failed - configuration");
pce.printStackTrace();
return;
} catch (SAXException se) {
System.err.println("ERROR: Parsing of depmap file failed");
se.printStackTrace();
return;
}
NodeList depNodes = (NodeList) mapDocument.getElementsByTagName("dependency");
for (int i = 0; i < depNodes.getLength(); i++) { String customFileName = System.getProperty("maven.local.depmap.file",
Element depNode = (Element) depNodes.item(i); null);
if (customFileName != null) {
debug("Processing file: " + customFileName);
processDepmapFile(customFileName);
}
NodeList mavenNodeList = (NodeList) depNode.getElementsByTagName("maven"); }
if (mavenNodeList.getLength() != 1) {
debug("Number of maven sub-elements is not 1. Bailing from depmap generation");
debug("Maven node: " + depNode.getTextContent());
return;
}
ArtifactDefinition mavenAD = getArtifactDefinition((Element) mavenNodeList.item(0));
ArtifactDefinition jppAD = null; private static void processDepmapFile(String fileName) {
NodeList jppNodeList = (NodeList) depNode.getElementsByTagName("jpp");
if (jppNodeList.getLength() == 1) {
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, Document mapDocument;
jppAD.groupId + "," + jppAD.artifactId + "," + jppAD.version); debug("Loading depmap file: " + fileName);
} else { try {
debug("*** Adding: " + mavenAD.groupId+"," + mavenAD.artifactId + " => " DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
+ jppAD.groupId + "," + jppAD.artifactId + "," + jppAD.version + " to map..."); fact.setNamespaceAware(true);
DocumentBuilder builder = fact.newDocumentBuilder();
// we can wrap even old depmaps, no harm done
WrapFragmentStream wfs = new WrapFragmentStream(fileName);
mapDocument = builder.parse(wfs);
wfs.close();
} catch (FileNotFoundException fnfe) {
System.err.println("ERROR: Unable to find map file: " + fileName);
fnfe.printStackTrace();
return;
} catch (IOException ioe) {
System.err
.println("ERROR: I/O exception occured when opening map file");
ioe.printStackTrace();
return;
} catch (ParserConfigurationException pce) {
System.err
.println("ERROR: Parsing of depmap file failed - configuration");
pce.printStackTrace();
return;
} catch (SAXException se) {
System.err.println("ERROR: Parsing of depmap file failed");
se.printStackTrace();
return;
}
jppArtifactMap.put(mavenAD.groupId+","+mavenAD.artifactId, NodeList depNodes = (NodeList) mapDocument
jppAD.groupId + "," + jppAD.artifactId + "," + jppAD.version); .getElementsByTagName("dependency");
}
} else {
debug("Number of jpp sub-elements is not 1. Dropping dependency");
debug("*** Adding: " + mavenAD.groupId+","+mavenAD.artifactId+"," + " => " + "JPP/maven2,empty-dep,"+mavenAD.version + " to map...");
jppArtifactMap.put(mavenAD.groupId+","+mavenAD.artifactId, "JPP/maven2,empty-dep,"+mavenAD.version);
}
}
}
private static ArtifactDefinition getArtifactDefinition(Element element) { for (int i = 0; i < depNodes.getLength(); i++) {
ArtifactDefinition ad = new ArtifactDefinition(); Element depNode = (Element) depNodes.item(i);
NodeList nodes = element.getElementsByTagName("groupId");
if (nodes.getLength() != 1) {
debug("groupId definition not found in depmap");
return null;
}
ad.groupId = nodes.item(0).getTextContent();
nodes = element.getElementsByTagName("artifactId"); NodeList mavenNodeList = (NodeList) depNode
if (nodes.getLength() != 1) { .getElementsByTagName("maven");
debug("artifactId definition not found in depmap"); if (mavenNodeList.getLength() != 1) {
return null; debug("Number of maven sub-elements is not 1. Bailing from depmap generation");
} debug("Maven node: " + depNode.getTextContent());
ad.artifactId = nodes.item(0).getTextContent(); return;
}
ArtifactDefinition mavenAD = getArtifactDefinition((Element) mavenNodeList
.item(0));
nodes = element.getElementsByTagName("version"); ArtifactDefinition jppAD = null;
if (nodes.getLength() != 1) { NodeList jppNodeList = (NodeList) depNode
ad.version = "DUMMY_VER"; .getElementsByTagName("jpp");
} else {
ad.version = nodes.item(0).getTextContent();
}
return ad;
}
if (jppNodeList.getLength() == 1) {
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...");
public static void debug(String msg) { jppArtifactMap.put(mavenAD.groupId + ","
if (System.getProperty("maven.local.debug") != null) + mavenAD.artifactId + "," + mavenAD.version,
System.err.println(msg); jppAD.groupId + "," + jppAD.artifactId + ","
} + jppAD.version);
} else {
debug("*** Adding: " + mavenAD.groupId + ","
+ mavenAD.artifactId + " => " + jppAD.groupId + ","
+ jppAD.artifactId + "," + jppAD.version
+ " to map...");
jppArtifactMap.put(mavenAD.groupId + ","
+ mavenAD.artifactId, jppAD.groupId + ","
+ jppAD.artifactId + "," + jppAD.version);
}
} else {
debug("Number of jpp sub-elements is not 1. Dropping dependency");
debug("*** Adding: " + mavenAD.groupId + ","
+ mavenAD.artifactId + "," + " => "
+ "JPP/maven2,empty-dep," + mavenAD.version
+ " to map...");
jppArtifactMap.put(mavenAD.groupId + "," + mavenAD.artifactId,
"JPP/maven2,empty-dep," + mavenAD.version);
}
}
}
private static ArtifactDefinition getArtifactDefinition(Element element) {
ArtifactDefinition ad = new ArtifactDefinition();
NodeList nodes = element.getElementsByTagName("groupId");
if (nodes.getLength() != 1) {
debug("groupId definition not found in depmap");
return null;
}
ad.groupId = nodes.item(0).getTextContent();
nodes = element.getElementsByTagName("artifactId");
if (nodes.getLength() != 1) {
debug("artifactId definition not found in depmap");
return null;
}
ad.artifactId = nodes.item(0).getTextContent();
nodes = element.getElementsByTagName("version");
if (nodes.getLength() != 1) {
ad.version = "DUMMY_VER";
} else {
ad.version = nodes.item(0).getTextContent();
}
return ad;
}
public static void debug(String msg) {
if (System.getProperty("maven.local.debug") != null)
System.err.println(msg);
}
} }

View File

@ -1,7 +1,7 @@
Name: maven Name: maven
Version: 3.0.3 Version: 3.0.3
Release: 4%{?dist} Release: 5%{?dist}
Summary: Java project management and project comprehension tool Summary: Java project management and project comprehension tool
Group: Development/Tools Group: Development/Tools
@ -308,6 +308,9 @@ cp -pr target/site/apidocs/* $RPM_BUILD_ROOT%{_javadocdir}/%{name}
%changelog %changelog
* Fri Jun 10 2011 Stanislav Ochotnicky <sochotnicky@redhat.com> - 3.0.3-5
- Process fragments directly instead of maven2-depmap.xml
* Mon Jun 6 2011 Stanislav Ochotnicky <sochotnicky@redhat.com> - 3.0.3-4 * Mon Jun 6 2011 Stanislav Ochotnicky <sochotnicky@redhat.com> - 3.0.3-4
- Add help to mvn-rpmbuild and mvn-local (rhbz#710448) - Add help to mvn-rpmbuild and mvn-local (rhbz#710448)