Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/util/VersionUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
groupId=foo.bar
artifactId=foo-bar
version=1.2.3-SNAPSHOT