View Javadoc

1   package com.atlassian.httpclient.apache.httpcomponents;
2   
3   import com.google.common.io.Closeables;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.net.URL;
10  import java.util.Properties;
11  
12  import static com.google.common.io.Resources.getResource;
13  import static java.lang.String.format;
14  
15  final class MavenUtils {
16      private static final Logger logger = LoggerFactory.getLogger(MavenUtils.class);
17  
18      private static final String UNKNOWN_VERSION = "unknown";
19  
20      static String getVersion(String groupId, String artifactId) {
21          final Properties props = new Properties();
22          InputStream is = null;
23          try {
24              is = getPomInputStreamUrl(groupId, artifactId).openStream();
25              props.load(is);
26              return props.getProperty("version", UNKNOWN_VERSION);
27          } catch (Exception e) {
28              logger.debug("Could not find version for maven artifact {}:{}", groupId, artifactId);
29              logger.debug("Got the following exception:", e);
30              return UNKNOWN_VERSION;
31          } finally {
32              try {
33                  Closeables.close(is, true);
34              } catch (IOException e) {
35                  logger.debug("Could not find version for maven artifact {}:{}", groupId, artifactId);
36                  logger.debug("IOException should not have been thrown.", e);
37              }
38          }
39      }
40  
41      private static URL getPomInputStreamUrl(String groupId, String artifactId) {
42          return getResource(MavenUtils.class, getPomFilePath(groupId, artifactId));
43      }
44  
45      private static String getPomFilePath(String groupId, String artifactId) {
46          return format("/META-INF/maven/%s/%s/pom.properties", groupId, artifactId);
47      }
48  }