简体   繁体   中英

How to fetch current subversion revision number and URL with maven

I make a checkout of some branch or tag from subversion repository and then build the project with 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.

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.

You could use the maven-antrun-plugin to execute 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.

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

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

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.

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:

[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.

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:

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

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 .

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.

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.

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.

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

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.

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

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

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

Have a look at http://www.jdotsoft.com/SvnMavenPlugin.php
I created it for my projects and published on Maven Central.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM