提问人:dschulten 提问时间:11/17/2023 更新时间:11/17/2023 访问量:11
Maven 插件生成的资源文件无法从 OSGi 包中读取
Resource files generated by Maven plugin not readable from OSGi bundle
问:
在我的 maven OSGi 项目中,我使用 来创建我的 bundle jar。maven-bundle-plugin
默认情况下,生成到 target/git.properties,他们建议生成到 /target/classes/git.properties,以确保将文件添加到 jar 内的 /classes 目录中:git-commit-id-maven-plugin
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>
${project.build.outputDirectory}/classes/git.properties
</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
</configuration>
</plugin>
这可行,文件存在于生成的 jar 中,但我无法从我的 OSGi 包中读取该文件。
答:
类装入器在运行时找不到资源文件(尽管它包含在捆绑包中)的原因是该资源文件未在 META-INF/MANIFEST 中声明。MF的。
通常,会自动将 src/main/resources 中的所有内容添加到清单中。这就是为什么插件忽略 target/classes/git.properties 的原因 - 忽略该位置。maven-bundle-plugin
maven-bundle-plugin
您可以使用 配置中的指令将其他资源添加到清单中。但请注意:<Include-Resource/>
maven-bundle-plugin
默认情况下,bundle 插件会转换项目的 Maven 资源 目录添加到单个指令中。如果你 指定您自己的指令,这将替换 生成了一个。将生成的 Maven 资源列表包含在 您自己的指令只需将 {maven-resources} 添加到 列表,它将自动展开。
就我而言,我希望我的资源被自动检测,所以我决定将其输出生成到 target/generated-resources/git.properties:maven-bundle-plugin
git-commit-id-plugin
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>
${project.build.directory}/generated-resources/git.properties
</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
</configuration>
</plugin>
接下来,我使用 将 target/generated-resources 文件夹添加到 Maven 反应器中:build-helper-maven-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
在任何情况下,您需要实现的是资源存在于 META_INF/MANIFEST 中。MF,如下所示:
Include-Resource: ...all the rest of your resources...,git.propertie
s=target/generated-resources/git.properties
只有这样,如下所示的代码才能起作用:
URL gitPropertiesFile = this.getClass().getClassLoader().getResource("/git.properties");
String gitInfo;
if (gitPropertiesFile != null) {
gitInfo = IOUtils.toString(gitPropertiesFile, StandardCharsets.ISO_8859_1);
} else {
gitInfo = "No version information found";
}
顺便说一句,如果您想使用优秀的 : 上面的示例适用于该插件的 V.4.0.0,它适用于 Java 8。有更新的版本,但这些版本具有不同的 Maven 坐标。有关详细信息,请参阅插件的文档。git-commit-id-maven-plugin
评论