com.redhat.ceylon.cmr.maven.MavenUtils Maven / Gradle / Ivy
package com.redhat.ceylon.cmr.maven;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.redhat.ceylon.cmr.api.ModuleDependencyInfo;
import com.redhat.ceylon.cmr.api.ModuleInfo;
import com.redhat.ceylon.common.Backends;
import com.redhat.ceylon.model.cmr.ModuleScope;
/**
* Utility class which does not depend on anything Aether (optional dep), so can be safely public
*
* @author Stéphane Épardaud
*/
public class MavenUtils {
public static String getDefaultMavenSettings() {
String path = System.getProperty("maven.repo.local");
if (path != null) {
File file = new File(path, "settings.xml");
if (file.exists())
return file.getAbsolutePath();
}
path = System.getProperty("user.home");
if (path != null) {
File file = new File(path, ".m2/settings.xml");
if (file.exists())
return file.getAbsolutePath();
}
path = System.getenv("M2_HOME");
if (path != null) {
File file = new File(path, "conf/settings.xml");
if (file.exists())
return file.getAbsolutePath();
}
return "classpath:settings.xml";
}
public static ModuleInfo getDependencies(File file, String name, String version) throws IOException {
try(InputStream is = new FileInputStream(file)){
return getDependencies(is, name, version);
}
}
public static ModuleInfo getDependencies(InputStream stream, String name, String version) throws IOException {
Document doc;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.parse(stream);
} catch (ParserConfigurationException e) {
throw new IOException(e);
} catch (SAXException e) {
throw new IOException(e);
}
doc.getDocumentElement().normalize();
Element root = doc.getDocumentElement();
String modGroupId = getText(root, "groupId");
// can be null, inherited from parent
if(modGroupId == null){
Element parent = getFirstElement(root, "parent");
if(parent != null)
modGroupId = getText(parent, "groupId");
}
String modArtifactId = getText(root, "artifactId");
String modVersion = getText(root, "version");
String modName = modGroupId + ":" + modArtifactId;
if(name != null && !name.equals(modName))
return null;
if(version != null && !version.equals(modVersion))
return null;
Element deps = getFirstElement(root, "dependencies");
Set ret = new HashSet<>();
if(deps != null){
NodeList depList = deps.getElementsByTagName("dependency");
if(depList != null){
for(int i=0;i 0)
builder.append("\n");
builder.append(desc);
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy