有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

放入POM内存的java git提交id不会进入清单

我试图强制我的Maven build在生成的jar文件的清单中生成这样一行:

SCM-Revision: fdf7abe874a0a54f580aec96da366c168446378c

这样,该值就是git提交id

所以,我找到了this plugin,并按照说明进行设置。详细输出和ant运行输出看起来不错,但生成的清单文件只有原始属性引用,而不是替换的字符串

这是我在多项目构建的父pom中的内容:

        <plugin>
            <groupId>pl.project13.maven</groupId>
            <artifactId>git-commit-id-plugin</artifactId>
            <version>2.2.3</version>
            <executions>
                <execution>
                    <id>get-the-git-infos</id>
                    <goals>
                        <goal>revision</goal>
                    </goals>
                    <phase>validate</phase>
                </execution>
            </executions>
            <configuration>
                <verbose>true</verbose>
                <generateGitPropertiesFile>true</generateGitPropertiesFile>
                <failOnNoGitDirectory>true</failOnNoGitDirectory>
                <injectAllReactorProjects>true</injectAllReactorProjects>
                <dotGitDirectory>${project.basedir}/../../.git</dotGitDirectory>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <SCM-Revision>${git.commit.id}, ${gitCommit}</SCM-Revision>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <configuration>
                        <target>
                            <echo>Git-Infos: ${git.commit.id}, ${gitCommit}</echo>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

因为我正在做其他实验来实现这一点,所以我还在前面的“属性”部分中定义了以下内容:

    <gitCommit>${git.commit.id}</gitCommit>

当我运行构建时,我在输出中看到了这一点,以及插件的详细输出:

[INFO] --- maven-antrun-plugin:1.8:run (default) @ usl-shared ---
[INFO] Executing tasks

main:
     [echo] Git-Infos: fdf7abe874a0a54f580aec96da366c168446378c, fdf7abe874a0a54f580aec96da366c168446378c
[INFO] Executed tasks

然而,这是我在jar的清单文件中得到的:

SCM-Revision: ${git.commit.id}, ${git.commit.id}

不知何故,jar插件中的属性引用并没有替代属性值

更新

我还注意到git。属性文件是在目标/类中创建的,但它不在jar文件中。这意味着该文件是在创建jar文件之后创建的,这意味着这些属性是在创建jar文件之后设置的,这使得这种行为可以理解。这听起来像是“阶段”的问题。我使用的是推荐的配置,但似乎这就是问题所在

更新

我不知道这是否相关,但请注意,我们工件项目的包装类型是“bundle”,而不是“jar”。我们还使用maven bundle插件。作为一种测试,我尝试将包装改为“jar”,这使得属性替换工作正常。然而,它放弃了我们所需的所有osgi属性


共 (1) 个答案

  1. # 1 楼答案

    正如您在更新中提到的,您的问题是关于maven-bundle-plugin

    因此,如果除了commit-id之外,您不使用maven-jar-pluginmaven-antrun-plugin,那么就可以去掉它们

    假设你的maven-bundle-plugin看起来像这样:

                 <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <extensions>true</extensions>
                    <configuration>
                        <instructions>
                            <Bundle-SymbolicName>${pom.groupId}.${pom.artifactId}</Bundle-SymbolicName>
                            <Bundle-Name>Service listener example</Bundle-Name>
                            <Bundle-Description>A bundle that displays messages at startup and when service events occur</Bundle-Description>
                            <Bundle-Vendor>Apache Felix</Bundle-Vendor>
                            <Bundle-Version>1.0.0</Bundle-Version>
                            <Bundle-Activator>tutorial.example1.Activator</Bundle-Activator>
                            <Import-Package>org.osgi.framework</Import-Package>
                            <SCM-Revision>${git.commit.id}, ${gitCommit}</SCM-Revision>
                        </instructions>
                    </configuration>
                </plugin>
    

    您只需在maven-bundle-plugin中添加属性(即SCM-Revision),在<configuration>部分将属性作为Instruction添加即可