From 787749988f028ae16aef055346d6fee707f0d5bf Mon Sep 17 00:00:00 2001 From: James Roper Date: Tue, 3 Jan 2012 20:43:57 +0100 Subject: [PATCH] Added support for leading versions from maven pom.properties files --- .../jackson/core/util/VersionUtil.java | 36 +++++++++++++++++++ .../jackson/core/util/TestVersionUtil.java | 6 +++- .../maven/foo/bar/foo-bar/pom.properties | 3 ++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/META-INF/maven/foo/bar/foo-bar/pom.properties diff --git a/src/main/java/com/fasterxml/jackson/core/util/VersionUtil.java b/src/main/java/com/fasterxml/jackson/core/util/VersionUtil.java index fc084e1fcd..a70913d8de 100644 --- a/src/main/java/com/fasterxml/jackson/core/util/VersionUtil.java +++ b/src/main/java/com/fasterxml/jackson/core/util/VersionUtil.java @@ -1,6 +1,7 @@ package com.fasterxml.jackson.core.util; import java.io.*; +import java.util.Properties; import java.util.regex.Pattern; import com.fasterxml.jackson.core.Version; @@ -97,6 +98,41 @@ public static Version versionFor(Class cls) return (version == null) ? Version.unknownVersion() : version; } + /** + * Will attempt to load the maven version for the given groupId and + * artifactId. Maven puts a pom.properties file in + * META-INF/maven/groupId/artifactId, containing the groupId, + * artifactId and version of the library. + * + * @param classLoader the ClassLoader to load the pom.properties file from + * @param groupId the groupId of the library + * @param artifactId the artifactId of the library + * @return The version + */ + public static Version mavenVersionFor(ClassLoader classLoader, String groupId, String artifactId) { + InputStream pomPoperties = classLoader.getResourceAsStream("META-INF/maven/" + groupId.replaceAll("\\.", "/") + + "/" + artifactId + "/pom.properties"); + if (pomPoperties != null) { + try { + Properties props = new Properties(); + props.load(pomPoperties); + String versionStr = props.getProperty("version"); + String pomPropertiesArtifactId = props.getProperty("artifactId"); + String pomPropertiesGroupId = props.getProperty("groupId"); + return parseVersion(versionStr, pomPropertiesGroupId, pomPropertiesArtifactId); + } catch (IOException e) { + // Ignore + } finally { + try { + pomPoperties.close(); + } catch (IOException e) { + // Ignore + } + } + } + return Version.unknownVersion(); + } + /** * Use variant that takes three arguments instead * diff --git a/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java b/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java index 05a4a5c3bc..f52d1773fc 100644 --- a/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java +++ b/src/test/java/com/fasterxml/jackson/core/util/TestVersionUtil.java @@ -1,7 +1,6 @@ package com.fasterxml.jackson.core.util; import com.fasterxml.jackson.core.Version; -import com.fasterxml.jackson.core.util.VersionUtil; public class TestVersionUtil extends com.fasterxml.jackson.test.BaseTest { @@ -16,4 +15,9 @@ public void testVersionParsing() { assertEquals(new Version(1, 2, 15, "foo"), VersionUtil.parseVersion("1.2.15-foo")); } + + public void testMavenVersionParsing() { + assertEquals(new Version(1, 2, 3, "SNAPSHOT", "foo.bar", "foo-bar"), + VersionUtil.mavenVersionFor(TestVersionUtil.class.getClassLoader(), "foo.bar", "foo-bar")); + } } diff --git a/src/test/resources/META-INF/maven/foo/bar/foo-bar/pom.properties b/src/test/resources/META-INF/maven/foo/bar/foo-bar/pom.properties new file mode 100644 index 0000000000..5ae4d1680a --- /dev/null +++ b/src/test/resources/META-INF/maven/foo/bar/foo-bar/pom.properties @@ -0,0 +1,3 @@ +groupId=foo.bar +artifactId=foo-bar +version=1.2.3-SNAPSHOT \ No newline at end of file