maven - Quick way to programmatically deploy artifacts to Nexus (in Java) -


i write java program deploys lot of legacy jars nexus. approach invoke process starts deploy:deploy-file goal on command line

mvn deploy:deploy-file ... 

this quite slow. wonder if there faster way this?

if target nexus, might find simpler use their rest api perform upload:

here examples using curl.

  1. uploading artifact , generating 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 artifact 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 

in java program, can use httpurlconnection make post call (example of here authentication here , documentation of curl here). basically, in post parameters, need have r=releases, haspom=true (or false if you're uploading pom it), e extension of artifact, g, a, v , p coordinates (groupid, artifactid, version , packaging) , file file deploy.

note won't able upload snapshots because explicitely disabled.


if want more generic solution, work whatever artifact, , whatever remote repository (even local one), can directly use aether api, used under scenes maven 3.1 , higher. team has example of such task in deployartifacts sample.

add project aether dependencies:

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

and can have following code deploy artifacts:

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 connect remove repository     authentication authentication = new authenticationbuilder().addusername("username").addpassword("password").build();      // creates remote repo @ given url deploy     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; } 

this code deploy single artifact having given coordinates (groupid, artifactid, type, classifier , version) configured remote repository:

  • in coordinates, can pass empty string leave blank. example, deploy without classifier, can use "" classifier.
  • the file deploy set method setfile on artifact.
  • the remote repository configured id, layout , url. "default" layout 1 used maven 2 repository (in contrast "legacy" layout maven 1). url same 1 use inside deploy-file goal, file:///c:/m2-repo or scp://host.com/path/to/repo.
  • if necessary, can create authentication connect remote repository (as shown in snippet).

if wish deploy attached artifacts it, pom file, can create subartifact with:

artifact pomartifact = new subartifact(artifact, "", "pom"); pomartifact = pomartifact.setfile(new file("pom.xml")); 

this attach pom artifact, without classifier, artifact configured above. can add deploy request main one:

deployrequest.addartifact(artifact).addartifact(pomartifact); 

and both of them deployed.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -