如何检查项目中用到的License
2023-09-27 本文已影响0人
我叫王也道长
检查项目中用到的License
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
执行命令生成依赖报告
./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<String, String> 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<String, String> 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<String> 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();
}
}
}
}
}