【问题标题】:Read Manifest file in Spring boot application在 Spring Boot 应用程序中读取 Manifest 文件
【发布时间】:2018-05-11 13:01:07
【问题描述】:

我们正在使用 Spring-boot 来构建微服务。在我的项目设置中,我们有一个名为 platform-b​​oot 的通用 maven 模块,主类带有注释 SpringBootApplication

如果我们想创建一个新的微服务(比如Service-1),我们只需添加一个platform-b​​oot模块的依赖并提供 pom.xml 中的 main-class 路径,我们可以开始了。

问题是当我尝试通过在依赖模块的“主类”中编写代码来读取 Service-1Manifest.MF 文件时。它读取platform-b​​ootManifest.MF文件。

下面是我在主类中读取Manifest.MF文件的代码sn-p。

MyMain.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//Returns the path of MyMain.class which is nested jar

请提出一种读取Service-1Manifest.MF文件的方法。

PS:我想阅读Maifest.MF 文件以获取实施版本。请建议是否还有其他获取方式。

【问题讨论】:

    标签: java maven spring-boot manifest manifest.mf


    【解决方案1】:

    我找到了两种方法来解决这个问题:

    1. 我们可以使用ma​​ven-dependency-plugin来解压子jar,同时 执行阶段prepare-package。该插件将提取 类文件从我的 platform-b​​oot 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>  
      
    2. 第二种方法更简单,在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来获取build 有助于监控的信息。

    【讨论】:

    • 如何使用 gradle 创建 'build-info'
    【解决方案2】:

    您好,请您详细说明阅读您的 service-1manifest.mf 的需要吗?

    如果您只想将 service1 作为父公共模块中的依赖项,并且不应该与您的 service1 可启动应用程序冲突,您可以通过 spring-boot-maven-plugin 中的 exec 配置生成两个 jar。

    【讨论】:

    • 我需要从 Service-1 的 Manifest.MF 中提取 Implementation-Version 以进行日志记录。我尝试过spring提供的${application.version}Manifest.MF读取Implementation-Version,但它从MyMain类所在的'platform-b​​oot'读取版本。
    猜你喜欢
    • 2018-02-19
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 2020-09-12
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多