首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring Boot Starter中获取ArtifactId和版本

在Spring Boot Starter中获取ArtifactId和版本
EN

Stack Overflow用户
提问于 2017-06-28 01:56:01
回答 2查看 13.5K关注 0票数 15

我目前正在开发一个Spring Boot Starter,它将托管一个host风格的web服务,其中包含一些关于正在运行的应用程序的元数据。

我在从我的mainfest文件中提取artifactId和versionId时遇到了困难。我认为我的问题是自动配置类是在主测试应用程序之前加载的,所以清单还不能被发现。我不确定我的逻辑是否正确,或者我是否从错误的角度来处理这个问题。

我最初是按照以下tutorial进行设置的。

这给了我三个不同的项目

Generic Spring Services with no context AutoConfiguration project for these services Spring Boot starter

作为最终结果,我将starter与一个测试项目配对。

目前,maven正在与Spring Boot一起使用,以生成清单文件。

Implementation-Title: MyExampleProjectWithCustomStarter Implementation-Version: 0.0.1-SNAPSHOT Archiver-Version: Plexus Archiver Built-By: mcf Implementation-Vendor-Id: com.coolCompany Spring-Boot-Version: 1.5.4.RELEASE Implementation-Vendor: Pivotal Software, Inc. Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: com.coolcompany.SpringBootExampleApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Created-By: Apache Maven 3.5.0 Build-Jdk: 1.8.0_131 Implementation-URL: http://someurl

但是,当我试图从我的通用服务包中找到示例项目的清单文件时,我找不到该文件。

代码语言:javascript
复制
  private String getApplicationVersion(String applicationName, List<Attributes> manifests) {
    String unknownVersion = "0.0.0-UNKNOWN";

    for (Attributes attr : manifests) {
      String title = attr.getValue(IMPL_TITLE);
      String version = attr.getValue(IMPL_VERSION);
      if (version != null) {
        if (applicationName.equalsIgnoreCase(title)) {
          return title + ' ' + version;
        }
      }
    }
    log.warn(
        "Could not find MANIFEST file with '" + applicationName + "' as Implementation-Title."
        + " Meta-API will return buildVersion '" + unknownVersion + "'.");

    return applicationName + ' ' + unknownVersion;
  }

  private List<Attributes> loadManifestFiles() {
    List<Attributes> manifests = new ArrayList<>();
    try {
      Enumeration<URL> resources =
          Thread.currentThread().getContextClassLoader().getResources("/META-INF/MANIFEST.MF");
      while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        try (InputStream is = url.openStream()) {
          manifests.add(new Manifest(is).getMainAttributes());
          System.out.println("Manifest size:" + manifests.size());
        } catch (IOException e) {
          log.error("Failed to read manifest from " + url, e);
        }
      }
    } catch (IOException e) {
      log.error("Failed to get manifest resources", e);
    }
    return manifests;
  }

当前清单实现-标题:

Spring Boot Web Starter Spring Boot Starter Spring Boot Spring Boot AutoConfigure Spring Boot Logging Starter null null jcl-over-slf4j null log4j-over-slf4j null Spring Boot Tomcat Starter Apache Tomcat Apache Tomcat Apache Tomcat hibernate-validator null JBoss Logging 3 ClassMate jackson-databind Jackson-annotations Jackson-core spring-web spring-aop spring-beans spring-context spring-webmvc spring-expression Spring Boot Actuator Starter Spring Boot Actuator null ** MyCustom-spring-boot-starter ** MyGenericSpringService null null null Metrics Core JVM Integration for Metrics null null Jackson datatype: JSR310 ** MyService-spring-boot-autoconfigure slf4j-api spring-core **缺少MyExampleProjectWithCustomStarter

清单记录数量: 44条

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-30 02:52:04

经过很多努力,我找到了一个令人惊讶的简单的答案。这就是spring-boot-actuator获取信息的方式。

Spring Boot Maven插件附带了一个build-info目标。只要在主项目中触发了这个目标,Spring就有一个BuildProperties类,您可以通过网络获取信息。

代码语言:javascript
复制
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>build-info</id>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

您可以访问starter中的属性,如下所示:

代码语言:javascript
复制
@Autowired
BuildProperties buildProperties;

...
buildProperties.getArtifact();
buildProperties.getVersion();

你甚至可以从插件中指定额外的属性。有关更多详细信息,请参阅插件文档:https://docs.spring.io/spring-boot/docs/current/maven-plugin/build-info-mojo.html

不幸的是,我从来没有完全理解为什么我不能访问正确的清单,但这应该会对其他试图解决这个问题的人有所帮助。

票数 32
EN

Stack Overflow用户

发布于 2018-06-25 14:29:16

另一个答案是完全正确的。如果你使用的是Gradle而不是Maven,那么对于其他发现这个问题的人来说:

生成构建信息就像将以下代码添加到build.gradle文件一样简单:

代码语言:javascript
复制
plugins {
    id 'org.springframework.boot' version '<your-boot-version>.RELEASE'
}

// ...    

springBoot {
    buildInfo()
}

如果您想要传递自定义属性:

代码语言:javascript
复制
springBoot {
    buildInfo {
        properties {
            additional = [
                'property.name': 'property value',
                'other.property': 'different.value'
            ]
        }
    }
}

那么在Java代码中的用法与使用BuildProperties的用法相同。你可以在this guide上找到更多关于这个插件的信息。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44786871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档