Skip to content

Instantly share code, notes, and snippets.

@vshank77
Created October 26, 2015 18:26
Show Gist options
  • Save vshank77/112dcc724e8d50a9474c to your computer and use it in GitHub Desktop.
Save vshank77/112dcc724e8d50a9474c to your computer and use it in GitHub Desktop.
Importing Maven License into Excel
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Sets;
import com.google.common.io.Resources;
import org.testng.annotations.Test;
import java.util.List;
import java.util.Objects;
import java.util.TreeSet;
import static com.google.common.base.Charsets.UTF_8;
public class LicenseReader {
@Test
public void readLine() throws Exception {
List<String> lines = Resources.readLines(LicenseReader.class.getClassLoader()
.getResource("THIRD-PARTY.txt"), UTF_8);
TreeSet<String> uniques = Sets.newTreeSet();
TreeSet<Item> result = Sets.newTreeSet();
for (String line : lines) {
Item item = new Item();
getArtifact(line, item);
getLicenseAndName(line, item);
result.add(item);
uniques.add(item.primaryLicense);
uniques.add(item.secondaryLicense);
}
for (Item item : result) System.out.println(item);
System.out.println("---------");
for (String unique : uniques) System.out.println(unique);
}
private void getLicenseAndName(String line, Item item) {
int firstBracStart = line.indexOf("(");
int firstBracClose = line.indexOf(")");
String primary = line.substring(firstBracStart + 1, firstBracClose);
item.primaryLicense = primary;
int secBracStart = line.indexOf("(", firstBracClose);
int lastBracStart = line.lastIndexOf("(");
int nameStart;
if (secBracStart != lastBracStart) {
int secBracClose = line.indexOf(")", secBracStart);
String secondary = line.substring(secBracStart + 1, secBracClose);
item.secondaryLicense = secondary;
nameStart = secBracClose + 1;
} else {
nameStart = firstBracClose + 1;
}
String name = line.substring(nameStart + 1, lastBracStart - 1);
item.name = name;
}
private void getArtifact(String line, Item item) {
int start = line.lastIndexOf("(");
int end = line.lastIndexOf(")");
String all = line.substring(start + 1, end);
int dash = all.indexOf("- http");
dash = (dash < 0) ? all.lastIndexOf("-") : dash;
String library = all.substring(0, dash - 1);
String website = all.substring(dash + 2);
String[] parts = library.split(":");
item.groupId = parts[0];
item.artifactId = parts[1];
item.version = parts[2];
item.website = website;
}
private static class Item implements Comparable<Item> {
String groupId;
String artifactId;
String version;
String website;
String name;
String primaryLicense;
String secondaryLicense = "";
@Override
public int compareTo(Item o) {
return ComparisonChain.start().compare(groupId, o.groupId).compare(artifactId, o.artifactId)
.compare(version, o.version).result();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
return Objects.equals(groupId, item.groupId) &&
Objects.equals(artifactId, item.artifactId) &&
Objects.equals(version, item.version);
}
@Override
public int hashCode() {
return Objects.hash(groupId, artifactId, version);
}
@Override
public String toString() {
return "Java\t" + name + '\t' + groupId + '\t' + artifactId + '\t' + version + '\t' +
website + '\t' + primaryLicense + '\t' + secondaryLicense;
}
}
}
mvn license:aggregate-add-third-party
mvn license:add-third-party
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment