【问题标题】:Quick way to programmatically deploy artifacts to Nexus (in Java)以编程方式将工件部署到 Nexus 的快速方法(在 Java 中)
【发布时间】:2016-08-08 14:35:14
【问题描述】:

我目前正在编写一个 Java 程序,用于将大量遗留 jar 部署到 Nexus。我的方法是调用在命令行上启动deploy:deploy-file 目标的进程

mvn deploy:deploy-file ...

这很慢。我想知道是否有更快的方法来做到这一点?

【问题讨论】:

    标签: java maven command-line-interface nexus


    【解决方案1】:

    如果您专门针对 Nexus,您可能会发现使用 their REST API 执行上传更简单:

    以下是一些使用 curl 的示例。

    1. 上传工件并生成 pom 文件:

      curl -v -F r=releases -F hasPom=false -F e=jar -F g=com.test -F a=project -F v=1.0 -F p=jar -F file=@project-1.0.jar -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content
      
    2. 使用 pom 文件上传工件:

      curl -v -F r=releases -F hasPom=true -F e=jar -F file=@pom.xml -F file=@project-1.0.jar -u admin:admin123 http://localhost:8081/nexus/service/local/artifact/maven/content
      

    在 Java 程序中,您可以使用 HttpURLConnection 进行 POST 调用(example of that hereauthentication heredocumentation of cURL here)。基本上,在 POST 参数中,您需要有 r=releaseshasPom=true(或 false,如果您使用它上传 POM)、e 用于工件的扩展,g、@987654338 @、vp 用于坐标(groupId、artifactId、版本和打包),最后file 用于部署文件。

    请注意,您将无法上传快照because it is explicitely disabled


    如果您想要一个更通用的解决方案,它适用于任何工件和任何远程存储库(甚至是本地存储库),您可以直接使用 Maven 3.1 及更高版本在场景下使用的 Aether API。该团队在DeployArtifacts 示例中提供了此类任务的示例。

    将 Aether 依赖项添加到您的项目中:

    <dependencies>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-impl</artifactId>
            <version>${aetherVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-connector-basic</artifactId>
            <version>${aetherVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-transport-file</artifactId>
            <version>${aetherVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.aether</groupId>
            <artifactId>aether-transport-http</artifactId>
            <version>${aetherVersion}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-aether-provider</artifactId>
            <version>${mavenVersion}</version>
        </dependency>
    </dependencies>
    <properties>
        <aetherVersion>1.1.0</aetherVersion>
        <mavenVersion>3.3.9</mavenVersion>
    </properties>
    

    然后您可以使用以下代码来部署工件:

    public static void main(String[] args) throws DeploymentException {
        RepositorySystem system = newRepositorySystem();
        RepositorySystemSession session = newSession(system);
    
        Artifact artifact = new DefaultArtifact("groupId", "artifactId", "classifier", "extension", "version");
        artifact = artifact.setFile(new File("/path/to/file"));
    
        // add authentication to connect to remove repository
        Authentication authentication = new AuthenticationBuilder().addUsername("username").addPassword("password").build();
    
        // creates a remote repo at the given URL to deploy to
        RemoteRepository distRepo = new RemoteRepository.Builder("id", "default", "url").setAuthentication(authentication).build();
    
        DeployRequest deployRequest = new DeployRequest();
        deployRequest.addArtifact(artifact);
        deployRequest.setRepository(distRepo);
    
        system.deploy(session, deployRequest);
    }
    
    private static RepositorySystem newRepositorySystem() {
        DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
        locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
        locator.addService(TransporterFactory.class, FileTransporterFactory.class);
        locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
        return locator.getService(RepositorySystem.class);
    }
    
    private static RepositorySystemSession newSession(RepositorySystem system) {
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        LocalRepository localRepo = new LocalRepository("target/local-repo");
        session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
        return session;
    }
    

    此代码会将具有给定坐标(groupId、artifactId、类型、分类器和版本)的单个工件部署到配置的远程存储库:

    • 在坐标中,您可以传递一个空字符串将其留空。例如,要在没有分类器的情况下进行部署,您可以使用 "" 作为分类器。
    • 要部署的文件在Artifact 上使用setFile 方法设置。
    • 远程存储库配置有其 ID、布局和 URL。 "default" 布局是 Maven 2 存储库使用的布局(与 Maven 1 的 "legacy" 布局相反)。该 URL 与您在 deploy-file goal 中使用的 URL 相同,因此是 file:///C:/m2-reposcp://host.com/path/to/repo
    • 如有必要,您可以创建一个Authentication 以连接到远程存储库(如sn-p所示)。

    如果您希望使用它部署附加的工件,例如 POM 文件,您可以使用以下命令创建 SubArtifact

    Artifact pomArtifact = new SubArtifact(artifact, "", "pom");
    pomArtifact = pomArtifact.setFile(new File("pom.xml"));
    

    这会将不带分类器的 POM 工件附加到上面配置的工件上。然后你可以像主请求一样将它添加到部署请求中:

    deployRequest.addArtifact(artifact).addArtifact(pomArtifact);
    

    它们都将被部署。

    【讨论】:

    【解决方案2】:

    您可以使用 Eclipse Aether API 在 Java 中以编程方式完成。查看我的Maven Repository Tools 的来源以获取更多详细信息。事实上,如果您的所有工件都已经在 Maven 存储库格式的本地文件夹中,您可能可以直接使用它来满足您的需求。

    具体部署相关代码在

    https://github.com/simpligility/maven-repository-tools/blob/master/maven-repository-provisioner/src/main/java/com/simpligility/maven/provisioner/MavenRepositoryDeployer.java

    【讨论】:

      猜你喜欢
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多