Maven, Tomcat e applicazioni WAR: recuperare la versione del war dal MANIFEST.MF

Innanzitutto occorre avere il file MANIFEST.MF all’interno della directory META-INF. Per farlo con maven è sufficiente configurare il plugin maven-war-plugin in questo modo:

<configuration>
        ...
	<archive>
		<manifest>
			<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
			<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
		</manifest>
	</archive>
        ...
</configuration>

Il file MANIFEST.MF generato avrà questi campi:

Manifest-Version: ******
Implementation-Title: ****** 
Implementation-Version: 3.1.2-SHAPSHOT
Implementation-Vendor-Id: *******
Build-Jdk: 1.7.0_11
Built-By: nicola.colonna
Created-By: Maven Integration for Eclipse
Specification-Title: *******
Specification-Version: 3.1.2-SHAPSHOT

Visto che siamo all’interno dell’ambiente di Tomcat ci occorre il SerlvetContext. A questo punto per leggere il file ed estrarre la proprietà Implementation-Version:

InputStream manifestStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
 
try {
	Manifest manifest = new Manifest(manifestStream);
	Attributes attributes = manifest.getMainAttributes();
	String impVersion = attributes.getValue("Implementation-Version");
} catch (IOException ex) {
	logger.error("Impossibile leggere il MANIFEST.MF: " + ex.getMessage());
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>