如何在Spring Boot Web应用程序中读取特定项目的元/清单。

发布于 2025-02-07 18:54:42 字数 2651 浏览 2 评论 0原文

我正在开发Spring Boot MVC应用程序。要求是读取清单。mf文件以获取这个春季启动应用程序的构建编号和应用程序版本编号等。

为此,我为清单类编写了以下BEAN定义,以便可以在控制器类中自动化它。

@Configuration
public class AppConfig{
    
    @Bean("manifest")
    public java.util.jar.Manifest getManifest()
    {
        // get the full name of the application manifest file
        String appManifestFileName = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString() + JarFile.MANIFEST_NAME;
        Enumeration resEnum;
        try
        {
            // get a list of all manifest files found in the jars loaded by the app
            resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
            while (resEnum.hasMoreElements())
            {
                try
                {
                    URL url = (URL) resEnum.nextElement();
                    System.out.println("Resource url=" + url.toString());
                    // is the app manifest file?
                    if (url.toString().equals(appManifestFileName))
                    {
                        // open the manifest
                        InputStream is = url.openStream();
                        if (is != null)
                        {
                            // read the manifest and return it to the application
                            Manifest manifest = new Manifest(is);
                            return manifest;
                        }
                    }
                }
                catch (Exception e)
                {
                    // Silently ignore wrong manifests on classpath?
                }
            }
        }
        catch (IOException e1)
        {
            // Silently ignore wrong manifests on classpath?
        }
        return null;
    }
}

以上代码取自在这里。但这无济于事。它总是给我无效的对象。

AppController.java

@RestController
public class AppController
{    
    @Autowired
    private Environment env;
    
    @Autowired
    @Qualifier("manifest")
    private Manifest manifest;
    
    @GetMapping("/get-app-details")
    public String getAppDetails()
    {
        Attributes mainAttributes = manifest.getMainAttributes();
        String buildNum = mainAttributes.getValue("Build-Number");
        buildNum = buildNum.substring(buildNum.lastIndexOf('_') + 1);
        String AppVersion = env.getProperty("App.Version") + "." + buildNum;
        return "Build Number - " + buildNum + ", AppVersion - " + AppVersion;
    }
}

附加信息 - 我正在使用Gradle将此应用程序构建为战争文件,并将其部署到外部Tomcat版本9中。

I'm working on a Spring Boot MVC application. The requirement is to read the MANIFEST.MF file to get build number and application version number etc of this spring boot app.

For this, I have written the following bean definition for Manifest Class so that I can autowire it in the Controller class.

@Configuration
public class AppConfig{
    
    @Bean("manifest")
    public java.util.jar.Manifest getManifest()
    {
        // get the full name of the application manifest file
        String appManifestFileName = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString() + JarFile.MANIFEST_NAME;
        Enumeration resEnum;
        try
        {
            // get a list of all manifest files found in the jars loaded by the app
            resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
            while (resEnum.hasMoreElements())
            {
                try
                {
                    URL url = (URL) resEnum.nextElement();
                    System.out.println("Resource url=" + url.toString());
                    // is the app manifest file?
                    if (url.toString().equals(appManifestFileName))
                    {
                        // open the manifest
                        InputStream is = url.openStream();
                        if (is != null)
                        {
                            // read the manifest and return it to the application
                            Manifest manifest = new Manifest(is);
                            return manifest;
                        }
                    }
                }
                catch (Exception e)
                {
                    // Silently ignore wrong manifests on classpath?
                }
            }
        }
        catch (IOException e1)
        {
            // Silently ignore wrong manifests on classpath?
        }
        return null;
    }
}

The above code is taken from here. But it didn't help. It is always giving me null object.

AppController.java

@RestController
public class AppController
{    
    @Autowired
    private Environment env;
    
    @Autowired
    @Qualifier("manifest")
    private Manifest manifest;
    
    @GetMapping("/get-app-details")
    public String getAppDetails()
    {
        Attributes mainAttributes = manifest.getMainAttributes();
        String buildNum = mainAttributes.getValue("Build-Number");
        buildNum = buildNum.substring(buildNum.lastIndexOf('_') + 1);
        String AppVersion = env.getProperty("App.Version") + "." + buildNum;
        return "Build Number - " + buildNum + ", AppVersion - " + AppVersion;
    }
}

Additional info - I'm using gradle to build this application as a war file and deploying it into external tomcat version 9.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文