首先向pom文件中添加依赖。
<properties>
<aetherVersion>1.0.0.v20140518</aetherVersion>
<mavenVersion>3.1.0</mavenVersion>
<wagonVersion>1.0</wagonVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-util</artifactId>
<version>${aetherVersion}</version>
</dependency>
<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.eclipse.aether</groupId>
<artifactId>aether-transport-wagon</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>${wagonVersion}</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
以下为上传和下载代码。
public class Params {
/**
* jar包在maven仓库中的groupId
*/
private String groupId;
/**
* jar包在maven仓库中的artifactId
*/
private String artifactId;
/**
* jar包在maven仓库中的version
*/
private String version;
/**
* 远程maven仓库的URL地址
*/
private String repository;
/**
* 下载的jar包存放的目标地址,默认为./temp
*/
private String target="G:/";
/**
* 登录远程maven仓库的用户名,若远程仓库不需要权限,设为null,默认为null
*/
private String username=null;
/**
* 登录远程maven仓库的密码,若远程仓库不需要权限,设为null,默认为null
*/
private String password=null;
/**
*
*/
private String uploadFileName;
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public Params() {
super();
}
public Params(String groupId, String artifactId) {
super();
this.groupId = groupId;
this.artifactId = artifactId;
}
public Params(String groupId, String artifactId, String username,
String password) {
super();
this.groupId = groupId;
this.artifactId = artifactId;
this.username = username;
this.password = password;
}
public Params(String groupId, String artifactId, String version,
String repository, String target, String username, String password) {
super();
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.repository = repository;
this.target = target;
this.username = username;
this.password = password;
}
public Params(String groupId, String artifactId, String version,
String username, String password) {
super();
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.username = username;
this.password = password;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getArtifactId() {
return artifactId;
}
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getRepository() {
return repository;
}
public void setRepository(String repository) {
this.repository = repository;
}
public String getTarget() {
return target;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setTarget(String target) {
this.target = target;
}
}import java.io.File;
import java.util.List;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.deployment.DeployRequest;
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.Authentication;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.DependencyRequest;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.resolution.VersionRangeResult;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;
import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
import org.eclipse.aether.util.repository.AuthenticationBuilder;
import org.eclipse.aether.version.Version;
public class MavenDownUp {
/**
* 初始化RepositorySystem
*
* @return RepositorySystem
*/
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);
}
/**
* create a repository system session
*
* @param system
* RepositorySystem
* @return RepositorySystemSession
*/
private static RepositorySystemSession newSession(RepositorySystem system, String target) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
/**
* 设置本地路径
*/
LocalRepository localRepo = new LocalRepository( /* "target/local-repo" */target);
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
return session;
}
/**
*
* @param params
* Params
* @return version 所有的版本号〃
* @throws VersionRangeResolutionException
*/
public static List
getAllVersions(Params params) throws VersionRangeResolutionException {
String groupId = params.getGroupId();
String artifactId = params.getArtifactId();
String repositoryUrl = params.getRepository();
String target = params.getTarget();
String username = params.getUsername();
String password = params.getPassword();
RepositorySystem repoSystem = newRepositorySystem();
RepositorySystemSession session = newSession(repoSystem, target);
RemoteRepository central = null;
if (username == null && password == null) {
central = new RemoteRepository.Builder("central", "default", repositoryUrl).build();
} else {
Authentication authentication = new AuthenticationBuilder().addUsername(username).addPassword(password)
.build();
central = new RemoteRepository.Builder("central", "default", repositoryUrl)
.setAuthentication(authentication).build();
}
Artifact artifact = new DefaultArtifact(groupId + ":" + artifactId + ":[0,)");
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact(artifact);
rangeRequest.addRepository(central);
VersionRangeResult rangeResult = repoSystem.resolveVersionRange(session, rangeRequest);
List
versions = rangeResult.getVersions();
System.out.println("Available versions " + versions);
return versions;
}
public static void downLoadDependency(Params params) throws DependencyCollectionException, DependencyResolutionException{
String groupId = params.getGroupId();
String artifactId = params.getArtifactId();
String version = params.getVersion();
String repositoryUrl = params.getRepository();
String target = params.getTarget();
String username = params.getUsername();
String password = params.getPassword();
RepositorySystem repoSystem = newRepositorySystem();
RepositorySystemSession session = newSession(repoSystem, target);
RemoteRepository central = null;
Dependency dependency =
new Dependency( new DefaultArtifact( groupId + ":" + artifactId + ":" + version ), "compile" );
if (username == null && password == null) {
central = new RemoteRepository.Builder("central", "default", repositoryUrl).build();
} else {
Authentication authentication = new AuthenticationBuilder().addUsername(username).addPassword(password)
.build();
central = new RemoteRepository.Builder("central", "default", repositoryUrl)
.setAuthentication(authentication).build();
}
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot( dependency );
collectRequest.addRepository( central );
DependencyNode node = repoSystem.collectDependencies( session, collectRequest ).getRoot();
DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot( node );
repoSystem.resolveDependencies(session, dependencyRequest);
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept( nlg );
System.out.println( nlg.getClassPath() );
}
public static void upLoad(Params params) throws DependencyCollectionException, DependencyResolutionException, DeploymentException{
String groupId = params.getGroupId();
String artifactId = params.getArtifactId();
String version = params.getVersion();
String repositoryUrl = params.getRepository();
String target = params.getTarget();
String username = params.getUsername();
String password = params.getPassword();
RepositorySystem repoSystem = newRepositorySystem();
RepositorySystemSession session = newSession(repoSystem, target);
RemoteRepository central = null;
if (username == null && password == null) {
central = new RemoteRepository.Builder("central", "default", repositoryUrl).build();
} else {
Authentication authentication = new AuthenticationBuilder().addUsername(username).addPassword(password)
.build();
central = new RemoteRepository.Builder("central", "default", repositoryUrl)
.setAuthentication(authentication).build();
}
Artifact artifact = new DefaultArtifact(groupId ,artifactId ,null,"jar",version,null,new File(params.getUploadFileName()));
DeployRequest deployRrequest = new DeployRequest();
deployRrequest.setRepository(central);
deployRrequest.addArtifact(artifact);
repoSystem.deploy(session, deployRrequest);
}
/**
*
* @param artifact
* maven-jar(groupId:artifactId:version)
* @param repositoryURL
* maven 仓库url
* @param username
* 登录时用户名
* @param password
* 登录时密码
* @throws ArtifactResolutionException
*/
public static void downLoad(Params params) throws ArtifactResolutionException {
String groupId = params.getGroupId();
String artifactId = params.getArtifactId();
String version = params.getVersion();
String repositoryUrl = params.getRepository();
String target = params.getTarget();
String username = params.getUsername();
String password = params.getPassword();
RepositorySystem repoSystem = newRepositorySystem();
RepositorySystemSession session = newSession(repoSystem, target);
RemoteRepository central = null;
if (username == null && password == null) {
central = new RemoteRepository.Builder("central", "default", repositoryUrl).build();
} else {
Authentication authentication = new AuthenticationBuilder().addUsername(username).addPassword(password)
.build();
central = new RemoteRepository.Builder("central", "default", repositoryUrl)
.setAuthentication(authentication).build();
}
Artifact artifact = new DefaultArtifact(groupId + ":" + artifactId + ":" + version);
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.addRepository(central);
artifactRequest.setArtifact(artifact);
repoSystem.resolveArtifact(session, artifactRequest);
System.out.println("success");
}
}
import java.io.IOException;
import com.util.MavenDownUp;
import com.util.Params;
public class Main {
public static void main(String[] args) throws IOException{
testDownLoad();
}
public static void testDownLoad(){
Params params = new Params();
params.setGroupId("tk.mybatis");
params.setArtifactId("mapper");
params.setVersion("3.3.9.2");
params.setUsername("custom");
params.setPassword("custom123");
params.setRepository("http://192.168.2.210:8081/repository/3rdParty/");
try {
MavenDownUp.downLoad(params);//下载jar
} catch (Exception e) {
System.out.println("下载失败");
e.printStackTrace();
}
}
public static void testUpLoad(){
Params params = new Params();
params.setGroupId("tk.mybatis");
params.setArtifactId("mapper");
params.setVersion("3.3.9.2");
params.setUsername("custom");
params.setPassword("custom123");
params.setRepository("http://192.168.2.210:8081/repository/3rdParty/");
params.setUploadFileName("G:\\mapper-3.3.9.2.jar");
/*@SuppressWarnings("static-access")
JSONObject obj = new JSONObject().fromObject(params);
System.out.println(obj.toString());*/
try {
MavenDownUp.upLoad(params);//上传jar
} catch (Exception e) {
System.out.println("上传失败");
e.printStackTrace();
}
}
}