我目前正在开发一个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
但是,当我试图从我的通用服务包中找到示例项目的清单文件时,我找不到该文件。
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条
发布于 2017-06-30 02:52:04
经过很多努力,我找到了一个令人惊讶的简单的答案。这就是spring-boot-actuator获取信息的方式。
Spring Boot Maven插件附带了一个build-info目标。只要在主项目中触发了这个目标,Spring就有一个BuildProperties类,您可以通过网络获取信息。
<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中的属性,如下所示:
@Autowired
BuildProperties buildProperties;
...
buildProperties.getArtifact();
buildProperties.getVersion();你甚至可以从插件中指定额外的属性。有关更多详细信息,请参阅插件文档:https://docs.spring.io/spring-boot/docs/current/maven-plugin/build-info-mojo.html
不幸的是,我从来没有完全理解为什么我不能访问正确的清单,但这应该会对其他试图解决这个问题的人有所帮助。
发布于 2018-06-25 14:29:16
另一个答案是完全正确的。如果你使用的是Gradle而不是Maven,那么对于其他发现这个问题的人来说:
生成构建信息就像将以下代码添加到build.gradle文件一样简单:
plugins {
id 'org.springframework.boot' version '<your-boot-version>.RELEASE'
}
// ...
springBoot {
buildInfo()
}如果您想要传递自定义属性:
springBoot {
buildInfo {
properties {
additional = [
'property.name': 'property value',
'other.property': 'different.value'
]
}
}
}那么在Java代码中的用法与使用BuildProperties的用法相同。你可以在this guide上找到更多关于这个插件的信息。
https://stackoverflow.com/questions/44786871
复制相似问题