如何使用maven获取当前的subversion版本号和URL

时间:2021-12-07 13:11:32

I make a checkout of some branch or tag from subversion repository and then build the project with maven.

我从subversion存储库中检查了一些分支或标记,然后使用maven构建项目。

Now, I'd like to get store current revision number and URL to some file. How can I do that? That is, I'd like to get revision number and URL of whatever branch/tag I have made checkout of.

现在,我想将商店当前的版本号和URL添加到某个文件中。我怎样才能做到这一点?也就是说,我想得到我已经签出的任何分支/标签的修订号和URL。

I know about buildnumber-maven-plugin but I think it doesn't do this. It fetches revision number of branch that is specified in pom.xml.

我知道buildnumber-maven-plugin,但我认为它不会这样做。它获取pom.xml中指定的分支的修订号。

9 个解决方案

#1


Solution exposed by user2990242 is great for standalone (thank you for good explanation). After that, you just have to add information in the manifest.mf with maven-jar plugin (or maven-war). here a full example:

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>

#2


You could use the maven-antrun-plugin to execute svnversion.

您可以使用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>

NB: I've used sh -c to run svnversion, so I can redirect the output to a file. I don't think that you can get the output into a property for use by the maven build.

注意:我使用sh -c来运行svnversion,所以我可以将输出重定向到一个文件。我不认为您可以将输出转换为maven构建使用的属性。

You can use the same approach to run svn info --url.

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

#3


As said already the Maven Build Number Plugin can be used to find the revision.

如上所述,Maven Build Number Plugin可用于查找修订版。

As for the URL: Maven Practice is to put it in the POM using the <scm>-tag. If you configure this right once and then use the appriopriate Maven plugins (maven-scm-plugin, maven-release-plugin) for branching, tagging, releasing, etc. you can be sure the <scm>-tag always contains the right url.

至于URL:Maven Practice是使用 -tag将它放入POM中。如果您配置一次,然后使用适当的Maven插件(maven-scm-plugin,maven-release-plugin)进行分支,标记,发布等,您可以确定 -tag始终包含正确的URL 。

#4


buildnumber-maven-plugin injects only current revision in your build so you're right. In order to get the URL I think that you should write a maven plugin that uses SVNKit.

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

#5


It seems to me that the API only supports execution of the 'svn' command and it's various parameters and switches. The problem is that in subversion, a different executable command is used to retrieve a proper detailed version number, that being 'svnversion'. Using this command, I can tell if I have a mixed version, a modified version, etc. For example:

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

Guess what? 'svn info' is lying to me here. My local copy is modified from the original 51159 which is why 'svnversion' reports the version number with an M attached to it. What if I'm experimenting with a branch that contains a mixed version? 'svnversion' can handle this. 'svn info' cannot. Worse, as shown above, it will provide misleading and potentially disastrous information if, for example, I'm basing a release off of the bad info.

你猜怎么着? 'svn info'在这里骗我。我的本地副本是从原来的51159修改的,这就是为什么'svnversion'报告带有M的版本号。如果我正在尝试包含混合版本的分支怎么办? 'svnversion'可以解决这个问题。 'svn info'不能。更糟糕的是,如上所示,如果我基于不良信息的发布,它将提供误导性和潜在的灾难性信息。

#6


You've already said this doesn't meet your needs, but for others who have a similar problem it is worth noting that you can use the Maven SCM API to invoke arbitrary SCM commands against the repository configured in the scm section of your POM. For example in this answer I showed how to commit a single file in a Maven mojo.

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

You could modify that example slightly to instead downcast the SCMProvider to SvnExeScmProvider and invoke its info() method. This returns an SvnInfoScmResult which wraps a SvnInfoItem. The item encapsulates the results of running an svn info command via the standard Subversion API.

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

#7


For completeness, antrun supports exporting ant properties to mvn since version 1.7. See Add a new parameter to export Ant properties to Maven properties

为了完整性,antrun支持从1.7版开始将ant属性导出到mvn。请参阅添加新参数以将Ant属性导出到Maven属性

#8


This is an old question, but the buildnumber-maven-plugin now exposes an ${scmBranch} value with the SCM URL in it. I've briefly tried it and it seems to work. To use it in resource filtering, you'll have to put it into a property first, as with the timestamp value exposed by Maven.

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

See http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html for more information.

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

#9


Buildernumber-maven-plugin dependents on the svn client you installed when "providerImplementations" not spepcified in the plugin configuration. When the svn execution path is not defined in the system path, or your svn client version is not matched, the plugin cannot call svn command line correctly.

