將jar包轉換成pom.xml文件中依賴配置的工具類

 一般格式的jar包文件都可以轉換,程序思路是去訪問了中央倉庫,然後解析頁面得出依賴配置的,
程序依賴解析器jsoup,web項目用戶自行到網上下載,maven項目粘貼下面代碼即可
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.8.3</version>
</dependency>

package cn.sun;
import java.io.*;
import java.util.List;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

import javax.sound.midi.Soundbank;

/**
 * @aothor 果凍
 * @date 2020-05-21 21:36
 注:將所有jar包轉成pom.xml文件中的依賴配置
 */
public class JarToPom {



    public static void main(String[] args) throws FileNotFoundException, IOException {
        
		String libpath = "F:/ASGD/files/lib";//jar包所在的文件夾路徑
		String outpath = "F:/ASGD/files/dependicy.txt";//輸出maven依賴配置的路徑
		//創建jar包所在的文件夾對象
		File lib = new File(libpath);
        //遍歷文件夾中的jar包
        for (File jar : lib.listFiles()) {
            //獲取jar包文件名稱,如    mysql-connector-java-5.1.8.jar
            String jarName  = jar.getName();
            int index = jarName.lastIndexOf("-");
            int jarIndex = jarName.lastIndexOf(".");

            String  bundleName = jarName.substring(0,index);//得到mysql-connector-java
            String bundleVersion = jarName.substring(index +1 ,jarIndex );//得到5.1.8

            if (bundleName == null || bundleVersion == null){
                continue;
            }

        /*  舉例:  <!-- mysql-connector-java-5.1.38.jar -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.38</version>
                </dependency>
        */

            String dependices = "<!--"+jar.getName()+"-->\r\n"+getDependices(bundleName, bundleVersion)+"\r\n";
			
            BufferedWriter bw = new BufferedWriter(new FileWriter(outpath,true));

            bw.write(dependices);
            bw.newLine();

            bw.flush();
            bw.close();
            System.out.println(bundleName+"已完成");

        }
    }







	

    public static String getDependices(String key, String ver) {
        //建立連接網址
        String url ="https://mvnrepository.com/search?q="+key;

        //定義變量前端頁面對象document
        Document doc = null;
        try {
            //訪問中央倉庫
            Connection header = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
           //設置超時時間8秒
            header.timeout(8000);
            //使用get請求獲取前端頁面document
            doc = header.get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //獲取body,進行解析頁面
        Element elem = doc.body();
        Node node = elem.childNodes().get(1).childNodes().get(2).childNodes().get(2).childNodes().get(0);
        Attributes attributes = node.attributes();
        //得到最終數據
        String href = attributes.get("href");
        String[] jarinfo = href.split("/");

        String result = "<dependency>\r\n" +
                "    <groupId>"+jarinfo[2]+"</groupId>\r\n" +
                "    <artifactId>"+key+"</artifactId>\r\n" +
                "    <version>"+ver+"</version>\r\n" +
                "</dependency>";
        return result;
    }
}




 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章