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

JF *_*ier 5 java command-line-interface nexus maven

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

mvn deploy:deploy-file ...
Run Code Online (Sandbox Code Playgroud)

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

Tun*_*aki 9

如果您专门针对Nexus,您可能会发现使用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
    
    Run Code Online (Sandbox Code Playgroud)
  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
    
    Run Code Online (Sandbox Code Playgroud)

在Java程序中,您可以使用它HttpURLConnection来进行POST调用(此处的示例包含此处身份验证以及此处的cURL文档).基本上,在POST参数,你需要有r=releases,hasPom=true(或者false,如果您要上传与它POM),e对神器的扩展,g,a,vp为坐标(的groupId,artifactId的,版本和包装),最后file为要部署的文件.

请注意,您将无法上载快照,因为它已明确禁用.


如果你想要一个更通用的解决方案,那将适用于任何工件,对于任何远程存储库(甚至是本地存储库),你可以直接使用Aether API,它在Maven 3.1及更高版本的场景下使用.团队在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>
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

  • 在坐标中,您可以传递一个空字符串以将其留空.例如,要在没有分类器的情况下进行部署,可以将其""用作分类器.
  • 要部署的文件是使用上面的方法设置setFileArtifact.
  • 远程存储库配置有ID,布局和URL.的"default"布局是通过使用Maven 2的存储库(对比的所述一个"legacy"布局对Maven 1).该URL与您在deploy-file目标内使用的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"));
Run Code Online (Sandbox Code Playgroud)

这会将POM工件(没有分类器)附加到上面配置的工件.然后,您可以将其添加到部署请求,如主要部分:

deployRequest.addArtifact(artifact).addArtifact(pomArtifact);
Run Code Online (Sandbox Code Playgroud)

并将部署它们.