我需要读取 Spring Boot MVC Web 应用程序的 META/MANIFEST.MF 文件中提供的信息,并使用此信息来执行一些业务逻辑。我使用 gradle 将应用程序构建为 war 文件并将其部署到外部 tomcat 中。
我已经尝试过以下方法:
@Configuration
public class AppConfig
{
@Bean("manifest")
public java.util.jar.Manifest getManifest() throws IOException
{
InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
Manifest manifestObj = new Manifest(inputFile);
return manifestObj;
}
}
应用服务.java
@Service
public class AppService
{
@Autowired
@Qualifier("manifest")
private Manifest manifest;
@PostConstruct
public String init()
{
Attributes mainAttributes = manifest.getMainAttributes();
String buildNum = mainAttributes.getValue("Build-Number");
String customPropInfo= mainAttributes.getValue("customPropInfo");
String systemPrp1= buildNum + "_" + "SomeBusinessInfoLogic1";
String systemPrp2= customPropInfo+ "_" + "SomeBusinessInfoLogic2";
//Some Business Logic with these attributes systemPrp, systemPrp2
logger.info("System Props are updated");
}
}
我两者都为空buildNum
and customPropInfo
.
注意:我尝试创建 Manifest bean 类似this https://stackoverflow.com/q/72640712/9145082这是我创建的。根据 @M.Deinum 建议,我在这里创建这个新问题。我也尝试了解决方案here https://stackoverflow.com/questions/3777055/reading-manifest-mf-file-from-jar-file-using-java/3777116#3777116这对我不起作用。
@M.Deinum 建议使用 Spring Boot 的 Actuator Info 端点。但是,当我们想要访问应用程序外部的信息时,此端点非常有用,但我的要求有所不同,因为我需要 MANIFEST.MF 文件中可用的数据来在应用程序内执行某些业务操作。
当我尝试时出现以下错误这个解决方案 https://stackoverflow.com/a/26687713/9145082 "/META-INF/MANIFEST.MF
".
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'manifest' defined in class path resource [com/abc/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.jar.Manifest]: Factory method 'getManifest' threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read(byte[], int, int)" because "this.in" is null
有人可以帮我从 Spring Boot MVC Web 应用程序的 META/MANIFEST.MF 中读取信息吗?
UPDATE1:当我尝试打印主要属性时,我得到以下主要属性。但问题是当我尝试将战争部署到外部 tomcat 时。
System.out.println("Manifest MainAttributes = " +manifestObj.getMainAttributes().keySet());
Output:
Manifest MainAttributes = [Manifest-Version, Implementation-Title, Automatic-Module-Name, Implementation-Version, Built-By, Spring-Boot-Jar-Type, Build-Jdk-Spec]
UPDATE2:我已更新到 AppService.java 以打印自动装配 Manifest 对象中可用的信息。像下面这样:
@Configuration
public class AppConfig
{
@Bean("manifest")
public java.util.jar.Manifest getManifest() throws IOException
{
InputStream inputFile = new ClassPathResource("/META-INF/MANIFEST.MF").getInputStream();
Manifest manifestObj = new Manifest(inputFile);
System.out.println("Manifest Manifest-Version = " +manifestObj.getMainAttributes().getValue("Manifest-Version"));
System.out.println("Manifest KeySet = " +manifestObj.getMainAttributes().keySet());
return manifestObj;
}
}
@Service
public class AppService
{
@Autowired
@Qualifier("manifest")
private Manifest manifest;
@PostConstruct
public String init()
{
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.forEach((k,v) -> {
System.out.println("AppService.init(): Key = "+k+", Value = "+v);
});
String buildNum = mainAttributes.getValue("Build-Number");
String customPropInfo= mainAttributes.getValue("customPropInfo");
String systemPrp1= buildNum + "_" + "SomeBusinessInfoLogic1";
String systemPrp2= customPropInfo+ "_" + "SomeBusinessInfoLogic2";
//Some Business Logic with these attributes systemPrp, systemPrp2
logger.info("System Props are updated");
}
}
我在控制台上看到以下输出:
AppService.init(): Key = Implementation-Title, Value = Apache Tomcat Bootstrap
AppService.init(): Key = Implementation-Version, Value = 9.0.12
AppService.init(): Key = Specification-Vendor, Value = Apache Software Foundation
AppService.init(): Key = Specification-Title, Value = Apache Tomcat Bootstrap
AppService.init(): Key = Class-Path, Value = commons-daemon.jar
AppService.init(): Key = Manifest-Version, Value = 1.0
AppService.init(): Key = Main-Class, Value = org.apache.catalina.startup.Bootstrap
AppService.init(): Key = Implementation-Vendor, Value = Apache Software Foundation
AppService.init(): Key = Ant-Version, Value = Apache Ant 1.9.9
AppService.init(): Key = X-Compile-Target-JDK, Value = 1.8
AppService.init(): Key = X-Compile-Source-JDK, Value = 1.8
AppService.init(): Key = Created-By, Value = some xyz
AppService.init(): Key = Specification-Version, Value = 9.0
因此,仅根据上面的输出,我认为 MANIFEST.MF 不是特定于应用程序的,而是来自commons-daemon.jar.