国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Java 通過mave命令下載jar包的示例代碼

瀏覽:78日期:2022-08-21 08:42:00

之前一直都是使用的idea,創(chuàng)建maven工程進行jar包導入操作,居然接到了通過java 代碼導入jar包的需求,滿臉的懵逼,好在功夫不負有心人,最終將其拿來了,

現(xiàn)在這里記錄一下,方便以后學習使用;

本次采用的方案是基于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; // 創(chuàng)建下載jar包存儲的位置 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); // 驗證下載成功 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()); } /** * 創(chuàng)建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');}

好啦,暫時就先記錄在這里,后面有機會在完善

到此這篇關于Java 通過mave命令下載jar的示例代碼的文章就介紹到這了,更多相關java mave命令下載jar內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Java
相關文章:
主站蜘蛛池模板: 石柱| 同德县| 广灵县| 七台河市| 甘谷县| 奎屯市| 丹巴县| 南宁市| 庐江县| 民丰县| 浦县| 湄潭县| 大同县| 永安市| 云梦县| 修文县| 蒲城县| 宜兰县| 乌鲁木齐县| 贡山| 游戏| 礼泉县| 和顺县| 沙田区| 乐陵市| 达孜县| 婺源县| 前郭尔| 乌兰察布市| 建湖县| 神农架林区| 富锦市| 富源县| 伊宁县| 乌什县| 汶川县| 广安市| 德江县| 霍州市| 宜良县| 金山区|