3.7. Uploading Artifacts to Maven Repository

There may be scenarios when your project may fail to fetch dependencies from a remote repository configured in its pom.xml. In such cases, you can programmatically upload dependencies to JBoss BPM Suite by uploading artifacts to the embedded maven repository through Business Central. JBoss BPM Suite uses a servlet for the maven repository interactions. This servlet processes a GET request to download an artifact and a POST request to upload one. You can leverage the servlet's POST request to upload an artifact to the repository via REST. To do this, implement the Http basic authentication and issue an HTTP POST request in the following format:
[protocol]://[hostname]:[port]/[context-root]/maven2/[groupId replacing '.' with '/']/[artifactId]/[version]/[artifactId]-[version].jar
For example, to upload the org.slf4j:slf4j-api:1.7.7 jar, where artifactId is slf4j-api, groupId is slf4j, and version is 1.7.7, the URI must be:
http://localhost:8080/business-central/maven2/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar
The following example illustrates uploading a jar located at /tmp directory as a user bpmsAdmin with the password abcd1234!, to an instance of JBoss BPM Suite running locally:
package com.rhc.example;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UploadMavenArtifact {
	private static final Logger LOG = LoggerFactory.getLogger(UploadMavenArtifact.class);

	public static void main(String[] args) {

		//Maven coordinates
		String groupId = "com.rhc.example";
		String artifactId = "bpms-upload-jar";
		String version = "1.0.0-SNAPSHOT";


		//File to upload
		File file = new File("/tmp/"+artifactId+"-"+version+".jar");

		//Server properties
		String protocol = "http";
		String hostname = "localhost";
		Integer port = 8080;
		String username = "bpmsAdmin";
		String password = "abcd1234!";


		//Create the HttpEntity (body of our POST)
		FileBody fileBody = new FileBody(file);
		MultipartEntityBuilder builder = MultipartEntityBuilder.create();
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
		builder.addPart("upfile", fileBody);
		HttpEntity entity = builder.build();


		//Calculate the endpoint from the maven coordinates
		String resource = "/business-central/maven2/" + groupId.replace('.', '/') + "/" + artifactId +"/" + version + "/" + artifactId + "-" + version + ".jar";

		LOG.info("POST " + hostname + ":" + port + resource);

		//Set up HttpClient to use Basic pre-emptive authentication with the provided credentials
		HttpHost target = new HttpHost(hostname, port, protocol);
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(username,password));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider).build();
        HttpPost httpPost = new HttpPost(resource);
		httpPost.setEntity(entity);
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(target, basicAuth);
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

		try {
			//Perform the HTTP POST
			CloseableHttpResponse response = httpclient.execute(target, httpPost, localContext);
			LOG.info(response.toString());
			//Now check your artifact repository!
		} catch (ClientProtocolException e) {
			LOG.error("Protocol Error", e);
			throw new RuntimeException(e);
		} catch (IOException e) {
			LOG.error("IOException while getting response", e);
			throw new RuntimeException(e);
		}
	}
}
Alternative Maven Approach

An alternative maven approach is to configure your projects pom.xml by adding the repository as shown below:

    <distributionManagement>
      <repository>
        <id>guvnor-m2-repo</id>
        <name>maven repo</name>
        <url>http://localhost:8080/business-central/maven2/</url>
        <layout>default</layout>
      </repository>
    </distributionManagement>
Once you specify the repository information in the pom.xml, add the corresponding configuration in settings.xml as shown below:
    <server>
      <id>guvnor-m2-repo</id>
      <username>bpmsAdmin</username>
      <password>abcd1234!</password>
      <configuration>
        <wagonProvider>httpclient</wagonProvider>
        <httpConfiguration>
          <all>
            <usePreemptive>true</usePreemptive>
          </all>
        </httpConfiguration>
      </configuration>
    </server>
Now when you run the mvn deploy command, the jar file gets uploaded.