煩惱:當咱們手上有一堆三方件jar包,想要轉成maven管理時,須要一個一個配置進pom文件中,並且GAV信息還得去收集。java
爲了快速生成以下信息,咱們能夠這樣....bash
GAV:groupId + artifactId + versionmaven
<dependency>spa
<groupId></group>unix
<artifactId></artifactId>code
<version></version>blog
</dependency>ip
1.經過解壓jar包文件,從中提取GAV信息get
原理:jar包中的pom.properties文件記錄了GAV信息。(因爲不是每一個jar包都有pom.properties文件的,因此沒有該文件的則查詢不到。此時能夠用方法2,或者手動上網搜索)servlet
1.1 sh腳本方式(仍是以爲腳本好用)
getGAV.sh /tmp/lib/javax.servlet-3.0.1.jar test/lib/javax.servlet-3.0.1.jar
參數傳文件路徑就行,支持多參數
#!/bin/bash ##獲取jar包的maven依賴GAV信息 function main() { local notFoundList for var in $@ do if [ ! -z "$var" ];then local jarName=`echo $var | sed 's#.*/##' | sed 's#.jar##g'` local dirName=`dirname $var` [ ! -d "$dirName/$jarName" ] && unzip $var -d $dirName/$jarName &>/dev/null if [ -d "$dirName/$jarName" ];then local pomProperties=`find $dirName/$jarName -name pom.properties` if [ "$pomProperties" != "" ];then dos2unix $pomProperties &>/dev/null echo "<dependency>" echo "<groupId>"`grep "groupId" $pomProperties | cut -d'=' -f2`"</groupId>" echo "<artifactId>"`grep "artifactId" $pomProperties | cut -d'=' -f2`"</artifactId>" echo "<version>"`grep "version" $pomProperties | cut -d'=' -f2`"</version>" echo "</dependency>" else notFoundList="$var $notFoundList" fi [ -d "$dirName/$jarName" ] && rm -rf "$dirName/$jarName" else notFoundList="$var $notFoundList" fi fi done if [ "$notFoundList" != "" ];then echo "=============" echo "notFoundList: $notFoundList" echo "=============" fi } main $@
1.2 java方式
/** * 經過jar包文件解壓,獲取maven的依賴信息(GAV:groupId artifactId version) * * @param jarFilePaths jar包文件名或者全路徑 */ public static void getGavFromJar(String... jarFilePaths) { List<String> notFoundList = new ArrayList<String>(); int successCount = 0; for (String jarFilePath : jarFilePaths) { File jarFile = new File(jarFilePath); if (jarFile.exists() && jarFile.isFile() && jarFilePath.endsWith(".jar")) { String groupId = ""; String artifactId = ""; String version = ""; // 不解壓讀取壓縮包中的文件內容 try ( ZipInputStream zis = new ZipInputStream(new FileInputStream(jarFile)); ZipFile zipFile = new ZipFile(jarFile); ) { ZipEntry zipEntry; boolean hasPomProperties = false; while ((zipEntry = zis.getNextEntry()) != null) { if (zipEntry.getName().startsWith("META-INF/maven") && zipEntry.getName().endsWith("pom.properties")) { try ( BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry))); ) { String line; while ((line = br.readLine()) != null) { if (line.contains("=")) { String[] arrGVA = line.split("="); if (arrGVA.length > 1) { if ("groupId".equals(line.split("=")[0])) { groupId = line.split("=")[1]; } else if ("artifactId".equals(line.split("=")[0])) { artifactId = line.split("=")[1]; } else if ("version".equals(line.split("=")[0])) { version = line.split("=")[1]; } } } } hasPomProperties = true; System.out.println("<dependency>\n<groupId>" + groupId + "</groupId>\n<artifactId>" + artifactId + "</artifactId>\n<version>" + version + "</version>\n</dependency>"); successCount++; } catch (IOException e) { e.printStackTrace(); } break; } } if (!hasPomProperties) { notFoundList.add(jarFilePath); } } catch (IOException e) { e.printStackTrace(); } } else { notFoundList.add(jarFilePath); } } System.out.println(); System.out.println("success: " + successCount + ", failed: " + notFoundList.size() + ", sum: " + jarFilePaths.length)); System.out.println("notFoundList: " + notFoundList); }
2.經過jar包名稱,獲取maven的依賴信息GAV
原理:訪問https://mvnrepository.com/search查詢獲取相關信息。(因爲artifactId和version拿捏得不是百分百準確,因此查詢準確率也不是百分百)
/** * 經過jar包名稱,獲取maven的依賴信息(GAV:groupId artifactId version) * @param jarFilePaths jar包文件名或者全路徑 */ public static void getGavFromWeb(String... jarFilePaths) { List<String> notFoundList = new ArrayList<String>(); int successCount = 0; for(String jarFilePath : jarFilePaths) { if (!StringUtils.isEmpty(jarFilePath) && jarFilePath.endsWith(".jar")) { String searchUrl = "https://mvnrepository.com/search"; File jar = new File(jarFilePath); // 去掉.jar後綴 String jarName = jar.getName().substring(0, jar.getName().lastIndexOf(".")); // 去掉版本號,獲取不必定準確的artifactId,用於關鍵字搜索 String artifactIdSimple = jarName.replaceAll("[-|_]\\d.*", ""); // 獲取不必定準確的version,用於搜索結果篩選 String versionSimple = jarName.substring(artifactIdSimple.length()).replaceFirst("[-|_]", ""); try { Document doc = Jsoup.connect(searchUrl).data("q", artifactIdSimple).get(); Elements ps = doc.getElementsByClass("im-subtitle"); if (!CollectionUtils.isEmpty(ps)) { // artifactId搜索結果取第一條 Element p = ps.get(0); if (p.childNodeSize() > 1) { String artifactUrl = p.child(1).absUrl("href"); // 從search結果的超連接中截取groupId和artifactId String[] ids = artifactUrl.split("/"); String groupId = ids[ids.length - 2]; String artifactId = ids[ids.length - 1]; String version = ""; doc = Jsoup.connect(artifactUrl).get(); Elements as = doc.getElementsByClass("vbtn release"); // version搜索結果中取對應的,若無則取第一條 if (!CollectionUtils.isEmpty(as)) { if (!StringUtils.isEmpty(versionSimple)) { for (Element a : as) { if (versionSimple.equals(a.text())) { version = versionSimple; break; } } } if (StringUtils.isEmpty(version)) { version = as.get(0).text(); } } System.out.println("<dependency>\n<groupId>" + groupId + "</groupId>\n<artifactId>" + artifactId + "</artifactId>\n<version>" + version + "</version>\n</dependency>"); successCount++; } else { notFoundList.add(jarFilePath); } } else { notFoundList.add(jarFilePath); } } catch (IOException e) { e.printStackTrace(); notFoundList.add(jarFilePath); } } else { notFoundList.add(jarFilePath); } } System.out.println(); System.out.println("success: " + successCount + ", failed: " + notFoundList.size() + ", sum: " + jarFilePaths.length); System.out.println("notFoundList: " + notFoundList); }
爲何代碼中這麼多if-else呢,大概是爲了提升容錯率吧。