views:

3448

answers:

5

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.

+1  A: 

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.

dfa
+4  A: 

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.

Dominic Mitchell
I ended up using this approach. However I am calling svn binary directly, this way it should work with both on linux and windows.
Juha Syrjälä
As for *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">` does the trick for me. But: your answer fits the question, which asks for storing it in a file. Alternatively, one could use `${my.revision}` in some `src/main/resources/my.properties` file though, and use `<resources>...<filter>true</filter></resources>` to replace those placeholders while building.
Arjan
+3  A: 

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.

Thomas Marti
This requires that I put the svn URL that I am checkouting to pom.xml. That does not work for me. I am not making checkout with maven, and sometime I checkout trunk, sometimes branchX, sometimes branchY. I'd like to fetch version data from checkouted svn tree.
Juha Syrjälä
If you can live with Dominics solution that's perfectly fine, but there's a good reason Maven folks decided to put the <scm>-tag in the POM. You might get the sources another way than as an SVN checkout (maybe you use 'svn export' or you use a zipped source-file or something else). BTW, checking out different branches is no problem, because if you use 'mvn release:branch' to create them, the POMs will have the correct value in the <scm>-tag.
Thomas Marti
Maybe I'll have to reconsider this, if we ever end up using maven to create branches. Currently we are doing it directly via svn. There also seemed to be some issues with build-number-plugin and parent/child pom.xmls.
Juha Syrjälä
A: 

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.

Rich Seller
+1  A: 

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.

Jim C.