Java 通過(guò)mave命令下載jar包的示例代碼
之前一直都是使用的idea,創(chuàng)建maven工程進(jìn)行jar包導(dǎo)入操作,居然接到了通過(guò)java 代碼導(dǎo)入jar包的需求,滿臉的懵逼,好在功夫不負(fù)有心人,最終將其拿來(lái)了,
現(xiàn)在這里記錄一下,方便以后學(xué)習(xí)使用;
本次采用的方案是基于pom.xml模板的形式+maven命令的方式,到倉(cāng)庫(kù)下載jar報(bào)錯(cuò),示例代碼如下:
項(xiàng)目依賴:
<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>遠(yuǎn)端倉(cāng)庫(kù)地址</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 {
// 準(zhǔn)備下載命令
String mavenCommandPath =
System.getProperty("os.name").toLowerCase().startsWith("win") ? "mvn" : mavenPath;
String mvnCmd = mavenCommandPath + MVN_PARAM;
// 創(chuàng)建下載jar包存儲(chǔ)的位置
File workDir = getMavenRepository(jarName);
// 基于模板創(chuàng)建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);
// 驗(yàn)證下載成功
if(null == process || process.waitFor()!= 0){
FileUtils.deleteQuietly(workDir);
throw new Exception("下載maven包失敗,請(qǐng)檢查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());
}
/**
* 創(chuàng)建jar包存儲(chǔ)的文件夾
* @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;
}
}
測(cè)試代碼:
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");
}
好啦,暫時(shí)就先記錄在這里,后面有機(jī)會(huì)在完善
到此這篇關(guān)于Java 通過(guò)mave命令下載jar的示例代碼的文章就介紹到這了,更多相關(guān)java mave命令下載jar內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于Java的打包jar、war、ear包的作用與區(qū)別詳解
- 解決java項(xiàng)目jar打包后讀取文件失敗的問(wèn)題
- 關(guān)于Idea創(chuàng)建Java項(xiàng)目并引入lombok包的問(wèn)題(lombok.jar包免費(fèi)下載)
- Java 使用反射調(diào)用jar包中的類(lèi)方式
- Java(TM) Platform SE binary 打開(kāi)jar文件的操作
- Java運(yùn)行Jar包內(nèi)存配置的操作
- idea打包java可執(zhí)行jar包的實(shí)現(xiàn)步驟
- windows下java -jar 后臺(tái)運(yùn)行以及殺死后臺(tái)進(jìn)程的操作
- windows定時(shí)器配置執(zhí)行java jar文件的方法詳解
- java使用jar包生成二維碼的示例代碼
- Java使用JSONObject需要的6個(gè)jar包下載地址
- Java jar打包工具使用方法步驟解析
- java 一鍵部署 jar 包和 war 包
相關(guān)文章
解決JSTL foEach標(biāo)簽 刷新報(bào)錯(cuò)的方法
本篇文章是對(duì)JSTL foEach標(biāo)簽刷新報(bào)錯(cuò)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Java之System.getProperty()的作用及使用說(shuō)明
這篇文章主要介紹了Java之System.getProperty()的作用及使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
SpringCloud可視化鏈路追蹤系統(tǒng)Zipkin部署過(guò)程
這篇文章主要介紹了SpringCloud可視化鏈路追蹤系統(tǒng)Zipkin部署過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java中StringBuilder與StringBuffer的區(qū)別
在Java編程中,字符串的拼接是一項(xiàng)常見(jiàn)的操作。為了有效地處理字符串的拼接需求,Java提供了兩個(gè)主要的類(lèi):StringBuilder和StringBuffer,本文主要介紹了Java中StringBuilder與StringBuffer的區(qū)別,感興趣的可以了解一下2023-08-08
MyBatis-Plus通用CRUD操作的實(shí)現(xiàn)
MyBatis-Plus是基于MyBatis的增強(qiáng)工具,主要目的是簡(jiǎn)化MyBatis的使用并提升開(kāi)發(fā)效率,它提供了通可以用CRUD操作、分頁(yè)插件、多種插件支持、自動(dòng)代碼生成器等功能,感興趣的可以了解一下2024-10-10

