Last active
March 13, 2017 15:53
-
-
Save agentgt/997d7662f4942b4127525a8478ce6a95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* EXAMPLE USAGE. NOT TESTED THOUGH. | |
*/ | |
public class Example { | |
public static void example() { | |
//do a double lock synchronization block if you are setting a singleton | |
PomManifest pm = null; | |
try { | |
pm = getManifest(); | |
catch(IOException) { | |
pm = getTestManifest(); | |
} | |
} | |
private PomManifest getManifest() throws IOException { | |
Manifest mf = new Manifest(getResource("/META-INF/MANIFEST.MF"); | |
return PomManifest.fromManifest(mf); | |
} | |
private PomManifest getTestManifest() throws IOException { | |
File pom = new File(System.getProperty("user.dir"),"pom.xml"); | |
//System.err.println("Loading manifest from pom file: " + pom); | |
return PomManifest.fromPom(new FileInputStream(pom)); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.snaphop.util; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Map; | |
import java.util.jar.Attributes; | |
import java.util.jar.Manifest; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.xpath.XPath; | |
import javax.xml.xpath.XPathConstants; | |
import javax.xml.xpath.XPathExpressionException; | |
import javax.xml.xpath.XPathFactory; | |
import org.w3c.dom.Document; | |
import org.xml.sax.SAXException; | |
import com.google.common.base.Optional; | |
import static java.util.jar.Attributes.Name.*; | |
public class PomManifest { | |
private static final String MAVEN_PROJECT_VERSION = "Maven-project-version"; | |
private static final String MAVEN_PROJECT_ARTIFACT_ID = "Maven-project-artifactId"; | |
private static final String MAVEN_PROJECT_GROUP_ID = "Maven-project-groupId"; | |
private static final String MAVEN_PROJECT_NAME = "Maven-project-name"; | |
private final String groupId; | |
private final String artifactId; | |
private final String version; | |
private final String name; | |
public PomManifest(String groupId, String artifactId, String version, String name) { | |
super(); | |
this.groupId = groupId; | |
this.artifactId = artifactId; | |
this.version = version; | |
this.name = name; | |
} | |
public static PomManifest fromPom( | |
InputStream stream) throws IOException { | |
XmlHelper h = XmlHelper.create(stream); | |
String groupId = h.findString("/project/groupId", "/project/parent/groupId") | |
.orNull(); | |
String artifactId = h.findString("/project/artifactId") | |
.orNull(); | |
String name = h.findString("/project/name") | |
.orNull(); | |
String version = h.findString("/project/version", "/project/parent/version") | |
.orNull(); | |
return new PomManifest(groupId, artifactId, version, name); | |
} | |
public static PomManifest fromManifest(Manifest m) { | |
Attributes ats = m.getMainAttributes(); | |
String name = findAttribute(ats,MAVEN_PROJECT_NAME, IMPLEMENTATION_TITLE.toString()) | |
.orNull(); | |
String groupId = findAttribute(ats,MAVEN_PROJECT_GROUP_ID, IMPLEMENTATION_VENDOR_ID.toString()) | |
.orNull(); | |
String artifactId = findAttribute(ats, MAVEN_PROJECT_ARTIFACT_ID, IMPLEMENTATION_VENDOR.toString()) | |
.orNull(); | |
String version = findAttribute(ats, MAVEN_PROJECT_VERSION, IMPLEMENTATION_VERSION.toString()) | |
.orNull(); | |
return new PomManifest(groupId, artifactId, version, name); | |
} | |
public Manifest toManifest() { | |
Manifest m = new Manifest(); | |
m.getMainAttributes().putValue(IMPLEMENTATION_TITLE.toString(), name); | |
m.getMainAttributes().putValue(IMPLEMENTATION_VENDOR_ID.toString(), groupId); | |
m.getMainAttributes().putValue(IMPLEMENTATION_VENDOR.toString(), groupId); | |
m.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), version); | |
addMavenInfo(this, m); | |
return m; | |
} | |
private final String PROP_PREFIX = "maven.project."; | |
public void copyToMap(Map<String, ? super String> m) { | |
copyToMap(m, PROP_PREFIX); | |
} | |
public void copyToMap(Map<String, ? super String> m, String PROP_PREFIX) { | |
m.put(PROP_PREFIX + "name", name); | |
m.put(PROP_PREFIX + "groupId", groupId); | |
m.put(PROP_PREFIX + "artifactId", artifactId); | |
m.put(PROP_PREFIX + "version", version); | |
m.put(PROP_PREFIX + "name", name); | |
} | |
public static Optional<String> findAttribute(Attributes ats, String ... names) { | |
for (String name : names) { | |
String v = ats.getValue(name); | |
if (v != null && ! v.isEmpty()) { | |
return Optional.of(v); | |
} | |
} | |
return Optional.absent(); | |
} | |
public static void addMavenInfo(PomManifest pm, Manifest m) { | |
synchronized(m) { | |
m.getMainAttributes().putValue(MAVEN_PROJECT_NAME, pm.name); | |
m.getMainAttributes().putValue(MAVEN_PROJECT_GROUP_ID, pm.groupId); | |
m.getMainAttributes().putValue(MAVEN_PROJECT_ARTIFACT_ID, pm.artifactId); | |
m.getMainAttributes().putValue(MAVEN_PROJECT_VERSION, pm.version); | |
} | |
} | |
public String getGroupId() { | |
return groupId; | |
} | |
public String getArtifactId() { | |
return artifactId; | |
} | |
public String getVersion() { | |
return version; | |
} | |
public String getName() { | |
return name; | |
} | |
@Override | |
public String toString() { | |
return "PomManifest [groupId=" | |
+ groupId | |
+ ", artifactId=" | |
+ artifactId | |
+ ", version=" | |
+ version | |
+ ", name=" | |
+ name | |
+ "]"; | |
} | |
static class XmlHelper { | |
private final Document doc; | |
private final XPath xpath; | |
private XmlHelper(Document doc, XPath xpath) { | |
super(); | |
this.doc = doc; | |
this.xpath = xpath; | |
} | |
public static XmlHelper create( | |
InputStream stream) throws IOException { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
Document doc; | |
try { | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
doc = builder.parse(stream); | |
} | |
catch (ParserConfigurationException e) { | |
throw new IOException(e); | |
} | |
catch (SAXException e) { | |
throw new IOException(e); | |
} | |
// Element root = doc.getDocumentElement(); | |
// http://maven.apache.org/POM/4.0.0" | |
XPath xpath = XPathFactory.newInstance() | |
.newXPath(); | |
return new XmlHelper(doc, xpath); | |
} | |
public Optional<String> findString( | |
String... paths) throws IOException { | |
try { | |
for (String path : paths) { | |
String v = (String) xpath.evaluate(path, doc, XPathConstants.STRING); | |
if (v == null || v.isEmpty()) | |
continue; | |
return Optional.of(v); | |
} | |
return Optional.absent(); | |
} | |
catch (XPathExpressionException e) { | |
throw new IOException(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment