Java 通过mave命令下载jar包的示例代码
#代码知识 发布时间: 2026-01-12
之前一直都是使用的idea,创建maven工程进行jar包导入操作,居然接到了通过java 代码导入jar包的需求,满脸的懵逼,好在功夫不负有心人,最终将其拿来了,

现在这里记录一下,方便以后学习使用;
本次采用的方案是基于pom.xml模板的形式+maven命令的方式,到仓库下载jar报错,示例代码如下:
项目依赖:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>
pom.xml模板:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<inceptionYear>2019</inceptionYear>
<groupId>com.tx.app</groupId>
<artifactId>autoapi</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>autoapi</name>
<parent>
<groupId>com.tx</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.2</version>
</parent>
<repositories>
<repository>
<id>autoapi</id>
<url>远端仓库地址</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<artifactItems>
<artifactItem>
<groupId>{0}</groupId>
<artifactId>{1}</artifactId>
<version>{2}</version>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
</build>
</project>
代码示例:
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 = "<dependency>\n"
+ " <groupId>commons-io</groupId>\n"
+ " <artifactId>commons-io</artifactId>\n"
+ " <version>2.6</version>\n"
+ " </dependency>";
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");
}
好啦,暂时就先记录在这里,后面有机会在完善
代码知识SEO上一篇 : ant design pro中可控的筛选和排序实例
下一篇 : Java正则表达式工具方法汇总
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!