烦恼:当我们手上有一堆三方件jar包,想要转成maven管理时,需要一个一个配置进pom文件中,而且GAV信息还得去收集。
为了快速生成如下信息,我们可以这样....
GAV:groupId + artifactId + version
<dependency>
<groupId></group>
<artifactId></artifactId>
<version></version>
</dependency>
1.通过解压jar包文件,从中提取GAV信息
原理:jar包中的pom.properties文件记录了GAV信息。(由于不是每个jar包都有pom.properties文件的,所以没有该文件的则查询不到。此时可以用方法2,或者手动上网搜索)
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 [email protected] 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>" [ -d "$dirName/$jarName" ] && rm -rf "$dirName/$jarName" 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 [email protected]
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(StringUtils.format("<dependency>\n<groupId>{}</groupId>\n<artifactId>{}</artifactId>\n<version>{}</version>\n</dependency>", groupId, artifactId, version)); 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(StringUtils.format("success: {}, failed: {}, sum: {}", successCount, notFoundList.size(), jarFilePaths.length)); System.out.println(StringUtils.format("notFoundList: {}", notFoundList)); }
ps:代码中的 StringUtils.format() 可能会有编译错误,是我自己封装的字符串拼接,主要是为了拼接字符串好看,改成普通的字符串拼接就行。
public static String format(String str, Object... args) {
return String.format(str.replaceAll("\\{\\}", "%s"), args);
}
原文地址:https://www.cnblogs.com/zhangzongjian/p/11784930.html