How to read my META-INF/MANIFEST.MF file in a Spring Boot app?

后端 未结 7 1379
后悔当初
后悔当初 2020-12-30 02:31

I\'m trying to read my META-INF/MANIFEST.MF file from my Spring Boot web app (contained in a jar file).

I\'m trying the following code:

        Input         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 03:17

    I have it exploiting Spring's resource resolution:

    @Service
    public class ManifestService {
    
        protected String ciBuild;
    
        public String getCiBuild() { return ciBuild; }
    
        @Value("${manifest.basename:/META-INF/MANIFEST.MF}")
        protected void setManifestBasename(Resource resource) {
            if (!resource.exists()) return;
            try (final InputStream stream = resource.getInputStream()) {
                final Manifest manifest = new Manifest(stream);
                ciBuild = manifest.getMainAttributes().getValue("CI-Build");
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    

    Here we're getting CI-Build, and you can easily extend the example to load other attributes.

提交回复
热议问题