2009-09-17 22 views
18

Tôi có một plugin Maven có một phiên bản groupId, artifactId và phiên bản trong sự kết hợp của nó.Làm cách nào để tải xuống các tạo phẩm Maven trong một plugin?

Tôi muốn có thể tải xuống phần tạo tác đó từ kho lưu trữ từ xa và sao chép tệp vào dự án. Tôi không thể tìm ra cách để tải về các tạo tác mặc dù.

Tôi hiểu rằng tôi có thể giải quyết các phụ thuộc bằng cách sử dụng plugin phụ thuộc, nhưng tôi cần nó xảy ra bên trong plugin của mình. Tôi có thể làm cái này như thế nào?

Trả lời

24

Plugin của bạn cần tạo một tạo phẩm bằng ArtifactFactory và groupId, artifactId và phiên bản của tạo tác được khởi động, sau đó chuyển phần tạo tác đó đến một ArtifactResolver để xử lý việc khám phá/tải xuống.

Trình giải quyết cần quyền truy cập vào kho lưu trữ cục bộ và kho lưu trữ từ xa. Tin vui là tất cả những thành phần đó là bạn có thể khai báo là phụ thuộc trong Mojo của bạn và để Plexus nối chúng vào cho bạn.

Trong another answer Tôi đã trình bày cách thực hiện việc này. Trong trường hợp của bạn, bạn cần một phiên bản cắt giảm với các thông số hơi khác nhau để đọc groupId, artifactId và phiên bản. Trong plugin dưới đây, các thành phần khác nhau được khai báo là thành phần plexus, và các thuộc tính để khai báo groupId, artifactId, phiên bản và loại bao gói.

package name.seller.rich.maven.plugins.bootstrap; 

import java.util.List; 

import org.apache.maven.artifact.Artifact; 
import org.apache.maven.artifact.factory.ArtifactFactory; 
import org.apache.maven.artifact.repository.ArtifactRepository; 
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; 
import org.apache.maven.artifact.resolver.ArtifactResolutionException; 
import org.apache.maven.artifact.resolver.ArtifactResolver; 
import org.apache.maven.plugin.AbstractMojo; 
import org.apache.maven.plugin.MojoExecutionException; 
import org.apache.maven.plugin.MojoFailureException; 

/** 
* Obtain the artifact defined by the groupId, artifactId, and version 
* from the remote repository. 
* 
* @goal bootstrap 
*/ 
public class BootstrapAppMojo extends AbstractMojo { 

    /** 
    * Used to look up Artifacts in the remote repository. 
    * 
    * @parameter expression= 
    * "${component.org.apache.maven.artifact.factory.ArtifactFactory}" 
    * @required 
    * @readonly 
    */ 
    protected ArtifactFactory factory; 

    /** 
    * Used to look up Artifacts in the remote repository. 
    * 
    * @parameter expression= 
    * "${component.org.apache.maven.artifact.resolver.ArtifactResolver}" 
    * @required 
    * @readonly 
    */ 
    protected ArtifactResolver artifactResolver; 

    /** 
    * List of Remote Repositories used by the resolver 
    * 
    * @parameter expression="${project.remoteArtifactRepositories}" 
    * @readonly 
    * @required 
    */ 
    protected List remoteRepositories; 

    /** 
    * Location of the local repository. 
    * 
    * @parameter expression="${localRepository}" 
    * @readonly 
    * @required 
    */ 
    protected ArtifactRepository localRepository; 

    /** 
    * The target pom's artifactId 
    * 
    * @parameter expression="${bootstrapArtifactId}" 
    * @required 
    */ 
    private String bootstrapArtifactId; 

    /** 
    * The target pom's groupId 
    * 
    * @parameter expression="${bootstrapGroupId}" 
    * @required 
    */ 
    private String bootstrapGroupId; 

    /** 
    * The target pom's type 
    * 
    * @parameter expression="${bootstrapType}" 
    * @required 
    */ 
    private String bootstrapType; 

    /** 
    * The target pom's version 
    * 
    * @parameter expression="${bootstrapVersion}" 
    * @required 
    */ 
    private String bootstrapVersion; 

    public void execute() throws MojoExecutionException, MojoFailureException { 
     try { 
      Artifact pomArtifact = this.factory.createArtifact(
       bootstrapGroupId, bootstrapArtifactId, bootstrapVersion, 
       "", bootstrapType); 

      artifactResolver.resolve(pomArtifact, this.remoteRepositories, 
       this.localRepository); 
     } catch (ArtifactResolutionException e) { 
      getLog().error("can't resolve parent pom", e); 
     } catch (ArtifactNotFoundException e) { 
      getLog().error("can't resolve parent pom", e); 
     } 
    } 
} 

Đây là một ví dụ về một pom cấu hình để sử dụng plugin (và tải về pom aspectjrt 1.6.4):

<?xml version="1.0" encoding="UTF-8"?> 
<project> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>name.seller.rich</groupId> 
    <artifactId>bootstrap-test</artifactId> 
    <version>1.0.0</version> 
    <build> 
     <plugins> 
     <plugin> 
      <groupId>name.seller.rich</groupId> 
      <artifactId>maven-bootstrap-plugin</artifactId> 
      <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
       <goal>bootstrap</goal> 
       </goals> 
       <configuration> 
       <bootstrapGroupId>org.aspectj</bootstrapGroupId> 
       <bootstrapArtifactId>aspectjrt</bootstrapArtifactId> 
       <bootstrapVersion>1.6.4</bootstrapVersion> 
       <bootstrapType>pom</bootstrapType> 
       </configuration> 
      </execution> 
      </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 
+0

wow, cảm ơn. Tôi sẽ thử lại –

+0

một lần nữa, nó hoạt động tốt. Cách tốt nhất để có dự án maven cho tệp đã tải xuống là gì? –

+0

cũng thực sự là một câu hỏi riêng: -/ –

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