如何将lib中的jar以maven的dependency形式导出
该文借鉴自:https://blog.csdn.net/qq_23850035/article/details/88779367,如另行发布,请带上之前的作者
package org.crazyit.activiti;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException {
Element dependencys = new DOMElement("dependencys");
File dir = new File("/Users/xxx/Documents/24_activiti/codes/02/xiaozu/lib"); //lib目录
for (File jar : dir.listFiles()) {
String jarname = jar.getName();
if (jarname.startsWith(".")) {
continue;
}
int index = jarname.lastIndexOf("-");
int jarIndex = jarname.lastIndexOf(".");
System.out.println("jarname = " + jarname);
String bundleName = jarname.substring(0, index);
String bundleVersion = jarname.substring(index + 1, jarIndex);
if (bundleName == null || bundleVersion == null) {
continue;
}
System.out.println("<!--" + jar.getName() + "-->");
System.out.println(getDependices(bundleName, bundleVersion));
System.out.println();
}
}
public static String getDependices(String key, String ver) {
if (ver.equals("sources")) {
return "";
}
// String url ="https://mvnrepository.com/search?q="+key;
String url = "https://maven.aliyun.com/artifact/aliyunMaven/searchArtifactByGav?groupId=&artifactId="+ key +"&version="+ ver +"&repoId=all&_input_charset=utf-8";
org.jsoup.nodes.Document doc = null;
org.jsoup.nodes.Document doc2 = null;
try {
doc = Jsoup.connect(url).ignoreContentType(true).timeout(60000).get();
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000L + new Random().nextInt(200));
} catch (Exception e) {
}
org.jsoup.nodes.Element elem = doc.body();
Object href1 = elem.childNodes().get(0);
HashMap hashMap = JSON.parseObject(JSON.toJSONString(href1), HashMap.class);
HashMap hashMap2 = JSON.parseObject(hashMap.get("wholeText").toString(), HashMap.class);
List<Item> ts = (List<Item>) JSONArray.parseArray(hashMap2.get("object").toString(), Item.class);
// System.out.println(JSON.toJSON(ts));
String groupId = null;
for (Item item : ts) {
if ("jar".equals(item.getPackaging())
&& ver.equals(item.getVersion())
&& key.equals(item.getArtifactId())) {
groupId = item.getGroupId();
break;
}
}
String result = "<dependency>\n" +
" <groupId>" + groupId + "</groupId>\n" +
" <artifactId>" + key + "</artifactId>\n" +
" <version>" + ver + "</version>\n" +
"</dependency>";
return result;
}
}
Item
package activiti;
import com.alibaba.fastjson.JSON;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
public class Item {
private String artifactId;
private String classifier;
private String fileName;
private String groupId;
private String packaging;
private String repositoryId;
private String version;
public String getArtifactId() {
return artifactId;
}
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}
public String getClassifier() {
return classifier;
}
public void setClassifier(String classifier) {
this.classifier = classifier;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getPackaging() {
return packaging;
}
public void setPackaging(String packaging) {
this.packaging = packaging;
}
public String getRepositoryId() {
return repositoryId;
}
public void setRepositoryId(String repositoryId) {
this.repositoryId = repositoryId;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}