maven中的多个部署

时间:2009-03-11 15:51:46

标签: java maven-2 hudson artifactory

我们有一个内部的神器库。目前所有快照都将在那里部署。我们还希望拥有一个带有Web界面的不同服务器,并希望将创建的工件复制到其中。

对于我们的构建,我们使用Hudson,但是后构建操作“将构件部署到Maven存储库”与scp一起不起作用。所以有一个问题是以其他优雅的方式做到这一点。为什么maven不能拥有多个分发存储库?有什么想法吗?

最好的事情是,如果artifactory在每次新部署后都支持(自动!)增量导出到标准maven存储库。

4 个答案:

答案 0 :(得分:12)

我认为maven不支持为单个配置文件部署到多个存储库,但也许配置文件可能会更改存储库的ID和URL。

  <distributionManagement>
    <repository>
      <id>${repo-id}</id>
      <name>${repo-name}</name>
      <url>${repo-url}</url>
    </repository>
  </distributionManagement>

Maven Deployment

然后使用配置文件选择要部署到哪个仓库:

<profiles>
  <profile>
    <id>repo1</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <repo-id>repo1</repo-id>
      <repo-name>Repo1 Name </repo-name>
      <repo-url>http://url.com/maven2</repo-url>
    </properties>
  </profile>
  <profile>
    <id>repo2</id>
    <properties>
      <repo-id>repo2</repo-id>
      <repo-name>Repo2 Name </repo-name>
      <repo-url>http://url2.com/maven2</repo-url>
    </properties>
  </profile>
</profiles>

Maven profiles

答案 1 :(得分:3)

如果您愿意使用自定义插件,则可以将Maven配置为在标准部署的同时部署到“镜像”位置列表。我建议在配置文件中定义它,以便您可以控制镜像的部署(在每个构建中执行此操作可能不合适)。

要定义新插件,您需要创建一个新的Maven项目并指定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>name.seller.rich</groupId>
  <artifactId>maven-mirror-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.2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>2.4</version>
    </dependency>
  </dependencies>
</project>

在src / main / java中定义一个Mojo。下面的代码声明了一个“镜像”目标,它采用一个mirrorRepository项列表(包含repositoryId和url)来镜像工件部署。该插件使用与maven-deploy-plugin相同的部署方法,并采用大多数相同的参数。

请注意,您仍然需要在settings.xml中为每个具有相应权限的存储库定义服务器以进行部署,否则构建将失败!

package name.seller.rich;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.deployer.ArtifactDeployer;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;

/**
 * @goal mirror
 * @phase deploy
 */
public class MirrorMojo extends AbstractMojo {
    /**
     * @parameter expression=
     *            "${component.org.apache.maven.artifact.deployer.ArtifactDeployer}"
     * @required
     * @readonly
     */
    private ArtifactDeployer deployer;

    /**
     * Map that contains the layouts
     * 
     * @component role=
     *            "org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
     */
    private Map repositoryLayouts;

    /**
     * Component used to create a repository
     * 
     * @component
     */
    private ArtifactRepositoryFactory repositoryFactory;

    /**
     * The type of remote repository layout to deploy to. Try <i>legacy</i> for
     * a Maven 1.x-style repository layout.
     * 
     * @parameter expression="${repositoryLayout}" default-value="default"
     * @required
     */
    private String repositoryLayout;

    /**
     * Parameter used to update the metadata to make the artifact as release.
     * 
     * @parameter expression="${updateReleaseInfo}" default-value="false"
     */
    private boolean updateReleaseInfo;

    /**
     * Whether to deploy snapshots with a unique version or not.
     * 
     * @parameter expression="${uniqueVersion}" default-value="true"
     */
    private boolean uniqueVersion;

    /**
     * @parameter expression="${mirrorRepositories}"
     * @required
     */
    private MirrorRepository[] mirrorRepositories;

    /**
     * @parameter expression="${localRepository}"
     * @required
     * @readonly
     */
    private ArtifactRepository localRepository;

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * Deploy all artifacts for the project to each mirror repository.
     */
    public void execute() throws MojoExecutionException, MojoFailureException {
        ArtifactRepositoryLayout layout;

        layout = (ArtifactRepositoryLayout) repositoryLayouts
                .get(repositoryLayout);

        for (int i = 0; i < mirrorRepositories.length; i++) {
            MirrorRepository mirrorRepository = mirrorRepositories[i];

            ArtifactRepository deploymentRepository = repositoryFactory
                    .createDeploymentArtifactRepository(mirrorRepository
                            .getRepositoryId(), mirrorRepository.getUrl(),
                            layout, uniqueVersion);

            String protocol = deploymentRepository.getProtocol();

            if ("".equals(protocol) || protocol == null) {
                throw new MojoExecutionException("No transfer protocol found.");
            }

            deployToRepository(deploymentRepository);
        }

    }

