[NO ISSUE][LIC] Add ability to fail and/or generate file on license warning

Change-Id: I236832b6bcf362b0faebe9baeff154c01e53495b
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2598
Reviewed-by: Michael Blow <mb...@apache.org>
Tested-by: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenk...@fulliautomatix.ics.uci.edu>
Reviewed-by: Murtadha Hubail <mhub...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/asterixdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/asterixdb/commit/7de451f8
Tree: http://git-wip-us.apache.org/repos/asf/asterixdb/tree/7de451f8
Diff: http://git-wip-us.apache.org/repos/asf/asterixdb/diff/7de451f8

Branch: refs/heads/master
Commit: 7de451f889af70ab1c2506e0b4e65f221affed35
Parents: d8959ee
Author: Michael Blow <mb...@apache.org>
Authored: Sat Apr 14 19:43:48 2018 -0400
Committer: Michael Blow <mb...@apache.org>
Committed: Sat Apr 14 21:15:35 2018 -0700

----------------------------------------------------------------------
 .../maven/license/DownloadLicensesMojo.java     |   2 +-
 .../hyracks/maven/license/GenerateFileMojo.java |  16 +--
 .../hyracks/maven/license/LicenseMojo.java      | 104 ++++++++++++++++++-
 3 files changed, 113 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/7de451f8/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/DownloadLicensesMojo.java
----------------------------------------------------------------------
diff --git 
a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/DownloadLicensesMojo.java
 
b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/DownloadLicensesMojo.java
index 1b2961f..293c08c 100644
--- 
a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/DownloadLicensesMojo.java
+++ 
b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/DownloadLicensesMojo.java
@@ -63,7 +63,7 @@ public class DownloadLicensesMojo extends LicenseMojo {
                 String fileName = entry.getLicense().getContentFile(false);
                 doDownload(timeoutMillis, i, url, fileName);
             });
-        } catch (IOException | ProjectBuildingException e) {
+        } catch (ProjectBuildingException e) {
             throw new MojoExecutionException("Unexpected exception: " + e, e);
         }
     }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/7de451f8/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/GenerateFileMojo.java
----------------------------------------------------------------------
diff --git 
a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/GenerateFileMojo.java
 
b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/GenerateFileMojo.java
index 22646c5..f61dadf 100644
--- 
a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/GenerateFileMojo.java
+++ 
b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/GenerateFileMojo.java
@@ -121,6 +121,10 @@ public class GenerateFileMojo extends LicenseMojo {
             persistLicenseMap();
             buildNoticeProjectMap();
             generateFiles();
+            if (seenWarning && failOnWarning) {
+                throw new MojoFailureException(
+                        "'failOnWarning' enabled and warning(s) (or error(s)) 
occurred during execution; see output");
+            }
         } catch (IOException | TemplateException | ProjectBuildingException e) 
{
             throw new MojoExecutionException("Unexpected exception: " + e, e);
         }
@@ -327,8 +331,9 @@ public class GenerateFileMojo extends LicenseMojo {
                 UnaryOperator.identity());
     }
 
