Maven:将单个工件提交到svn存储库

时间:2009-07-15 09:31:05

标签: svn maven-2 maven-scm

如何在部署生命周期阶段将单个工件(如文件或目录)提交到svn存储库中的任意位置? 对于存储库,我指的是一个普通的Subversion存储库,而不是Subversion存储库中保存的Maven存储库!

我已经评估了wagon-svn,但它似乎只能用于将整个模块构建提交到Subversion存储库中保存的Maven存储库。

这是我的用例:我在构建期间生成一个包含所有依赖项的JAR文件。这将在Python脚本中使用,因此在Maven世界之外。我想提供当前版本的JAR,它始终位于存放Python框架的存储库中的相同位置。

1 个答案:

答案 0 :(得分:3)

在你这样做之前,我会仔细考虑你这样做的原因。 派生的工件不应该放入SCM,因为它们可以很容易地重建,而是你可以考虑将工件附加到你的构建中,以便它与它一起部署。

这可以使用build-helper-maven-plugin完成。下面的示例配置将src / assembly / archive.xml附加为分类器“archive”的附加工件。

  <plugin>
    <inherited>true</inherited>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>attach-artifacts</id>
        <phase>deploy</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
            <artifacts>
              <artifact>
                <file>src/assembly/archive.xml</file>
                <type>xml</type>
                <classifier>archive</classifier>
              </artifact>
            </artifacts>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

可以通过在依赖关系声明中指定分类器和类型来直接引用此工件。例如:

<dependency>
  <groupId>my.group.id</groupId>
  <artifactId>artifact-id</artifactId>
  <version>1.0.0</version>
  <type>xml</type>
  <classifier>archive</classifier>
</dependency>

如果你这样做,Maven SCM API为常见的SCM提供者和操作提供了一个抽象层。以下代码can be added to a mojo。它期望提供两个参数,即提交给Subversion的文件和scm url。绑定到项目时,它会将文件提交到Subversion存储库。

package name.seller.rich;

import java.io.File;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.command.add.AddScmResult;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;

/**
 * @goal checkin-file
 */
public class SVNCheckinMojo extends AbstractMojo {

    /**
     * @component
     * @required
     */
    private ScmManager manager;

    /**
     * @component
     * @required
     */
    private ScmProvider provider;

    /**
     * @parameter
     * @required
     */
    private String connectionUrl;

    /**
     * @parameter
     * @required
     */
    private File svnFile;

    /**
     * Obtain the SVN repository.
     */
    public ScmRepository getRepository() throws MojoExecutionException {
        try {
            ScmRepository repository = manager.makeScmRepository(connectionUrl);

            if (!(repository.getProviderRepository() instanceof SvnScmProviderRepository)) {
                throw new MojoExecutionException(
                        "the scm provider is not an SVN provider");
            }

            return repository;
        } catch (Exception e) {
            throw new MojoExecutionException(
                    "Unable to obtain SCM repositorys", e);
        }
    }

    public void execute() throws MojoExecutionException, MojoFailureException {
        ScmRepository repository = getRepository();

        File dir = svnFile.getParentFile();
        File file = new File(svnFile.getName());
        ScmFileSet fileSet = new ScmFileSet(dir, file);
        try {

            AddScmResult result = provider.add(repository, fileSet);

            if (!result.isSuccess()) {
                throw new MojoExecutionException("unable to add file \""
                        + svnFile + "\" to SCM URL \"" + connectionUrl + "\"");
            }
        } catch (ScmException e) {
            throw new MojoExecutionException(
                    "failed to commit file to repository", e);
        }
    }
}

这是插件的示例pom,请注意 maven-plugin 包装:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-svn-hack-plugin</artifactId>
  <packaging>maven-plugin</packaging>
  <version>0.0.1</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-project</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-api</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-provider-svnexe</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.scm</groupId>
      <artifactId>maven-scm-manager-plexus</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>1.5.1</version>
    </dependency>
  </dependencies>
</project>

这是一个示例配置,将在打包阶段检查文件。

<build>
  <plugins>
    <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-svn-hack-plugin</artifactId>
  <version>0.0.1</version>
  <configuration>
    <connectionUrl>scm:svn:http://svn.apache.org/svn/root/module</connectionUrl>
    <svnFile>${project.build.directory}/${artifactId}-${version}-test.txt</svnFile>
  </configuration>
  <executions>
    <execution>
      <id>checkin-file</id>
      <phase>package</phase>
      <goals>
        <goal>checkin-file</goal>
      </goals>
    </execution>
  </executions>
    </plugin>
  </plugins>
</build>

或者在紧要关头,您可以使用antrun plugin来调用ant subversion library