读取 Spring 引导应用程序中的清单文件
Read Manifest file in Spring boot application
我们正在使用 Spring-boot 来构建微服务。在我的项目设置中,我们有一个名为 platform-boot 的通用 Maven 模块,主模块 class 带有注释 SpringBootApplication
如果我们想创建一个新的微服务(比如Service-1),我们只需添加platform-boot[=39=的依赖] 模块并在 pom.xml
中提供 main-class 路径,我们就可以开始了。
问题是当我尝试通过在依赖模块的 'main-class' 中编写代码来读取 Service-1 的 Manifest.MF
文件时。它读取 platform-boot 的 Manifest.MF
文件。
下面是我如何在主 class.
中读取 Manifest.MF
文件的代码片段
MyMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//Returns the path of MyMain.class which is nested jar
请建议一种读取 Service-1 的 Manifest.MF
文件的方法。
PS:我想读取 Maifest.MF
文件以获得 Implementation-Version。请建议是否还有其他获取方式。
您好,能否请您详细说明阅读您的服务的需要-1manifest.mf?
如果您只想将 service1 作为父公共模块中的依赖项并且不应该与您的 service1 可启动应用程序冲突,您可以通过 spring-boot-maven- 中的 exec 配置生成两个 jar插件 .
我找到了两种方法来解决这个问题:
我们可以使用maven-dependency-plugin解压子jar
执行阶段 prepare-package
。该插件将提取
class 文件从我的 platform-boot jar 到我的 Service-1.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>my.platform</groupId>
<artifactId>platform-boot</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>**/*.class,**/*.xml,**/*.text</includes>
<excludes>**/*test.class</excludes>
</artifactItem>
</artifactItems>
<includes>**/*.java, **/*.text</includes>
<excludes>**/*.properties</excludes>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
第二种方法就简单多了,在spring-boot-maven-plugin中添加一个目标
build-info
。这将在您的 META-INF 文件夹中写入一个文件 build-info.properties
,并且可以通过以下代码访问。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
在您的主要方法中,您可以使用 BuildProperties 获取此信息
bean 已经在 ApplicationContext 中注册。
ApplicationContext ctx = SpringApplication.run(Application.class, args);
BuildProperties properties = ctx.getBean(BuildProperties.class);
其实Spring-actuator也是用这个BuildProperties
来获取构建
有助于监控的信息。
我们正在使用 Spring-boot 来构建微服务。在我的项目设置中,我们有一个名为 platform-boot 的通用 Maven 模块,主模块 class 带有注释 SpringBootApplication
如果我们想创建一个新的微服务(比如Service-1),我们只需添加platform-boot[=39=的依赖] 模块并在 pom.xml
中提供 main-class 路径,我们就可以开始了。
问题是当我尝试通过在依赖模块的 'main-class' 中编写代码来读取 Service-1 的 Manifest.MF
文件时。它读取 platform-boot 的 Manifest.MF
文件。
下面是我如何在主 class.
中读取Manifest.MF
文件的代码片段
MyMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//Returns the path of MyMain.class which is nested jar
请建议一种读取 Service-1 的 Manifest.MF
文件的方法。
PS:我想读取 Maifest.MF
文件以获得 Implementation-Version。请建议是否还有其他获取方式。
您好,能否请您详细说明阅读您的服务的需要-1manifest.mf?
如果您只想将 service1 作为父公共模块中的依赖项并且不应该与您的 service1 可启动应用程序冲突,您可以通过 spring-boot-maven- 中的 exec 配置生成两个 jar插件 .
我找到了两种方法来解决这个问题:
我们可以使用maven-dependency-plugin解压子jar 执行阶段
prepare-package
。该插件将提取 class 文件从我的 platform-boot jar 到我的 Service-1.<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>unpack</id> <phase>prepare-package</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>my.platform</groupId> <artifactId>platform-boot</artifactId> <type>jar</type> <overWrite>false</overWrite> <outputDirectory>${project.build.directory}/classes</outputDirectory> <includes>**/*.class,**/*.xml,**/*.text</includes> <excludes>**/*test.class</excludes> </artifactItem> </artifactItems> <includes>**/*.java, **/*.text</includes> <excludes>**/*.properties</excludes> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> </configuration> </execution> </executions> </plugin>
第二种方法就简单多了,在spring-boot-maven-plugin中添加一个目标
build-info
。这将在您的 META-INF 文件夹中写入一个文件build-info.properties
,并且可以通过以下代码访问。<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
在您的主要方法中,您可以使用 BuildProperties 获取此信息 bean 已经在 ApplicationContext 中注册。
ApplicationContext ctx = SpringApplication.run(Application.class, args); BuildProperties properties = ctx.getBean(BuildProperties.class);
其实Spring-actuator也是用这个
BuildProperties
来获取构建 有助于监控的信息。