0
votes

I am using:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.4</version>
</plugin>

with Spring Boot application I am adding BuildNumber into META-INF\MANIFEST.MF in WAR file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
</plugin>

When I start this application like:

java -jar MyApp.war

everything is fine.

When I start it in tomcat (apache-tomcat-8.0.33 ) as ROOT application - extracted resources into ROOT directory (no WAR file used) application is not able to find my BuildNumber.

method which is looking for BuildNumber is here:

@PostConstruct
private void init() {
    try {
        Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            LOGGER.debug("Looking into URL: {} for BuildNumnber", url);
            Manifest manifest = new Manifest(url.openStream());
            Attributes mainAttributes = manifest.getMainAttributes();
            if (mainAttributes != null) {
                Object obj = mainAttributes.get(IMPLEMENTATION_BUILD);
                if (obj != null) {
                    buildNumber = obj;
                    LOGGER.info("Found P3J Build Number: " + buildNumber);
                    break;
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to determine build number. {}", e);
    }
}

Any advice?

//edit

This code is looking only into manifests in JAR files - my BuildNumber is in WAR file and in Tomcat it is extracted into 'webapps/ROOT/'. But there is still MANIFEST.MF with proper BuildNumber but is not scanned by this code.

How to check content of \webapps\ROOT\META-INF\MANIFEST.MF in Tomcat?

//edit

There is answer but - now I am looking how to do that in Spring Boot application.

1
I think this might not help you, but we for example simple use Spring-Boots banner.txt in order to log the application version on startup, might this work for you? It works like this: ${AnsiColor.RED} :: ${application.title} - ${application.version} - Spring Boot${spring-boot.formatted-version} :: ${Ansi.DEFAULT}Kevin Wittek

1 Answers

0
votes

Looks like this method solve my issue:

@RestController
@RequestMapping("/build")
public class BuildNumberResource {

@Autowired
private ServletContext servletContext;

@PostConstruct
private void init() {

    try {
        InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
        Manifest manifest = new Manifest(inputStream);
        Attributes mainAttributes = manifest.getMainAttributes();
        if (mainAttributes != null) {
            Object obj = mainAttributes.get(IMPLEMENTATION_BUILD);
            if (obj != null) {
                buildNumber = obj;
                LOGGER.info("Found P3J Build Number in ServletContext.MANIFEST: " + buildNumber);
                return;
            }
        }
    } catch (IOException e) {
        LOGGER.error("Unable to extract BuildNumber from ServletContext. {}", e);
    }

// edit

looks like this second part is no longer required

    try {
        Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            LOGGER.debug("Looking into URL: {} for BuildNumnber", url);
            Manifest manifest = new Manifest(url.openStream());
            Attributes mainAttributes = manifest.getMainAttributes();
            if (mainAttributes != null) {
                Object obj = mainAttributes.get(IMPLEMENTATION_BUILD);
                if (obj != null) {
                    buildNumber = obj;
                    LOGGER.info("Found P3J Build Number: " + buildNumber);
                    return;
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Unable to determine build number. {}", e);
    }
}

sorry for using stackoverflowas my Rubber duck ;)