包括依赖项的git-revision

时间:2018-10-09 06:22:31

标签: java git maven svn buildnumber-maven-plugin

简短版本:我想将当前模块及其依赖项的最新修订号(与单个模块相关的)存储在jar的清单中。

我有一个多模块Maven项目。这些模块内置在有阴影的jar中(包括所有依赖项),因此它们可以独立部署。其中一些模块依赖于其他模块(对于其他模块,最先构建的几个模块是“常见”的)。 在构建这些模块时,我要包括模块的最新提交修订以及相关性最新提交修订。我所有的模块都保存在同一个git-repo中(没有“ sub-gits”)。

示例:

<module>common</module> 
<module>data</module>
<module>application1</module>

前两个模块是常见的模块,它们是application1中的依赖项。

让我们说最近一次提交是864ccc,我在其中同时更改了Data和application1Common但是最后一次在889a01版中进行了更改。 构建这些模块时,我希望清单中显示以下输出:

Revision-App: 864ccc
Revision-Common: 889a01
Revision-Data: 864ccc

我以前曾将MojoHaus的 buildnumber-maven-plugin 与SVN一起使用。 通过此设置,我可以通过pom.xml中的以下代码来完成此操作:

    <build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        <manifestEntries>
                            <URL>${project.url}</URL>
                            <Application-Build-Time>${timeStamp}</Application-Build-Time>
                            <Revision-Appl>${app.rev}</Revision-Application>
                            <Revision-Data>${data.rev}</Revision-Data>
                            <Revision-Common>${common.rev}</Revision-Common>
                        </manifestEntries>
                    </archive>
                    <outputDirectory>../target</outputDirectory>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>${buildnumber-maven-plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.tmatesoft.svnkit</groupId>
                        <artifactId>svnkit</artifactId>
                        <version>1.8.5</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <providerImplementations>
                        <svn>javasvn</svn>
                    </providerImplementations>
                </configuration>
                <executions>
                    <execution>
                        <id>timeStamp</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                        <configuration>
                            <doCheck>false</doCheck>
                            <doUpdate>false</doUpdate>
                            <buildNumberPropertyName>timeStamp</buildNumberPropertyName>
                            <format>{0,date,yyyy-MM-dd HH:mm:ss}</format>
                            <items>
                                <item>timestamp</item>
                            </items>
                            <revisionOnScmFailure>SVN_FAILURE</revisionOnScmFailure>
                        </configuration>
                    </execution>
                    <execution>
                        <id>appRev</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                        <configuration>
                            <doCheck>false</doCheck>
                            <doUpdate>false</doUpdate>
                            <buildNumberPropertyName>app.rev</buildNumberPropertyName>
                            <useLastCommittedRevision>true</useLastCommittedRevision>
                            <scmDirectory>${project.basedir}</scmDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>commonRev</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                        <configuration>
                            <doCheck>false</doCheck>
                            <doUpdate>false</doUpdate>
                            <buildNumberPropertyName>common.rev</buildNumberPropertyName>
                            <useLastCommittedRevision>true</useLastCommittedRevision>
                            <scmDirectory>${project.basedir}/../common</scmDirectory>
                            <getRevisionOnlyOnce>false</getRevisionOnlyOnce>
                            <revisionOnScmFailure>SVN_FAILURE</revisionOnScmFailure>
                        </configuration>
                    </execution>
                    <execution>
                        <id>dataRev</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                        <configuration>
                            <doCheck>false</doCheck>
                            <doUpdate>false</doUpdate>
                            <buildNumberPropertyName>data.rev</buildNumberPropertyName>
                            <useLastCommittedRevision>true</useLastCommittedRevision>
                            <scmDirectory>${project.basedir}/../data</scmDirectory>
                            <getRevisionOnlyOnce>false</getRevisionOnlyOnce>
                            <revisionOnScmFailure>SVN_FAILURE</revisionOnScmFailure>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>com.company.package:*</include>
                        </includes>
                    </artifactSet>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

但是我无法终生想出如何用git做同样的事情。 我只能获得超级pom的HEAD版本(包含模块)。

您能帮我用git获得相同的结果吗?

运行mvn build会给我以下输出:

[INFO] --- buildnumber-maven-plugin:1.4:create (appRev) @ common ---
[INFO] Executing: /bin/sh -c cd '/home/user/java/application1' && 'git' 'rev-parse' '--verify' 'HEAD'
[INFO] Working directory: /home/user/java/application1
[INFO] Storing buildNumber: 864cccf9056d273313eb9fb690b58674f72d4a6b at timestamp: 1539006765309
[INFO] Storing buildScmBranch: mvn-revision
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.4:create (commonRev) @ common ---
[INFO] Executing: /bin/sh -c cd '/home/user/java/common/../common' && 'git' 'rev-parse' '--verify' 'HEAD'
[INFO] Working directory: /home/user/java/common/../common
[INFO] Storing buildNumber: 864cccf9056d273313eb9fb690b58674f72d4a6b at timestamp: 1539006765326
[INFO] Storing buildScmBranch: mvn-revision
[INFO] 
[INFO] --- buildnumber-maven-plugin:1.4:create (dataRev) @ common ---
[INFO] Executing: /bin/sh -c cd '/home/user/java/common/../data' && 'git' 'rev-parse' '--verify' 'HEAD'
[INFO] Working directory: /home/user/java/common/../data
[INFO] Storing buildNumber: 864cccf9056d273313eb9fb690b58674f72d4a6b at timestamp: 1539006765330
[INFO] Storing buildScmBranch: mvn-revision
[INFO] 

我得到运行git rev-parse --verify HEAD的输出。但是我想让第一行的输出在当前工作目录中运行git rev-list --verify HEAD -- .

您有什么想法吗?

我不需要坚持使用这个插件,我只想描述一下结果。

0 个答案:

没有答案