Maven插件生成的资源文件不能从OSGi包中读取

fzsnzjdm  于 5个月前  发布在  Maven
关注(0)|答案(1)|浏览(65)

在我的maven OSGi项目中,我使用maven-bundle-plugin来创建我的bundle jar。
git-commit-id-maven-plugin默认生成到 *target/git.properties *,他们建议生成到 /target/classes/git.properties,以确保文件被添加到jar内的 /classes 目录:

<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包中读取文件。

ldfqzlk8

ldfqzlk81#

虽然资源文件包含在bundle中,但类加载器在运行时找不到它的原因是资源文件没有在 META-INF/MANIFEST.MF 中声明。
通常情况下,maven-bundle-plugin会自动将 src/main/resources 中的所有内容添加到清单中。这就是为什么插件会忽略 *target/classes/git.properties * -maven-bundle-plugin会忽略该位置。
您可以使用maven-bundle-plugin配置中的<Include-Resource/>指令将其他资源添加到清单中。但请注意:
默认情况下,bundle插件将项目的Maven资源目录转换为单个指令。如果您指定了自己的指令,这将替换生成的指令。要在自己的指令中包含生成的Maven资源列表,只需将{maven-resources}添加到列表中,它将自动展开。
-- Felix Bundle插件文档
在我的例子中,我希望我的资源能被maven-bundle-plugin自动检测到,所以我决定让git-commit-id-plugin将其输出生成到 *target/generated-resources/git.properties *:

<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>

字符串
接下来,我使用build-helper-maven-plugintarget/generated-resources 文件夹添加到Maven reactor中:

<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";
        }


作为旁注,如果你想使用优秀的git-commit-id-maven-plugin:上面的例子是针对该插件的V.4.0.0,它可以工作到Java 8。有更新的版本,但它们具有不同的Maven坐标。有关详细信息,请参阅插件的documentation

相关问题