Author: tchemit Date: 2010-11-16 11:33:37 +0100 (Tue, 16 Nov 2010) New Revision: 1876 Url: http://nuiton.org/repositories/revision/maven-license-plugin/1876 Log: Anomalie #776: THIRD-PARTY missing file not re-generated as needed Evolution #1047: Migrate in THIRD-PARTY missing file to specify the type of depedencies Modified: 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/AddThirdPartyMojo.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-11-11 22:52:27 UTC (rev 1875) +++ trunk/src/main/java/org/nuiton/license/plugin/AddThirdPartyMojo.java 2010-11-16 10:33:37 UTC (rev 1876) @@ -41,6 +41,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.SortedSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -176,17 +177,67 @@ getLog().info("found " + unsafeMappings.size() + " unsafe mappings"); } + // compute if missing file should be (re)-generate + boolean generateMissingfile = computeDoGenerateMissingFile( + unsafeMappings, + unsafeDependencies + ); + + setDoGenerateMissing(generateMissingfile); + + if (generateMissingfile && isVerbose()) { + StringBuilder sb = new StringBuilder(); + sb.append("Will use from missing file "); + sb.append(unsafeMappings.size()); + sb.append(" dependencies :"); + for (Map.Entry<Object, Object> entry : unsafeMappings.entrySet()) { + String id = (String) entry.getKey(); + String license = (String) entry.getValue(); + sb.append("\n - ").append(id).append(" - ").append(license); + } + getLog().info(sb.toString()); + } else { + if (isUseMissingFile() && !unsafeMappings.isEmpty()) { + getLog().info("Missing file " + getMissingFile() + " is up-to-date."); + } + } + return unsafeMappings; + } + + /** + * @param unsafeMappings the unsafe mapping coming from the missing file + * @param unsafeDependencies the unsafe dependencies from the project + * @return {@code true} if missing ifle should be (re-)generated, {@code false} otherwise + * @throws IOException if any IO problem + * @since 3.0 + */ + protected boolean computeDoGenerateMissingFile(SortedProperties unsafeMappings, + SortedSet<MavenProject> unsafeDependencies) throws IOException { + + if (!isUseMissingFile()) { + + // never use the missing file + return false; + } + if (isForce()) { - // use missingFile and found some unsafe dependencies - setDoGenerateMissing(isUseMissingFile() && !CollectionUtils.isEmpty(unsafeMappings.keySet())); + // the mapping fro missing file is not empty, regenerate it + return !CollectionUtils.isEmpty(unsafeMappings.keySet()); } else { - // use missingFile and still have some unsafe dependencies - setDoGenerateMissing(isUseMissingFile() && !CollectionUtils.isEmpty(unsafeDependencies)); + if (!CollectionUtils.isEmpty(unsafeDependencies)) { + + // there is some unsafe dependencies from the project, must + // regenerate missing file + return true; + } + + // check if the missing file has changed + SortedProperties oldUnsafeMappings = new SortedProperties(getEncoding()); + oldUnsafeMappings.load(getMissingFile()); + return !unsafeMappings.equals(oldUnsafeMappings); } - - return unsafeMappings; } @Override Modified: trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java =================================================================== --- trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java 2010-11-11 22:52:27 UTC (rev 1875) +++ trunk/src/main/java/org/nuiton/license/plugin/LicenseMap.java 2010-11-16 10:33:37 UTC (rev 1876) @@ -46,6 +46,7 @@ import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; + /** * Map of artifacts (stub in mavenproject) group by their license. * @@ -151,11 +152,65 @@ unsafeMappings.load(missingFile); } + // get from the missing file, all unknown dependencies + List<String> unknownDependenciesId = new ArrayList<String>(); + + // migrate unsafe mapping (before version 3.0 we do not have the type of + // dependency in the missing file, now we must deal with it, so check it + + + List<String> migrateId = new ArrayList<String>(); + SortedMap<String, MavenProject> artifactCache = + AbstractAddThirdPartyMojo.getArtifactCache(); + for (Object o : unsafeMappings.keySet()) { + String id = (String) o; + MavenProject project = artifactCache.get(id); + if (project == null) { + // try with the --jar type + project = artifactCache.get(id + "--jar"); + if (project == null) { + + // now we are sure this is a unknown dependency + unknownDependenciesId.add(id); + + } else { + + // this dependency must be migrated + migrateId.add(id); + } + } + } + + if (!unknownDependenciesId.isEmpty()) { + + // there is some unknown dependencies in the missing file, remove them + for (String id : unknownDependenciesId) { + getLog().warn("dependency [" + id + "] does not exists in project, remove it from the missing file."); + unsafeMappings.remove(id); + } + + unknownDependenciesId.clear(); + } + + if (!migrateId.isEmpty()) { + + // there is some dependencies to migrate in the missing file + for (String id : migrateId) { + String newId = id + "--jar"; + getLog().info("Migrate " + id + " to " + newId + " in the missing file."); + Object value = unsafeMappings.get(id); + unsafeMappings.remove(id); + unsafeMappings.put(newId, value); + } + + migrateId.clear(); + } + // push back loaded dependencies for (Object o : unsafeMappings.keySet()) { String id = (String) o; - MavenProject project = AbstractAddThirdPartyMojo.getArtifactCache().get(id); + MavenProject project = artifactCache.get(id); if (project == null) { getLog().warn("dependency [" + id + "] does not exists in project."); continue;