如何使用Maven获取当前Subversion版本号和URL

4

我从subversion仓库中检出了一些分支或标签,然后使用maven构建项目。

现在,我想将当前版本号和URL存储到某个文件中。我该怎么做?也就是说,我想获取无论我检出的是哪个分支/标签的版本号和URL。

我知道buildnumber-maven-plugin,但我认为它并不能做到这一点。它只会获取在pom.xml中指定的分支的版本号。

9个回答

4
你可以使用 maven-antrun-plugin 来执行 svnversion
<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 &gt;version.txt" />
                </exec>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  […]
</project>

注意:我使用了sh -c来运行svnversion,以便将输出重定向到文件中。我不认为您可以将输出获取到属性中以供maven构建使用。

您可以使用相同的方法来运行svn info --url


最终我采用了这种方法。不过我直接调用svn二进制文件,这样就可以同时在Linux和Windows上运行。 - Juha Syrjälä
1
关于I don't think that you can get the output into a property for use by the maven build: <exec outputproperty="my.revision" executable="svnversion"> 对我来说很管用。但是:你的回答适用于问题,该问题要求将其存储在文件中。或者,可以在某个src/main/resources/my.properties文件中使用${my.revision},并使用<resources>...<filter>true</filter></resources>在构建时替换这些占位符。 - Arjan

4

如前所述,Maven Build Number Plugin可以用于查找版本号。

至于URL:Maven的惯例是使用<scm>标签将其放在POM中。如果您正确配置了一次,并使用适当的Maven插件(maven-scm-pluginmaven-release-plugin)进行分支、标记、发布等操作,则可以确保<scm>标签始终包含正确的URL。


这需要我将正在检出的svn URL放入pom.xml中。但这对我不起作用。我不是使用maven进行检出,有时我检出trunk,有时是branchX,有时是branchY。我想从检出的svn树中获取版本数据。 - Juha Syrjälä
如果您可以接受Dominic的解决方案,那就完全没问题,但Maven开发人员决定将<scm>标签放在POM中有很好的原因。您可能以与SVN checkout不同的方式获取源代码(也许您使用'svn export'或使用压缩的源文件或其他方法)。顺便说一下,检查不同的分支没有问题,因为如果您使用'mvn release:branch'创建它们,POM将在<scm>标签中具有正确的值。 - Thomas Marti
如果我们最终使用Maven创建分支,也许我需要重新考虑这个问题。目前我们是直接通过SVN来完成的。同时,build-number-plugin和parent/child pom.xmls似乎存在一些问题。 - Juha Syrjälä

1

在我看来,该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]$

猜猜看?这里的'svn info'在骗我。我的本地副本已经从原始的51159进行了修改,这就是为什么'svnversion'报告带有M附加的版本号。如果我正在尝试包含混合版本的分支会怎样呢?'svnversion'可以处理这个问题,但'svn info'却不能。更糟糕的是,如上所示,如果例如我基于错误信息发布版本,则它将提供误导性和潜在灾难性的信息。

1

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

1

buildnumber-maven-plugin 只在您的构建中注入当前版本,所以您是正确的。为了获取 URL,我认为您应该编写一个使用 SVNKit 的 Maven 插件。


0

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>

0

0

您已经说过这不符合您的需求,但对于其他遇到类似问题的人来说,值得注意的是,您可以使用Maven SCM API来调用针对POM文件中scm部分配置的存储库的任意SCM命令。例如,在this answer中,我展示了如何在Maven mojo中提交单个文件。

您可以稍微修改该示例,将SCMProvider向下转换为SvnExeScmProvider并调用其info()方法。这将返回一个SvnInfoScmResult,它包装了一个SvnInfoItem。该项封装了通过标准Subversion API运行svn info命令的结果。


0

这是一个老问题,但是现在buildnumber-maven-plugin插件公开了一个带有SCM URL的${scmBranch}值。我已经简要尝试过它,似乎可以工作。要在资源过滤中使用它,您必须首先将其放入属性中,就像Maven公开的timestamp值一样。

有关更多信息,请参见http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接