maven之 web项目改造-根据jar包 生成pom.xml

根据jar包生成pom.xml denpendency引用

源码如下

package com.lee;
import java.io.File;
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 org.jsoup.Jsoup;

import com.alibaba.fastjson.JSONObject;
public class ConvertJarToPOM {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Element dependencys = new DOMElement("dependencys");
		File dir = new File("C:\\Users\\James Taylor\\Desktop\\HLNovel\\WebContent\\WEB-INF\\lib");
		for (File jar : dir.listFiles()) {
			JarInputStream jis = new JarInputStream(new FileInputStream(jar));
			Manifest mainmanifest = jis.getManifest();
			jis.close();
			if(mainmanifest==null) {
				System.out.println("====jar包未查到======="+jar.getName());
				continue;
			}
			String bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name");
			String bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version");
			Element ele = null;
			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) {
				bundleName = "";
				bundleVersion = "";
				String[] ns = jar.getName().replace(".jar", "").split("-");
				for (String s : ns) {
					if (Character.isDigit(s.charAt(0))) {
						bundleVersion += s + "-";
					} else {
						bundleName += 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.setLength(0);
				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));
			}
			dependencys.add(ele);
			System.out.println();
		}
		System.out.println(dependencys.asXML());
	}

	public static Element getDependices(String key, String ver) {
		Element dependency = new DOMElement("dependency");
		// 设置代理
		// System.setProperty("http.proxyHost", "127.0.0.1");
		// System.setProperty("http.proxyPort", "8090");
		try {
			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(groupId);
				dependency.add(artifactId);
				dependency.add(version);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dependency;
	}

}

pom.xml

<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>
  <groupId>com.lee</groupId>
  <artifactId>NGC7293</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>NGC7293 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  
    <dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.12</version>
	    <scope>test</scope>
	</dependency>

	
	<dependency>
	    <groupId>org.dom4j</groupId>
	    <artifactId>dom4j</artifactId>
	    <version>2.0.0</version>
	</dependency>

	
    
    <dependency>
		<groupId>org.jsoup</groupId>
		<artifactId>jsoup</artifactId>
		<version>1.8.3</version>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.6</version>
	</dependency>
	
    
  </dependencies>
  <build>
    <finalName>NGC7293</finalName>
  </build>
</project>

结果:

 

转载于:https://my.oschina.net/u/3551274/blog/1503488

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值