Skip to content

Instantly share code, notes, and snippets.

@ripla
Created July 8, 2014 09:54
Show Gist options
  • Save ripla/af8c5f359d5802f488a9 to your computer and use it in GitHub Desktop.
Save ripla/af8c5f359d5802f488a9 to your computer and use it in GitHub Desktop.
Code required to read Maven WAR artifact version in java.
<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>
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