ftp maven 依赖jar包_Java 通过mave命令下载jar包的示例代码

本文介绍了如何使用Java代码结合Maven命令动态下载jar包。通过创建pom.xml模板并调用Maven的dependency-plugin,实现从远程仓库下载指定jar到本地。文中提供了一个完整示例,包括关键代码片段和测试用例,帮助开发者理解这一过程。
摘要由CSDN通过智能技术生成

之前一直都是使用的idea,创建maven工程进行jar包导入操作,居然接到了通过java 代码导入jar包的需求,满脸的懵逼,好在功夫不负有心人,最终将其拿来了,

现在这里记录一下,方便以后学习使用;

本次采用的方案是基于pom.xml模板的形式+maven命令的方式,到仓库下载jar报错,示例代码如下:

项目依赖:

commons-io

commons-io

2.6

pom.xml模板:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

2019

com.tx.app

autoapi

jar

1.0

autoapi

com.tx

parent-pom

1.0.2

autoapi

远端仓库地址

org.apache.maven.plugins

maven-dependency-plugin

3.0.0

{0}

{1}

{2}

代码示例:

public class MavenParse {

private static String mavenPath = "/maven/apache-maven-3.6.3/bin/mvn";

private static final String MVN_PARAM = " dependency:copy -DoutputDirectory=lib -U";

void downloadDependency(DependencyDTO dependencyDTO, String jarName) throws Exception {

// 准备下载命令

String mavenCommandPath =

System.getProperty("os.name").toLowerCase().startsWith("win") ? "mvn" : mavenPath;

String mvnCmd = mavenCommandPath + MVN_PARAM;

// 创建下载jar包存储的位置

File workDir = getMavenRepository(jarName);

// 基于模板创建pom.xml

File pomFile = new File(workDir, "pom.xml");

String pomXml = createPomModel(dependencyDTO);

FileUtils.writeStringToFile(pomFile, pomXml, "utf-8");

Process process = Runtime.getRuntime().exec(mvnCmd, null, workDir);

// 验证下载成功

if(null == process || process.waitFor()!= 0){

FileUtils.deleteQuietly(workDir);

throw new Exception("下载maven包失败,请检查maven配置");

}

}

private String createPomModel(DependencyDTO dependencyDTO) throws IOException {

File pomFile = new File(getClass().getResource("/pom.xml").getFile());

String template = FileUtils.readFileToString(pomFile, "utf-8");

return MessageFormat.format(template,

dependencyDTO.getGroupId(),

dependencyDTO.getArtifactId(),

dependencyDTO.getVersion());

}

/**

* 创建jar包存储的文件夹

* @param fileName

* @return

* @throws Exception

*/

private static File getMavenRepository(String fileName)throws Exception {

String parentPath = System.getProperty("user.dir")+File.separator+"automation";

parentPath = parentPath+File.separator+"mavenParse";

parentPath = parentPath+File.separator+FilenameUtils.getBaseName(fileName);

File dir = new File(parentPath);

try {

FileUtils.forceMkdir(dir);

} catch (Exception e) {

throw new RuntimeException(e);

}

return dir;

}

}

测试代码:

public static void main(String[] args) throws Exception {

String dependency = "\n"

+ " commons-io\n"

+ " commons-io\n"

+ " 2.6\n"

+ " ";

DependencyDTO dependencyDTO = new DependencyDTO();

dependencyDTO.setGroupId("commons-io");

dependencyDTO.setArtifactId("commons-io");

dependencyDTO.setVersion("2.6");

dependencyDTO.setPomContent(dependency);

MavenParse parse = new MavenParse();

parse.downloadDependency(dependencyDTO,"commons-io.jar");

}

好啦,暂时就先记录在这里,后面有机会在完善

到此这篇关于Java 通过mave命令下载jar的示例代码的文章就介绍到这了,更多相关java mave命令下载jar内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值