Created
July 8, 2014 09:54
-
-
Save ripla/af8c5f359d5802f488a9 to your computer and use it in GitHub Desktop.
Code required to read Maven WAR artifact version in java.
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
<plugin> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.3</version> | |
<configuration> | |
<archive> | |
<manifest> | |
<addDefaultImplementationEntries>true</addDefaultImplementationEntries> | |
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> | |
</manifest> | |
</archive> | |
<archiveClasses>true</archiveClasses> | |
</plugin> |
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
public final class Version { | |
private static Logger logger = LoggerFactory.getLogger(Version.class); | |
private static String appVersion; | |
private Version() { | |
} | |
public static String getVersionString() { | |
String versionString = "no version info"; | |
try { | |
versionString = String.format("Version %s", getApplicationVersion()); | |
} catch (final Exception e) { | |
logger.error("Error fetching version: " + e); | |
} | |
return versionString; | |
} | |
private static String getApplicationVersion() { | |
if (appVersion == null) { | |
throw new IllegalStateException("No application Version set"); | |
} | |
return appVersion; | |
} | |
public static void setVersionFromServletContext(final ServletContext servletContext) { | |
final String name = "/META-INF/MANIFEST.MF"; | |
final Properties props = new Properties(); | |
try { | |
props.load(servletContext.getResourceAsStream(name)); | |
setApplicationVersion((String) props.get("Implementation-Version")); | |
} catch (final IOException e) { | |
logger.error("Error fetching version: " + e); | |
} | |
} | |
private static void setApplicationVersion(final String string) { | |
appVersion = string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment