Monday, December 29, 2014

Get package version from MANIFEST file

In pom.xml, specify default ImplementationEntries and SpecificationEntries as below:
 

          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                        <manifestEntries>
                            <Implementation-Version>${project.version}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                    <archiveClasses>true</archiveClasses>
                </configuration>
            </plugin>


These fields will be automatically added to MANIFEST file.

In Java class, use this snippet to read value:
   @Override
    public BuildVersion getVersion() throws IOException {
        Package p = this.getClass().getPackage();
        BuildVersion buildVersion = new BuildVersion();
        buildVersion.setBuildName(p.getImplementationTitle());
        buildVersion.setVersion(p.getImplementationVersion());
        return buildVersion;       
    }


(BuildVersion is a custom class that contains result.)

Note: war file has to be put in server. At that time manifest file will be available to read properly.

No comments:

Post a Comment