    /**
     * Deploy all artifacts to the passed repository.
     */
    private void deployToRepository(ArtifactRepository repo)
            throws MojoExecutionException {
        String protocol = repo.getProtocol();

        if (protocol.equalsIgnoreCase("scp")) {
            File sshFile = new File(System.getProperty("user.home"), ".ssh");

            if (!sshFile.exists()) {
                sshFile.mkdirs();
            }
        }

        File pomFile = project.getFile();
        Artifact artifact = project.getArtifact();
        // Deploy the POM
        boolean isPomArtifact = "pom".equals(project.getPackaging());
        if (!isPomArtifact) {
            ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact,
                    pomFile);
            artifact.addMetadata(metadata);
        }

        if (updateReleaseInfo) {
            artifact.setRelease(true);
        }

        try {
            List attachedArtifacts = project.getAttachedArtifacts();

            if (isPomArtifact) {
                deployer.deploy(pomFile, artifact, repo, localRepository);
            } else {
                File file = artifact.getFile();

                if (file != null && !file.isDirectory()) {
                    deployer.deploy(file, artifact, repo, localRepository);
                } else if (!attachedArtifacts.isEmpty()) {
                    getLog()
                            .info(
                                    "No primary artifact to deploy, deploy attached artifacts instead.");
                } else {
                    String message = "The packaging for this project did not assign a file to the build artifact";
                    throw new MojoExecutionException(message);
                }
            }

            for (Iterator i = attachedArtifacts.iterator(); i.hasNext();) {
                Artifact attached = (Artifact) i.next();

                deployer.deploy(attached.getFile(), attached, repo,
                        localRepository);
            }
        } catch (ArtifactDeploymentException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
}

mojo引用MirrorRepository类型来封装repositoryId和url,它是一个简单的bean:

package name.seller.rich;

public class MirrorRepository {
    private String repositoryId;
    private String url;

    public String getRepositoryId() {
        return repositoryId;
    }

    public void setRepositoryId(String repositoryId) {
        this.repositoryId = repositoryId;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

这是使用插件的示例配置。请注意支持所有部署格式(http,scp,ftp):

<plugin>
  <groupId>name.seller.rich</groupId>
  <artifactId>maven-mirror-plugin</artifactId>
  <executions>
    <execution>
      <id>mirror</id>
      <phase>deploy</phase>
      <goals>
        <goal>mirror</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mirrorRepositories>
      <mirrorRepository>
        <repositoryId>mirror</repositoryId>
        <url>http://path/to/mirror</url>
      </mirrorRepository>
    </mirrorRepositories>
    <!--any other deploy configuration needed-->
  </configuration>
</plugin>

答案 2 :(得分:0)

Artifactory 具有自动导出功能。来自the documentation

  

您可以自动并定期备份整个Artifactory系统。   备份过程在目标备份目录中创建带时间戳的目录(或zip文件),与使用元数据运行完整系统导出基本相同。 [...]每个备份都有自己的计划并排除某些存储库[...]

     

备份内容(提取时)采用标准Maven格式,可以加载到任何外部Maven存储库中   [...]

     

Artifactory支持以递增方式备份到目标备份目录中的同一目标目录(名为“current”)。这种备份只是向输出目录写入增量,从而实现极快的备份。

这不正是你需要的吗? 要传输文件,您可以将共享目录挂载到远程服务器并在那里进行备份,或者在本地执行备份,然后再进行rsync。

答案 3 :(得分:0)

我认为在Artifactory中,默认情况下,它维护着不同的逻辑存储库,用于上传快照和非快照。使用权限,您可以使快照存储库仅对某些人可见。

如果这还不够,另一个与Artifactory 2.0一起使用的解决方案是让Artifactory使用MySQL数据库,该数据库对另一个MySQL数据库进行异步复制,而后者又由Artifactory的单独安装读取。如果这太实时,您可以简单地根据业务规则进行两种不同的安装。