我从subversion仓库中检出了一些分支或标签,然后使用maven构建项目。
现在,我想将当前版本号和URL存储到某个文件中。我该怎么做?也就是说,我想获取无论我检出的是哪个分支/标签的版本号和URL。
我知道buildnumber-maven-plugin,但我认为它并不能做到这一点。它只会获取在pom.xml中指定的分支的版本号。
我从subversion仓库中检出了一些分支或标签,然后使用maven构建项目。
现在,我想将当前版本号和URL存储到某个文件中。我该怎么做?也就是说,我想获取无论我检出的是哪个分支/标签的版本号和URL。
我知道buildnumber-maven-plugin,但我认为它并不能做到这一点。它只会获取在pom.xml中指定的分支的版本号。
<project>
[…]
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<configuration>
<tasks>
<exec executable="sh">
<arg value="-c"/>
<arg value="svnversion >version.txt" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[…]
</project>
注意:我使用了sh -c
来运行svnversion,以便将输出重定向到文件中。我不认为您可以将输出获取到属性中以供maven构建使用。
您可以使用相同的方法来运行svn info --url
。
如前所述,Maven Build Number Plugin可以用于查找版本号。
至于URL:Maven的惯例是使用<scm>标签将其放在POM中。如果您正确配置了一次,并使用适当的Maven插件(maven-scm-plugin,maven-release-plugin)进行分支、标记、发布等操作,则可以确保<scm>标签始终包含正确的URL。
在我看来,该API似乎仅支持执行“svn”命令及其各种参数和开关。问题在于,在Subversion中,使用不同的可执行命令来检索正确的详细版本号,即“svnversion”。使用此命令,我可以确定是否具有混合版本、修改版本等。例如:
[jim@localhost sb_rc1 993]$ svn info | grep Revision
Revision: 51159
[jim@localhost sb_rc1 994]$ svnversion
51159M
[jim@localhost sb_rc1 994]$
用户user2990242提出的解决方案非常适用于独立使用(感谢详细的说明)。之后,您只需要使用maven-jar插件(或maven-war)在manifest.mf中添加信息。以下是完整示例:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<timestampFormat>{0,date,dd-MM-yyyy HH:mm:ss}</timestampFormat>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.maven-scm-provider-svnjava</groupId>
<artifactId>maven-scm-provider-svnjava</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Vendor>ENTERPRISE</Implementation-Vendor>
<Implementation-Version>${project.version}</Implementation-Version>
<Built-By>${user.name}</Built-By>
<Built-OS>${os.name}</Built-OS>
<Build-Date>${timestamp}</Build-Date>
<SCM-Revision>${buildNumber}</SCM-Revision>
</manifestEntries>
</archive>
</configuration>
</plugin>
Buildernumber-maven-plugin 依赖于你安装的 svn 客户端,当插件配置中未指定 "providerImplementations" 时。如果系统路径中未定义 svn 执行路径,或者你的 svn 客户端版本不匹配,则插件无法正确调用 svn 命令行。
因此,添加一个 javasvn 客户端及其依赖是个好主意。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.maven-scm-provider-svnjava</groupId>
<artifactId>maven-scm-provider-svnjava</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.7.8</version>
</dependency>
</dependencies>
</plugin>
您已经说过这不符合您的需求,但对于其他遇到类似问题的人来说,值得注意的是,您可以使用Maven SCM API来调用针对POM文件中scm
部分配置的存储库的任意SCM命令。例如,在this answer中,我展示了如何在Maven mojo中提交单个文件。
您可以稍微修改该示例,将SCMProvider
向下转换为SvnExeScmProvider
并调用其info()
方法。这将返回一个SvnInfoScmResult
,它包装了一个SvnInfoItem
。该项封装了通过标准Subversion API运行svn info
命令的结果。
这是一个老问题,但是现在buildnumber-maven-plugin
插件公开了一个带有SCM URL的${scmBranch}
值。我已经简要尝试过它,似乎可以工作。要在资源过滤中使用它,您必须首先将其放入属性中,就像Maven公开的timestamp
值一样。
有关更多信息,请参见http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html。
<exec outputproperty="my.revision" executable="svnversion">
对我来说很管用。但是:你的回答适用于问题,该问题要求将其存储在文件中。或者,可以在某个src/main/resources/my.properties
文件中使用${my.revision}
,并使用<resources>...<filter>true</filter></resources>
在构建时替换这些占位符。 - Arjan