r1869 - trunk/src/main/java/org/nuiton/license/plugin
Author: tchemit Date: 2010-10-30 00:02:15 +0200 (Sat, 30 Oct 2010) New Revision: 1869 Url: http://nuiton.org/repositories/revision/maven-license-plugin/1869 Log: introduce ArtifactHelper + fix Anomalie #1007: THIRD-PARTY.properties files overwritten Added: trunk/src/main/java/org/nuiton/license/plugin/ArtifactHelper.java Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java Modified: trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java 2010-10-29 08:29:07 UTC (rev 1868) +++ trunk/src/main/java/org/nuiton/license/plugin/AbstractAddThirdPartyMojo.java 2010-10-29 22:02:15 UTC (rev 1869) @@ -36,8 +36,6 @@ import java.util.TreeMap; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingException; @@ -258,11 +256,13 @@ if (isGenerateBundle()) { - File bundleFile = - PluginHelper.getFile(getOutputDirectory(), getBundleThirdPartyPath()); + File bundleFile = PluginHelper.getFile(getOutputDirectory(), + getBundleThirdPartyPath() + ); if (isVerbose()) { - log.info("bundle third-party file : " + bundleFile.lastModified()); + log.info("bundle third-party file : " + + bundleFile.lastModified()); } setDoGenerateBundle(isForce() || !bundleFile.exists() || @@ -310,43 +310,12 @@ for (MavenProject dep : unsafeDependencies) { // no license found for the dependency - log.warn(" - " + getArtifactId(dep.getArtifact())); + log.warn(" - " + ArtifactHelper.getArtifactId(dep.getArtifact())); } } return unsafe; } - public static String getArtifactId(Artifact artifact) { - StringBuilder sb = new StringBuilder(); - sb.append(artifact.getGroupId()); - sb.append("--"); - sb.append(artifact.getArtifactId()); - sb.append("--"); - sb.append(artifact.getVersion()); - if (!StringUtils.isEmpty(artifact.getClassifier())) { - sb.append("--"); - sb.append(artifact.getClassifier()); - } - return sb.toString(); - } - - public static String getArtifactName(MavenProject project) { - StringBuilder sb = new StringBuilder(); - sb.append(project.getName()); - sb.append(" ("); - sb.append(project.getGroupId()); - sb.append(":"); - sb.append(project.getArtifactId()); - sb.append(":"); - sb.append(project.getVersion()); - sb.append(" - "); - String url = project.getUrl(); - sb.append(url == null ? "no url defined" : url); - sb.append(")"); - - return sb.toString(); - } - protected void writeThirdPartyFile() throws IOException { Log log = getLog(); @@ -369,7 +338,7 @@ for (MavenProject mavenProject : projects) { - String s = getArtifactName(mavenProject); + String s = ArtifactHelper.getArtifactName(mavenProject); sb.append("\n * ").append(s); } } @@ -385,7 +354,7 @@ List<String> lines = new ArrayList<String>(); for (Map.Entry<MavenProject, String[]> entry : map.entrySet()) { - String artifact = getArtifactName(entry.getKey()); + String artifact = ArtifactHelper.getArtifactName(entry.getKey()); StringBuilder buffer = new StringBuilder(); for (String license : entry.getValue()) { buffer.append(" (").append(license).append(")"); Modified: trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-10-29 08:29:07 UTC (rev 1868) +++ trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-10-29 22:02:15 UTC (rev 1869) @@ -25,17 +25,6 @@ package org.nuiton.license.plugin; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.maven.artifact.Artifact; @@ -47,6 +36,16 @@ import org.apache.maven.project.ProjectBuildingException; import org.nuiton.io.SortedProperties; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.SortedSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + /** * Le goal pour copier le fichier THIRD-PARTY.txt (contenant les licenses de * toutes les dependances du projet) dans le classpath (et le generer s'il @@ -140,18 +139,20 @@ // no information can be retrive from anywhere)... continue; } - String id = getArtifactId(artifact); + String id = ArtifactHelper.getArtifactId(artifact); + if (isVerbose()) { + getLog().info("detected artifact " + id); + } - // Check if the project should be included // If there is no specified artifacts and group to include, include all boolean isToInclude = haveNoIncludedArtifacts && haveNoIncludedGroups - || isIncludable(artifact, includedGroupPattern, includedArtifactPattern); + || isIncludable(artifact, includedGroupPattern, includedArtifactPattern); // Check if the project should be excluded boolean isToExclude = isToInclude - && haveExclusions - && isExcludable(artifact, excludedGroupPattern, excludedArtifactPattern); + && haveExclusions + && isExcludable(artifact, excludedGroupPattern, excludedArtifactPattern); if (isToInclude && !isToExclude) { MavenProject project = addArtifact(id, artifact); @@ -167,7 +168,7 @@ SortedProperties unsafeMappings = getLicenseMap().loadUnsafeMapping(getEncoding(), - getMissingFile()); + getMissingFile()); SortedSet<MavenProject> unsafeDependencies = getUnsafeDependencies(); @@ -185,15 +186,14 @@ setDoGenerateMissing(isUseMissingFile() && !CollectionUtils.isEmpty(unsafeDependencies)); } - return unsafeMappings; } @Override protected boolean checkSkip() { if (!isDoGenerate() && - !isDoGenerateBundle() && - !isDoGenerateMissing()) { + !isDoGenerateBundle() && + !isDoGenerateMissing()) { getLog().info("All files are up to date, skip goal execution."); return false; @@ -232,14 +232,13 @@ FileWriter writer = new FileWriter(file); try { StringBuilder sb = new StringBuilder(" Generated by " + getClass().getName()); - Set<String> licenses = licenseMap.keySet(); + List<String> licenses = new ArrayList<String>(licenseMap.keySet()); + licenses.remove(LicenseMap.getUnknownLicenseMessage()); if (!licenses.isEmpty()) { sb.append("\n-------------------------------------------------------------------------------"); sb.append("\n Already used licenses in project :"); for (String license : licenses) { - if (!LicenseMap.getUnknownLicenseMessage().equals(license)) { - sb.append("\n - ").append(license); - } + sb.append("\n - ").append(license); } } sb.append("\n-------------------------------------------------------------------------------"); @@ -305,7 +304,7 @@ } catch (PatternSyntaxException e) { getLog().warn("The pattern specified by expression <" - + includedGroups + "> seems to be invalid."); + + includedGroups + "> seems to be invalid."); } } @@ -323,7 +322,7 @@ } catch (PatternSyntaxException e) { getLog().warn("The pattern specified by expression <" - + includedArtifacts + "> seems to be invalid."); + + includedArtifacts + "> seems to be invalid."); } } @@ -350,7 +349,7 @@ } catch (PatternSyntaxException e) { getLog().warn("The pattern specified by expression <" - + excludedGroups + "> seems to be invalid."); + + excludedGroups + "> seems to be invalid."); } } @@ -368,7 +367,7 @@ } catch (PatternSyntaxException e) { getLog().warn("The pattern specified by expression <" - + excludedArtifacts + "> seems to be invalid."); + + excludedArtifacts + "> seems to be invalid."); } } Added: trunk/src/main/java/org/nuiton/license/plugin/ArtifactHelper.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/ArtifactHelper.java (rev 0) +++ trunk/src/main/java/org/nuiton/license/plugin/ArtifactHelper.java 2010-10-29 22:02:15 UTC (rev 1869) @@ -0,0 +1,69 @@ +package org.nuiton.license.plugin; + +import org.apache.commons.lang.StringUtils; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.project.MavenProject; + +import java.util.Comparator; + +/** + * A helper for artifacts. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.4.2 + */ +public class ArtifactHelper { + + protected static Comparator<MavenProject> projectComparator; + + public static String getArtifactId(Artifact artifact) { + StringBuilder sb = new StringBuilder(); + sb.append(artifact.getGroupId()); + sb.append("--"); + sb.append(artifact.getArtifactId()); + sb.append("--"); + sb.append(artifact.getVersion()); + String type = artifact.getType(); + if (!StringUtils.isEmpty(type) && !"pom".equals(type)) { + sb.append("--"); + sb.append(artifact.getType()); + } + if (!StringUtils.isEmpty(artifact.getClassifier())) { + sb.append("--"); + sb.append(artifact.getClassifier()); + } + return sb.toString(); + } + + public static String getArtifactName(MavenProject project) { + StringBuilder sb = new StringBuilder(); + sb.append(project.getName()); + sb.append(" ("); + sb.append(project.getGroupId()); + sb.append(":"); + sb.append(project.getArtifactId()); + sb.append(":"); + sb.append(project.getVersion()); + sb.append(" - "); + String url = project.getUrl(); + sb.append(url == null ? "no url defined" : url); + sb.append(")"); + + return sb.toString(); + } + + public static Comparator<MavenProject> getProjectComparator() { + if (projectComparator == null) { + projectComparator = new Comparator<MavenProject>() { + @Override + public int compare(MavenProject o1, MavenProject o2) { + + String id1 = getArtifactId(o1.getArtifact()); + String id2 = getArtifactId(o2.getArtifact()); + return id1.compareTo(id2); + } + }; + } + return projectComparator; + } +} Modified: trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java 2010-10-29 08:29:07 UTC (rev 1868) +++ trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java 2010-10-29 22:02:15 UTC (rev 1869) @@ -38,7 +38,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -47,10 +46,6 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - /** * Map of artifacts (stub in mavenproject) group by their license. * @@ -94,7 +89,7 @@ for (Object o : licenses) { String id; - id = AbstractAddThirdPartyMojo.getArtifactId(project.getArtifact()); + id = ArtifactHelper.getArtifactId(project.getArtifact()); if (o == null) { getLog().warn("could not acquire the license for " + id); continue; @@ -131,7 +126,7 @@ for (MavenProject dep : unsafeDependencies) { // no license found for the dependency - log.debug(" - " + AbstractAddThirdPartyMojo.getArtifactId(dep.getArtifact())); + log.debug(" - " + ArtifactHelper.getArtifactId(dep.getArtifact())); } } } @@ -139,7 +134,8 @@ return unsafeDependencies; } - protected SortedProperties loadUnsafeMapping(String encoding, File missingFile) throws IOException, ProjectBuildingException { + protected SortedProperties loadUnsafeMapping(String encoding, + File missingFile) throws IOException, ProjectBuildingException { SortedSet<MavenProject> unsafeDependencies = getUnsafeDependencies(); @@ -186,14 +182,13 @@ if (unsafeDependencies.isEmpty()) { - // no more unknown license in map remove(getUnknownLicenseMessage()); } else { // add a "with no value license" for missing dependencies for (MavenProject project : unsafeDependencies) { - String id = AbstractAddThirdPartyMojo.getArtifactId(project.getArtifact()); + String id = ArtifactHelper.getArtifactId(project.getArtifact()); unsafeMappings.setProperty(id, ""); } } @@ -201,11 +196,12 @@ } public SortedSet<MavenProject> put(String key, MavenProject value) { + // handle multiple values as a set to avoid duplicates SortedSet<MavenProject> valueList = get(key); if (valueList == null) { - valueList = new TreeSet<MavenProject>(getProjectComparator()); + valueList = new TreeSet<MavenProject>(ArtifactHelper.getProjectComparator()); } if (getLog().isDebugEnabled()) { getLog().debug("key:" + key + ",value: " + value); @@ -216,7 +212,7 @@ public SortedMap<MavenProject, String[]> toDependencyMap() { SortedMap<MavenProject, Set<String>> tmp = - new TreeMap<MavenProject, Set<String>>(getProjectComparator()); + new TreeMap<MavenProject, Set<String>>(ArtifactHelper.getProjectComparator()); for (Map.Entry<String, SortedSet<MavenProject>> entry : entrySet()) { String license = entry.getKey(); @@ -232,7 +228,7 @@ } SortedMap<MavenProject, String[]> result = - new TreeMap<MavenProject, String[]>(getProjectComparator()); + new TreeMap<MavenProject, String[]>(ArtifactHelper.getProjectComparator()); for (Map.Entry<MavenProject, Set<String>> entry : tmp.entrySet()) { List<String> value = new ArrayList<String>(entry.getValue()); Collections.sort(value); @@ -246,23 +242,6 @@ return unknownLicenseMessage; } - protected static transient Comparator<MavenProject> projectComparator; - - public static Comparator<MavenProject> getProjectComparator() { - if (projectComparator == null) { - projectComparator = new Comparator<MavenProject>() { - @Override - public int compare(MavenProject o1, MavenProject o2) { - - String id1 = AbstractAddThirdPartyMojo.getArtifactId(o1.getArtifact()); - String id2 = AbstractAddThirdPartyMojo.getArtifactId(o2.getArtifact()); - return id1.compareTo(id2); - } - }; - } - return projectComparator; - } - public void mergeLicenses(String... licenses) { if (licenses.length == 0) { return; @@ -272,7 +251,7 @@ SortedSet<MavenProject> mainSet = get(mainLicense); if (mainSet == null) { getLog().warn("No license [" + mainLicense + "] found, will create it."); - mainSet = new TreeSet<MavenProject>(getProjectComparator()); + mainSet = new TreeSet<MavenProject>(ArtifactHelper.getProjectComparator()); put(mainLicense, mainSet); } int size = licenses.length;
participants (1)
-
tchemit@users.nuiton.org