-    private void resolveArtifactFiles(final String name, ProjectFlag 
ignoreFlag, ProjectFlag alternateFilenameFlag,
-            Predicate<JarEntry> filter, BiConsumer<Project, String> consumer, 
UnaryOperator<String> contentTransformer)
+    private void resolveArtifactFiles(final String name, final ProjectFlag 
ignoreFlag,
+            final ProjectFlag alternateFilenameFlag, final Predicate<JarEntry> 
filter,
+            final BiConsumer<Project, String> consumer, final 
UnaryOperator<String> contentTransformer)
             throws MojoExecutionException, IOException {
         for (Project p : getProjects()) {
             File artifactFile = new File(p.getArtifactPath());
@@ -339,11 +344,10 @@ public class GenerateFileMojo extends LicenseMojo {
                 continue;
             }
             String alternateFilename = (String) getProjectFlag(p.gav(), 
alternateFilenameFlag);
-            if (alternateFilename != null) {
-                filter = entry -> entry.getName().equals(alternateFilename);
-            }
+            Predicate<JarEntry> finalFilter =
+                    alternateFilename != null ? entry -> 
entry.getName().equals(alternateFilename) : filter;
             try (JarFile jarFile = new JarFile(artifactFile)) {
-                SortedMap<String, JarEntry> matches = 
gatherMatchingEntries(jarFile, filter);
+                SortedMap<String, JarEntry> matches = 
gatherMatchingEntries(jarFile, finalFilter);
                 if (matches.isEmpty()) {
                     warnUnlessFlag(p, ignoreFlag, "No " + name + " file found 
for " + p.gav());
                 } else {

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/7de451f8/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/LicenseMojo.java
----------------------------------------------------------------------
diff --git 
a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/LicenseMojo.java
 
b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/LicenseMojo.java
index 4466d20..55987da 100644
--- 
a/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/LicenseMojo.java
+++ 
b/hyracks-fullstack/hyracks/hyracks-maven-plugins/license-automation-plugin/src/main/java/org/apache/hyracks/maven/license/LicenseMojo.java
@@ -22,6 +22,7 @@ import static 
org.apache.hyracks.maven.license.LicenseUtil.toGav;
 import static 
org.apache.hyracks.maven.license.ProjectFlag.IGNORE_LICENSE_OVERRIDE;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -37,6 +38,7 @@ import java.util.TreeSet;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.mutable.MutableBoolean;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
@@ -50,6 +52,7 @@ import org.apache.maven.model.License;
 import org.apache.maven.model.Model;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
@@ -59,7 +62,6 @@ import 
org.apache.maven.project.inheritance.ModelInheritanceAssembler;
 
 public abstract class LicenseMojo extends AbstractMojo {
 
-    private static final String VERIFIED_VERSIONS_PROP = 
"license-automation-plugin.verifiedVersions";
     @Parameter
     protected List<Override> overrides = new ArrayList<>();
 
@@ -105,6 +107,12 @@ public abstract class LicenseMojo extends AbstractMojo {
     @Parameter(required = true)
     protected File licenseDirectory;
 
+    @Parameter
+    protected File warningTouchFile;
+
+    @Parameter
+    protected boolean failOnWarning;
+
     private Map<String, MavenProject> projectCache = new HashMap<>();
 
     private Map<String, Model> supplementModels;
@@ -115,17 +123,109 @@ public abstract class LicenseMojo extends AbstractMojo {
     Map<String, LicensedProjects> licenseMap = new TreeMap<>();
     private Map<Pair<String, ProjectFlag>, Object> projectFlags = new 
HashMap<>();
 
+    protected boolean seenWarning;
+
     protected Map<String, LicensedProjects> getLicenseMap() {
         return licenseMap;
     }
 
-    protected void init() throws MojoExecutionException, 
MalformedURLException, ProjectBuildingException {
+    protected void init() throws MojoExecutionException {
+        if (warningTouchFile != null) {
+            warningTouchFile.getParentFile().mkdirs();
+        }
+        interceptLogs();
         excludedScopes.add("system");
         excludePatterns = compileExcludePatterns();
         supplementModels = SupplementalModelHelper.loadSupplements(getLog(), 
models);
         buildUrlLicenseMap();
     }
 
+    private void interceptLogs() {
+        final Log originalLog = getLog();
+        setLog(new Log() {
+            public boolean isDebugEnabled() {
+                return originalLog.isDebugEnabled();
+            }
+
+            public void debug(CharSequence charSequence) {
+                originalLog.debug(charSequence);
+            }
+
+            public void debug(CharSequence charSequence, Throwable throwable) {
+                originalLog.debug(charSequence, throwable);
+            }
+
+            public void debug(Throwable throwable) {
+                originalLog.debug(throwable);
+            }
+
+            public boolean isInfoEnabled() {
+                return originalLog.isInfoEnabled();
+            }
+
+            public void info(CharSequence charSequence) {
+                originalLog.info(charSequence);
+            }
+
+            public void info(CharSequence charSequence, Throwable throwable) {
+                originalLog.info(charSequence, throwable);
+            }
+
+            public void info(Throwable throwable) {
+                originalLog.info(throwable);
+            }
+
+            public boolean isWarnEnabled() {
+                return originalLog.isWarnEnabled();
+            }
+
+            public void warn(CharSequence charSequence) {
+                seenWarning();
+                originalLog.warn(charSequence);
+            }
+
+            public void warn(CharSequence charSequence, Throwable throwable) {
+                seenWarning();
+                originalLog.warn(charSequence, throwable);
+            }
+
+            public void warn(Throwable throwable) {
+                seenWarning();
+                originalLog.warn(throwable);
+            }
+
+            public boolean isErrorEnabled() {
+                return originalLog.isErrorEnabled();
+            }
+
+            public void error(CharSequence charSequence) {
+                seenWarning();
+                originalLog.error(charSequence);
+            }
+
+            public void error(CharSequence charSequence, Throwable throwable) {
+                seenWarning();
+                originalLog.error(charSequence, throwable);
+            }
+
+            public void error(Throwable throwable) {
+                seenWarning();
+                originalLog.error(throwable);
+            }
+
+            private void seenWarning() {
+                seenWarning = true;
+                if (warningTouchFile != null) {
+                    try {
+                        FileUtils.touch(warningTouchFile);
+                    } catch (IOException e) {
+                        originalLog.error("unable to touch " + 
warningTouchFile, e);
+                    }
+                }
+            }
+        });
+    }
+
     protected void addDependenciesToLicenseMap() throws 
ProjectBuildingException {
         Map<MavenProject, List<Pair<String, String>>> dependencyLicenseMap = 
gatherDependencies();
         dependencyLicenseMap.forEach((depProject, value) -> {

Reply via email to