fix(TUP-18791):Update the library nexus2 deploy to use aether (#1266)

This commit is contained in:
wchen-talend
2017-11-06 11:10:05 +08:00
committed by hcyi
parent eb9525aa9f
commit ccdbca23ed

View File

@@ -13,27 +13,16 @@
package org.talend.librariesmanager.nexus;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.DefaultHttpClient;
import org.talend.commons.exception.ExceptionHandler;
import org.talend.core.GlobalServiceRegister;
import org.talend.core.model.general.INexusService;
import org.eclipse.m2e.core.MavenPlugin;
import org.talend.core.nexus.IRepositoryArtifactHandler;
import org.talend.core.nexus.NexusConstants;
import org.talend.core.nexus.NexusServerUtils;
import org.talend.core.runtime.maven.MavenArtifact;
import org.talend.core.runtime.maven.MavenConstants;
import org.talend.designer.maven.utils.PomUtil;
import org.talend.core.runtime.maven.MavenUrlHelper;
import org.talend.designer.maven.aether.RepositorySystemFactory;
/**
* created by wchen on Aug 2, 2017 Detailled comment
@@ -102,23 +91,19 @@ public class Nexus2RepositoryHandler extends AbstractArtifactRepositoryHandler {
*/
@Override
public void deploy(File content, String groupId, String artifactId, String classifier, String extension, String version)
throws IOException {
try {
// TODO keep the nexus2 work as before, but need to remove the delete code latter
MavenArtifact artifact = new MavenArtifact();
artifact.setArtifactId(artifactId);
artifact.setGroupId(groupId);
artifact.setClassifier(classifier);
artifact.setType(version);
artifact.setVersion(version);
deleteOldEntity(artifact, extension);
} catch (Exception e) {
ExceptionHandler.process(e);
}
if (GlobalServiceRegister.getDefault().isServiceRegistered(INexusService.class)) {
INexusService nexusService = (INexusService) GlobalServiceRegister.getDefault().getService(INexusService.class);
nexusService.upload(serverBean, groupId, artifactId, version, content.toURI().toURL());
throws Exception {
String repositoryId = "";
boolean isRelease = !version.endsWith(MavenUrlHelper.VERSION_SNAPSHOT);
if (isRelease) {
repositoryId = serverBean.getRepositoryId();
} else {
repositoryId = serverBean.getSnapshotRepId();
}
String repositoryurl = getRepositoryURL(isRelease);
String localRepository = MavenPlugin.getMaven().getLocalRepositoryPath();
RepositorySystemFactory.deploy(content, localRepository, repositoryId, repositoryurl, serverBean.getUserName(),
serverBean.getPassword(), groupId, artifactId, classifier, extension, version);
}
@Override
@@ -131,48 +116,4 @@ public class Nexus2RepositoryHandler extends AbstractArtifactRepositoryHandler {
return new Nexus2RepositoryHandler();
}
private void deleteOldEntity(MavenArtifact artifact, String type) throws Exception {
String target = null;
if (artifact.getVersion() != null && !artifact.getVersion().endsWith(MavenConstants.SNAPSHOT)) {
target = getRepositoryURL(true);
}
if (target == null) {
return;
}
String artifactPath = PomUtil.getArtifactPath(artifact);
if (!artifactPath.endsWith(type)) {
if (artifactPath.lastIndexOf(".") != -1) {
artifactPath = artifactPath.substring(0, artifactPath.lastIndexOf(".") + 1) + type;
} else {
artifactPath = artifactPath + "." + type;
}
}
target = target + artifactPath;
URL targetURL = new URL(target);
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpHead httpHead = null;
HttpResponse response = null;
StatusLine statusLine = null;
if (targetURL.getFile() != null && !targetURL.getFile().endsWith("SNAPSHOT.jar")) {
httpClient.getCredentialsProvider().setCredentials(new AuthScope(targetURL.getHost(), targetURL.getPort()),
new UsernamePasswordCredentials(serverBean.getUserName(), serverBean.getPassword()));
httpHead = new HttpHead(targetURL.toString());
response = httpClient.execute(httpHead);
statusLine = response.getStatusLine();
int responseResult = statusLine.getStatusCode();
if (responseResult == 200) {
HttpDelete httpDelete = new HttpDelete(targetURL.toString());
httpClient.execute(httpDelete);
}
}
} catch (Exception e) {
throw new Exception(targetURL.toString(), e);
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}