Search code examples
mavenmaven-3pom.xml

How to get the scm information without directly executing an svn call from maven?


I'd like to convert the following into a Jenkins compatible plugin which doesn't rely on executing an external call to the executable.
Are there alternative plugins that can be used to get this information for entry into the META-INF folder?

Note: SVN is not on the path in the Jenkins environment.

Maven 3.5.3
Java 1.8

Plugin:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>${maven-antrun-plugin.ver}</version>
            <executions>
                <execution>
                    <id>get-scm-info</id>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <tasks>
                            <mkdir dir="target/buildinfo" />
                            <exec executable="svn" output="target/buildinfo/scm.properties">
                                <arg value="info" />
                                <arg value="${basedir}" />
                                <arg value="--non-interactive" />
                            </exec>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Solution

  • The goal create-metadata of the build number plugin will create the required file containing the scm version information. The configuration below will create a file called scm.properties in the folder target/buildinfo.

    As there is no svn installation available on the build machine, you could use svnkit instead of a normal svn installation. To use svnkit there are two additional configuations necessary:

    • explicity tell the build-number plugin to use svnkit: <providerImplementations><svn>javasvn</svn></providerImplementations>
    • add two additional dependencies (maven-scm-provider-svnjava and svnkit) to the plugin.
    <build>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>buildnumber-maven-plugin</artifactId>
          <version>1.4</version>
          <executions>
            <execution>
              <phase>generate-resources</phase>
              <goals>
                <goal>create-metadata</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <doCheck>true</doCheck>
            <doUpdate>true</doUpdate>
            <outputDirectory>target/buildinfo</outputDirectory>
            <outputName>scm.properties</outputName>
            <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.2</version>
          </dependency>
          <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.9.1</version> 
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
    

    To get everything working, the pom has to contain a scm section with the correct svn url:

    <scm>
      <developerConnection>scm:svn:https://...</developerConnection>
    </scm>