当“providerImplementations”没有在插件配置中强化时,您安装的svn客户端上的Buildernumber-maven-plugin依赖。如果未在系统路径中定义svn执行路径,或者您的svn客户端版本不匹配,则插件无法正确调用svn命令行。

So, adding a javasvn client and its dependency is a good idea.

因此,添加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>

#1


Solution exposed by user2990242 is great for standalone (thank you for good explanation). After that, you just have to add information in the manifest.mf with maven-jar plugin (or maven-war). here a full example:

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>

#2


You could use the maven-antrun-plugin to execute svnversion.

您可以使用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>

NB: I've used sh -c to run svnversion, so I can redirect the output to a file. I don't think that you can get the output into a property for use by the maven build.

注意:我使用sh -c来运行svnversion,所以我可以将输出重定向到一个文件。我不认为您可以将输出转换为maven构建使用的属性。

You can use the same approach to run svn info --url.

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

#3


As said already the Maven Build Number Plugin can be used to find the revision.

如上所述,Maven Build Number Plugin可用于查找修订版。

As for the URL: Maven Practice is to put it in the POM using the <scm>-tag. If you configure this right once and then use the appriopriate Maven plugins (maven-scm-plugin, maven-release-plugin) for branching, tagging, releasing, etc. you can be sure the <scm>-tag always contains the right url.

至于URL:Maven Practice是使用 -tag将它放入POM中。如果您配置一次,然后使用适当的Maven插件(maven-scm-plugin,maven-release-plugin)进行分支,标记,发布等,您可以确定 -tag始终包含正确的URL 。

#4


buildnumber-maven-plugin injects only current revision in your build so you're right. In order to get the URL I think that you should write a maven plugin that uses SVNKit.

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

#5


It seems to me that the API only supports execution of the 'svn' command and it's various parameters and switches. The problem is that in subversion, a different executable command is used to retrieve a proper detailed version number, that being 'svnversion'. Using this command, I can tell if I have a mixed version, a modified version, etc. For example:

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

Guess what? 'svn info' is lying to me here. My local copy is modified from the original 51159 which is why 'svnversion' reports the version number with an M attached to it. What if I'm experimenting with a branch that contains a mixed version? 'svnversion' can handle this. 'svn info' cannot. Worse, as shown above, it will provide misleading and potentially disastrous information if, for example, I'm basing a release off of the bad info.

你猜怎么着? 'svn info'在这里骗我。我的本地副本是从原来的51159修改的,这就是为什么'svnversion'报告带有M的版本号。如果我正在尝试包含混合版本的分支怎么办? 'svnversion'可以解决这个问题。 'svn info'不能。更糟糕的是,如上所示,如果我基于不良信息的发布,它将提供误导性和潜在的灾难性信息。

#6


You've already said this doesn't meet your needs, but for others who have a similar problem it is worth noting that you can use the Maven SCM API to invoke arbitrary SCM commands against the repository configured in the scm section of your POM. For example in this answer I showed how to commit a single file in a Maven mojo.

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

You could modify that example slightly to instead downcast the SCMProvider to SvnExeScmProvider and invoke its info() method. This returns an SvnInfoScmResult which wraps a SvnInfoItem. The item encapsulates the results of running an svn info command via the standard Subversion API.

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

#7


For completeness, antrun supports exporting ant properties to mvn since version 1.7. See Add a new parameter to export Ant properties to Maven properties

为了完整性,antrun支持从1.7版开始将ant属性导出到mvn。请参阅添加新参数以将Ant属性导出到Maven属性

#8


This is an old question, but the buildnumber-maven-plugin now exposes an ${scmBranch} value with the SCM URL in it. I've briefly tried it and it seems to work. To use it in resource filtering, you'll have to put it into a property first, as with the timestamp value exposed by Maven.

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

See http://mojo.codehaus.org/buildnumber-maven-plugin/usage.html for more information.

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

#9


Buildernumber-maven-plugin dependents on the svn client you installed when "providerImplementations" not spepcified in the plugin configuration. When the svn execution path is not defined in the system path, or your svn client version is not matched, the plugin cannot call svn command line correctly.

当“providerImplementations”没有在插件配置中强化时,您安装的svn客户端上的Buildernumber-maven-plugin依赖。如果未在系统路径中定义svn执行路径,或者您的svn客户端版本不匹配,则插件无法正确调用svn命令行。

So, adding a javasvn client and its dependency is a good idea.

因此,添加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>