Wednesday, June 10, 2015

maven-scm-plugin pom.xml sample for svn checkout/checkin - and why the error "svn: [dir] is not a working copy"

Below is a snippet of my pom.xml that performs the following 3 steps when invoked by "mvn generate-sources" -
1. maven-scm-plugin to do a svn checkout into the target/checkout directory
2. exec-maven-plugin to use curl to download a jar file into the target/checkout directory
3. maven-scm-plugin to do a svn add & checkin from the target/checkout directory
 ------------------------------------------------------
  <scm>
    <connection>scm:svn:http://your_svn_url</connection>
    <developerConnection>scm:svn:http://your_svn_url</developerConnection>
    <url>scm:svn:http://your_svn_url</url>
  </scm>

  <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
        <version>1.9</version>
        <configuration>
          <username>...</username>
          <password>...</password>
          <basedir>target</basedir>
          <workingDirectory>target/checkout</workingDirectory>
        </configuration>
        <executions>
         <execution>
            <id>perform-checkout</id>
                <phase>initialize</phase>
            <goals>
                <goal>checkout</goal>
            </goals>
           <configuration>
          <workingDirectory>target/checkout</workingDirectory>
          <checkoutDirectory>target/checkout</checkoutDirectory>
           </configuration>
         </execution>
          <execution>
            <id>perform-checkin</id>
                <phase>generate-sources</phase>
            <goals>
                <goal>add</goal>
                <goal>checkin</goal>
            </goals>
            <configuration>
                <workingDirectory>target/checkout</workingDirectory>
                <basedir>target</basedir>
                <includes>*</includes>
                <message>test</message>
            </configuration>
         </execution>
        </executions>
      </plugin>

    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
           <id>id1</id>
           <phase>initialize</phase>
           <goals>
                <goal>exec</goal>
            </goals>
           <configuration>
                <executable>curl</executable>
                <workingDirectory>target/checkout</workingDirectory>
                <arguments>
                        <argument>-O</argument>
                        <argument>https://repo.abc.com/abc.jar</argument>
                </arguments>
           </configuration>
        </execution>
    </executions>
</plugin>
</plugins>
--------------------------------------------------
A few things to note here -
1. As you can see, the "initialize" phase will execute the scm:checkout and exec:exec, then the "generate-sources" phase will  execute the scm:add and scm:checkin. You can of course change the phases to whatever fit your need, but phases determines the sequence that your plugin executions will be executed.

2. You should always do a svn checkout first, so the directory you plan to use to checkin contains the svn working copy (in this case, your svn working directory is [your_current_dir]/target/checkout). Otherwise, the scm:checkin would fail with error "svn: [your_current_dir/target/checkout] is not a working copy". You can of course manually checkout the svn working directory first, then use maven to just do the checkin. But with the above executions, you can do both checkout/checkin with maven.

3. You should also check that the .svn directory inside your working directory is properly populated. One thing that could cause the .svn to be empty is that you've set the "includes" configuration to wrong value for scm:checkout - for examples, if you have an "includes=*" in your checkout execution configuration, your ".svn" directory will be empty, and your subsequent scm:checkin would fail.


No comments:

Post a Comment