2009-03-11 34 views
9

Chúng tôi có kho lưu trữ nội bộ artifactory. Tại thời điểm này tất cả các bức ảnh chụp sẽ được triển khai ở đó. Chúng tôi cũng muốn có một máy chủ khác với giao diện web và muốn sao chép nó vào các tạo phẩm được tạo ra.Nhiều triển khai trong maven

Đối với các bản dựng của chúng tôi, chúng tôi sử dụng Hudson, nhưng hành động sau khi xây dựng "Triển khai các tạo phẩm cho kho lưu trữ Maven" cùng với scp không hoạt động. Vì vậy, có một câu hỏi làm nó theo một cách thanh lịch khác. Tại sao maven không thể có một số kho phân phối? Bất kỳ ý tưởng?

Điều đẹp nhất sẽ là nếu tạo tác sẽ hỗ trợ xuất khẩu gia tăng (tự động!) Sang kho lưu trữ maven tiêu chuẩn sau mỗi lần triển khai mới.

Trả lời

11

Tôi không nghĩ rằng maven hỗ trợ triển khai cho nhiều kho lưu trữ cho một cấu hình duy nhất, nhưng có lẽ hồ sơ có thể thay đổi id và url của kho lưu trữ.

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

Maven Deployment

Sau đó, sử dụng hồ sơ để chọn mà repo để triển khai đến:

<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

cảm ơn! này làm việc độc đáo đối với tôi, với một sự thay đổi - nếu bạn đã sử dụng cấu hình cho những thứ khác, có thể bạn sẽ muốn kích hoạt tài sản dựa trên (ví dụ ' repo2 'để kích hoạt repo2, và tương tự với! ! cho repo2, sau đó chỉ '-Drepo2') –

+2

hoặc đặt' distributionManagement' trực tiếp trong định nghĩa 'profile', nếu bạn không muốn sử dụng các thuộc tính –

0

Artifactory không có một tính năng xuất khẩu tự động. Từ the documentation:

Bạn có thể sao lưu tự động và định kỳ toàn bộ hệ thống Artifactory. Quá trình sao lưu tạo thư mục được đặt thời gian (hoặc tệp zip) trong thư mục sao lưu đích và về cơ bản giống như chạy toàn bộ hệ thống xuất với siêu dữ liệu. [...] Mỗi bản sao lưu có thể có lịch biểu riêng và loại trừ các kho lưu trữ nhất định [...]

Nội dung của bản sao lưu (khi được trích xuất) ở định dạng Maven chuẩn và có thể được tải vào bất kỳ kho lưu trữ Maven ngoài nào [ ...]

Artifactory hỗ trợ sao lưu tăng dần lên cùng một thư mục đích (có tên "hiện tại") trong thư mục sao lưu đích. Đây là loại sao lưu chỉ bằng văn bản deltas đến thư mục đầu ra, kết quả sao lưu cực nhanh.

Đó không phải là chính xác những gì bạn cần? Để chuyển các tập tin, bạn có thể gắn kết một thư mục chia sẻ với máy chủ từ xa và thực hiện sao lưu ở đó, hoặc thực hiện sao lưu cục bộ và sau đó rsync nó.

0

Tôi nghĩ trong Artifactory, theo mặc định, nó duy trì các kho lưu trữ logic khác nhau để tải lên ảnh chụp nhanh và không chụp nhanh. Sử dụng quyền, bạn có thể làm cho kho lưu trữ ảnh chụp chỉ hiển thị đối với một số. Nếu đó là không đủ, một giải pháp khác làm việc với Artifactory 2.0 là có Artifactory sử dụng cơ sở dữ liệu MySQL sao chép không đồng bộ vào cơ sở dữ liệu MySQL khác, mà lần lượt được đọc bởi một cài đặt riêng biệt của Artifactory. Nếu đó là quá thời gian thực, bạn chỉ có thể có hai cài đặt khác nhau cập nhật dựa trên quy tắc kinh doanh.

3

Nếu bạn sẵn sàng sử dụng plugin tùy chỉnh, bạn có thể định cấu hình Maven triển khai thành danh sách các vị trí "gương" cùng lúc với triển khai chuẩn. Tôi muốn khuyên bạn nên xác định điều này trong một hồ sơ để bạn có thể kiểm soát những gì triển khai được nhân đôi (nó có thể không thích hợp để làm điều này trên mỗi xây dựng).

Để xác định một plugin mới, bạn cần phải tạo một dự án Maven mới và chỉ định POM đã đóng gói 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> 

Trong src/main/java định nghĩa một Mojo. Đoạn mã dưới đây tuyên bố một mục tiêu "gương", nó lấy một danh sách các mục mirrorRepository (chứa một repositoryId và url) để phản ánh việc triển khai tạo tác. Plugin sử dụng cùng một cách tiếp cận để triển khai như là maven-deploy-plugin, và lấy hầu hết các tham số giống nhau.

Lưu ý rằng bạn vẫn cần xác định máy chủ trong tệp settings.xml của mình cho mỗi kho lưu trữ có quyền phù hợp để thực hiện triển khai hoặc quá trình xây dựng sẽ không thành công!

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); 
     } 
    } 
} 

Các mojo tham chiếu một loại MirrorRepository để đóng gói các repositoryId và url, nó là một bean đơn giản:

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; 
    } 
} 

Dưới đây là một cấu hình ví dụ sử dụng các plugin. Lưu ý rằng tất cả các định dạng triển khai được hỗ trợ (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> 
+0

Plugin này có được xuất bản để dễ tiêu thụ không? – Gunnar

Các vấn đề liên quan