有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

maven以编程方式将工件部署到Nexus的快速方法(Java)

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

mvn deploy:deploy-file ...

这相当慢。我想知道有没有更快的方法


共 (2) 个答案

  1. # 1 楼答案

    您可以使用EclipseAetherAPI在Java中以编程方式完成此操作。查看我的Maven Repository Tools的来源以了解更多细节。事实上,如果您的所有工件都已经以Maven repository格式存在于本地文件夹中,那么您可能可以根据需要直接使用它

    具体来说,部署相关的代码在

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

  2. # 2 楼答案

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

    Here are some examples using curl.

    1. Uploading an artifact and generating a pom file:

      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. Uploading an artifact with a pom file:

      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作为工件的扩展名、gavp作为坐标(groupId、artifactId、版本和打包),最后file作为要部署的文件

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


    如果您想要一个更通用的解决方案,这将适用于任何工件,以及任何远程存储库(甚至是本地存储库),您可以直接使用Aether API,Maven 3.1及更高版本在场景下使用该API。团队在^{}样本中有这样一个任务的例子

    将乙醚依赖项添加到项目中:

    <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进行配置。{}布局是Maven 2存储库使用的布局(与Maven 1的{a7}布局相反)。URL与在^{} goal中使用的URL相同,因此file:///C:/m2-reposcp://host.com/path/to/repo
    • 如有必要,您可以创建一个Authentication连接到远程存储库(如代码段所示)

    如果您希望使用它部署附加的工件(如POM文件),您可以创建一个SubArtifact,其中包括:

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

    这将向上面配置的工件附加一个没有分类器的POM工件。然后,您可以将其添加到部署请求中,就像主请求一样:

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

    他们两人都将被部署