如何检查项目中用到的License

检查项目中用到的License


    
        org.jsoup
        jsoup
        1.11.3
    
    
        org.apache.commons
        commons-lang3
        3.12.0
    

执行命令生成依赖报告
./mvnw project-info-reports:dependencies

直接上代码

package jsbxyyx;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

public class Tree {

    // ./mvnw project-info-reports:dependencies
    static Map map = new TreeMap<>();
    static AtomicInteger counter = new AtomicInteger();

    public static void main(String[] args) throws Exception {
        String base = System.getProperty("user.dir");
        File file = new File(base);
        recursion(file);
        System.out.println("counter:" + counter.get());
        String outFile = "l.txt";
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        for (Map.Entry entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            String[] ss = StringUtils.split(key, "$");
            out.write((ss[0] + " | " + value + " | " + ss[1] + "\n").getBytes(StandardCharsets.UTF_8));
        }
        Files.write(new File(base + "/" + outFile).toPath(), out.toByteArray());
    }

    static void recursion(File f) throws Exception {
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    recursion(file);
                }
            }
        } else if (f.getName().equals("dependencies.html")) {
            byte[] bytes = Files.readAllBytes(f.toPath());
            Elements table_el = Jsoup.parse(new String(bytes, StandardCharsets.UTF_8)).getElementsByTag("table");
            for (Element table : table_el) {
                Elements ths = table.getElementsByTag("th");
                boolean has_license = false;
                int index_license = 0;
                for (Element th : ths) {
                    String text = th.text().trim();
                    if (text.equalsIgnoreCase("Licenses")) {
                        has_license = true;
                        break;
                    }
                    index_license++;
                }
                if (has_license) {
                    Elements trs = table.getElementsByTag("tr");
                    System.out.println("file:" + f.getAbsolutePath());
                    for (int i = 1; i < trs.size(); i++) {
                        Element tr = trs.get(i);
                        String groupId = tr.getElementsByTag("td").get(0).text().trim();
                        String artifactId = tr.getElementsByTag("td").get(1).text().trim();
                        String version = tr.getElementsByTag("td").get(2).text().trim();
                        String type = tr.getElementsByTag("td").get(index_license - 1).text().trim();
                        Element td_license = tr.getElementsByTag("td").get(index_license);
                        Elements td_license_a = td_license.getElementsByTag("a");
                        List licenseList = td_license_a.eachText();
                        String licenses = td_license_a.isEmpty() ?
                                td_license.text().trim() :
                                StringUtils.joinWith(" # ", licenseList.toArray(new String[0]));
                        System.out.println(groupId + ":" + artifactId + " | " + licenses + " | " + version);
                        map.put(groupId + ":" + artifactId + "$" + version, licenses);
                    }
                    counter.incrementAndGet();
                }
            }
        }
    }

}

你可能感兴趣的:(java,java)