项目引用的jar包数了一下将近100个,直接引用其他项目的pom文件自然也不适合。所以有了下面的代码,把使用到的jar包放在文件夹中,运行一下,自动生成。
-
-
import java.io.FileInputStream;
-
import java.io.FileNotFoundException;
-
import java.io.IOException;
-
import java.util.jar.JarInputStream;
-
import java.util.jar.Manifest;
-
import org.dom4j.Element;
-
import org.dom4j.dom.DOMElement;
-
-
import com.alibaba.fastjson.JSONObject;
-
-
public static void main(String[] args) throws FileNotFoundException, IOException {
-
Element dependencys = new DOMElement("dependencys");
-
File dir = new File("F:/lib");
-
for (File jar : dir.listFiles()) {
-
JarInputStream jis = new JarInputStream(new FileInputStream(jar));
-
Manifest mainmanifest = jis.getManifest();
-
-
String bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name");
-
String bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version");
-
-
System.out.println(jar.getName());
-
StringBuffer sb = new StringBuffer(jar.getName());
-
if (bundleName != null) {
-
bundleName = bundleName.toLowerCase().replace(" ", "-");
-
sb.append(bundleName+"\t").append(bundleVersion);
-
ele = getDependices(bundleName, bundleVersion);
-
System.out.println(sb.toString());
-
System.out.println(ele.asXML());
-
-
if (ele == null || ele.elements().size() == 0) {
-
-
-
String[] ns = jar.getName().replace(".jar", "").split("-");
-
-
if (Character.isDigit(s.charAt(0))) {
-
bundleVersion += s + "-";
-
-
-
-
-
if (bundleVersion.endsWith("-")) {
-
bundleVersion = bundleVersion.substring(0, bundleVersion.length() - 1);
-
-
if (bundleName.endsWith("-")) {
-
bundleName = bundleName.substring(0, bundleName.length() - 1);
-
-
ele = getDependices(bundleName, bundleVersion);
-
-
sb.append(bundleName+"\t").append(bundleVersion);
-
System.out.println(sb.toString());
-
System.out.println(ele.asXML());
-
-
-
ele = getDependices(bundleName, bundleVersion);
-
if (ele.elements().size() == 0) {
-
ele.add(new DOMElement("groupId").addText("not find"));
-
ele.add(new DOMElement("artifactId").addText(bundleName));
-
ele.add(new DOMElement("version").addText(bundleVersion));
-
-
-
-
-
System.out.println(dependencys.asXML());
-
-
public static Element getDependices(String key, String ver) {
-
Element dependency = new DOMElement("dependency");
-
-
-
-
-
String url = "http://search.maven.org/solrsearch/select?q=a%3A%22" + key + "%22%20AND%20v%3A%22" + ver + "%22&rows=3&wt=json";
-
org.jsoup.nodes.Document doc = Jsoup.connect(url).ignoreContentType(true).timeout(30000).get();
-
String elem = doc.body().text();
-
JSONObject response = JSONObject.parseObject(elem).getJSONObject("response");
-
if (response.containsKey("docs") && response.getJSONArray("docs").size() > 0) {
-
JSONObject docJson = response.getJSONArray("docs").getJSONObject(0);
-
Element groupId = new DOMElement("groupId");
-
Element artifactId = new DOMElement("artifactId");
-
Element version = new DOMElement("version");
-
groupId.addText(docJson.getString("g"));
-
artifactId.addText(docJson.getString("a"));
-
version.addText(docJson.getString("v"));
-
-
dependency.add(artifactId);
-
-
-
-
-
-
-
-
生成之后可能会引用一些依赖,不想要这些依赖的话可以在pom.xml加上这些就不引用依赖了
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
maven项目中pom.xml快速生成
原文:https://www.cnblogs.com/anyiz/p/10659749.html