How to get current subversion revision number and url using maven

I am doing a check on some branch or tag from the subversion repository, and then creating a project using maven.

Now I would like to get the current version number and store URL for some file. How can i do this? That is, I would like to get the revision number and URL of any branch / tag that I made.

I know about buildnumber-maven-plugin , but I think it is not. It selects the version number of the branch specified in pom.xml.

+4
source share
9 answers

The solution exposed by user2990242 is great for battery life (thanks for the good explanation). After that, you just need to add the information to manifest.mf using the maven-jar plugin (or maven-war). here is a complete 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> 
+1
source

You can use maven-antrun-plugin to do 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 used sh -c to run svnversion, so I can redirect the output to a file. I do not think that you can get the output to the property for use with the maven assembly.

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

+4
source

As already mentioned, Maven Build Number Plugin can be used to search for a version.

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

+4
source

buildnumber-maven-plugin only introduces the current version of your build, so you're right. To get the url, I think you should write a maven plugin that uses SVNKit .

+1
source

It seems to me that the API only supports the execution of the 'svn' command, as well as various parameters and switches. The problem is that the disruptive program uses another executable command to get the correct version number, which is "svnversion". Using this command, I can tell if I have a mixed version, a modified version, etc. For instance:

 [ 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 was changed from the original 51159, so "svnversion" reports the version number with M. attached. What if I experiment with a branch containing a mixed version? "svnversion" can handle this. "svn info" cannot. Even worse, as shown above, this will lead to misleading and potentially harmful information if, for example, I base exemption from bad information.

+1
source

You already said that this does not meet your needs, but for others who have a similar problem, it is worth noting that you can use the SCM Maven 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 Maven mojo.

You can modify this example a bit to instead disable SCMProvider to SvnExeScmProvider and call its info() method. This returns SvnInfoScmResult , which wraps SvnInfoItem . The element encapsulates the results of running the svn info through the standard Subversion API.

0
source

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.

0
source

This is an old question, but buildnumber-maven-plugin now provides the value of ${scmBranch} with the SCM URL. I briefly tried and it seemed to work. To use it in resource filtering, you first need to put it in a property, as with the timestamp parameter opened by Maven.

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

0
source

The initiators of the Buildernumber-maven-plugin on the svn client that you installed when the "providerImplementations" are not programmed in the plugin configuration. When the path of the svn path is not defined in the system path or your version of the svn client is not mapped, the plugin cannot correctly invoke the svn command line.

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

All Articles