You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2017/10/06 10:00:52 UTC

svn commit: r1811313 - in /sling/trunk/tooling/maven/maven-sling-plugin: ./ src/main/java/org/apache/sling/maven/bundlesupport/ src/main/resources/META-INF/m2e/ src/test/java/org/apache/sling/maven/bundlesupport/

Author: sseifert
Date: Fri Oct  6 10:00:52 2017
New Revision: 1811313

URL: http://svn.apache.org/viewvc?rev=1811313&view=rev
Log:
SLING-7181 Maven Sling Plugin: Add Incremental Build Support for Validation Mojo

Added:
    sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/
    sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml   (with props)
    sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java   (with props)
Modified:
    sling/trunk/tooling/maven/maven-sling-plugin/pom.xml
    sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java

Modified: sling/trunk/tooling/maven/maven-sling-plugin/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-sling-plugin/pom.xml?rev=1811313&r1=1811312&r2=1811313&view=diff
==============================================================================
--- sling/trunk/tooling/maven/maven-sling-plugin/pom.xml (original)
+++ sling/trunk/tooling/maven/maven-sling-plugin/pom.xml Fri Oct  6 10:00:52 2017
@@ -213,6 +213,12 @@
             <version>3.4</version>
             <scope>provided</scope>
         </dependency>        
+        <dependency>
+            <groupId>org.sonatype.plexus</groupId>
+            <artifactId>plexus-build-api</artifactId>
+            <version>0.0.7</version>
+            <scope>compile</scope>
+        </dependency>
 
         <dependency>
             <groupId>junit</groupId>

Modified: sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java?rev=1811313&r1=1811312&r2=1811313&view=diff
==============================================================================
--- sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java (original)
+++ sling/trunk/tooling/maven/maven-sling-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java Fri Oct  6 10:00:52 2017
@@ -21,20 +21,29 @@ package org.apache.sling.maven.bundlesup
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import javax.json.JsonException;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.CharEncoding;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.Scanner;
+import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
  * The <code>validate</code> goal checks the JSON code of a bundle.
@@ -42,6 +51,10 @@ import org.codehaus.plexus.util.Director
 @Mojo(name = "validate", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
 public class ValidationMojo extends AbstractMojo {
 
+    private static final Pattern LINE_NUMBER_PATTERN = Pattern.compile("lineNumber=(\\d+),");
+    private static final Pattern COLUMN_NUMBER_PATTERN = Pattern.compile("columnNumber=(\\d+),");
+    private static final Pattern MESSAGE_CLEANUP_PATTERN = Pattern.compile("^(.*) on \\[lineNumber=\\d+, columnNumber=\\d+, streamOffset=\\d+\\](.*)$", Pattern.DOTALL);
+    
     /**
      * The Maven project.
      */
@@ -67,24 +80,32 @@ public class ValidationMojo extends Abst
     @Parameter(property = "sling.validation.jsonQuoteTick", defaultValue = "false", required = false)
     private boolean jsonQuoteTick;
 
+    @Component
+    private BuildContext buildContext;
+    
     /**
      * @see org.apache.maven.plugin.AbstractMojo#execute()
      */
-    public void execute()
-    throws MojoExecutionException {
+    public void execute() throws MojoExecutionException, MojoFailureException {
         if ( this.skip ) {
             getLog().info("Validation is skipped.");
             return;
         }
+        
         final Iterator<Resource> rsrcIterator = this.project.getResources().iterator();
         while ( rsrcIterator.hasNext() ) {
             final Resource rsrc = rsrcIterator.next();
 
             final File directory = new File(rsrc.getDirectory());
             if ( directory.exists() ) {
+
+                if (!buildContext.hasDelta(directory)) {
+                    getLog().debug("No files found to validate, skipping.");
+                    return;
+                }
+                
                 getLog().debug("Scanning " + rsrc.getDirectory());
-                final DirectoryScanner scanner = new DirectoryScanner();
-                scanner.setBasedir( directory );
+                final Scanner scanner = buildContext.newScanner(directory);
 
                 if ( rsrc.getExcludes() != null && rsrc.getExcludes().size() > 0 ) {
                     scanner.setExcludes( (String[]) rsrc.getExcludes().toArray(new String[rsrc.getExcludes().size()] ) );
@@ -97,39 +118,95 @@ public class ValidationMojo extends Abst
                 scanner.scan();
 
                 final String[] files = scanner.getIncludedFiles();
+                int countProcessed = 0;
+                List<Exception> failures = new ArrayList<>();
                 if ( files != null ) {
                     for(int m=0; m<files.length; m++) {
-                        this.validate(directory, files[m]);
+                        final File file = new File(directory, files[m]);
+                        buildContext.removeMessages(file);
+                        try {
+                            this.validate(file);
+                        }
+                        catch (Exception ex) {
+                            failures.add(ex);
+                            buildContext.addMessage(file,
+                                    parseLineNumber(ex.getMessage()),
+                                    parseColumnNumber(ex.getMessage()), 
+                                    cleanupMessage(ex.getMessage()),
+                                    BuildContext.SEVERITY_ERROR,
+                                    ex.getCause());
+                        }
+                        countProcessed++;
+                    }
+                }
+                
+                if (!failures.isEmpty()) {
+                    if (!buildContext.isIncremental()) {
+                        throw new MojoFailureException("Validated " + countProcessed + " file(s), found " + failures.size() + " failures.");
                     }
                 }
+                else {
+                    getLog().info("Validated " + countProcessed + " file(s).");
+                }
             }
         }
     }
 
-    private void validate(final File directory, final String fileName)
-    throws MojoExecutionException {
-        getLog().debug("Validating " + fileName);
-        final File file = new File(directory, fileName);
+    private void validate(final File file) throws MojoExecutionException {
+        getLog().debug("Validating " + file.getPath());
         if ( file.isFile() ) {
-            if ( fileName.endsWith(".json") && !this.skipJson ) {
-                getLog().debug("Validation JSON file " + fileName);
+            if ( file.getName().endsWith(".json") && !this.skipJson ) {
+                getLog().debug("Validation JSON file " + file.getPath());
                 FileInputStream fis = null;
                 String json = null;
                 try {
                     fis = new FileInputStream(file);
                     json = IOUtils.toString(fis, CharEncoding.UTF_8);
-                } catch (IOException e) {
-                    throw new MojoExecutionException("An Error occured while validating the file '"+fileName+"'", e);
+                } catch (IOException ex) {
+                    throw new MojoExecutionException(ex.getMessage(), ex);
                 } finally {
                     IOUtils.closeQuietly(fis);
                 }
                 // validate JSON
                 try {
                     JsonSupport.validateJsonStructure(json, jsonQuoteTick);
-                } catch (JsonException e) {
-                    throw new MojoExecutionException("An Error occured while validating the file '"+fileName+"'", e);
+                } catch (JsonException ex) {
+                    throw new MojoExecutionException("Invalid JSON: " + ex.getMessage());
                 }
             }
         }
     }
+    
+    static int parseLineNumber(String message) {
+        return parseNumber(message, LINE_NUMBER_PATTERN);
+    }
+    
+    static int parseColumnNumber(String message) {
+        return parseNumber(message, COLUMN_NUMBER_PATTERN);
+    }
+
+    static int parseNumber(String message, Pattern pattern) {
+        Matcher matcher = pattern.matcher(message);
+        if (matcher.find()) {
+            return NumberUtils.toInt(matcher.group(1));
+        }
+        else {
+            return 0;
+        }
+    }
+    
+    static String cleanupMessage(String message) {
+        String result;
+        Matcher matcher = MESSAGE_CLEANUP_PATTERN.matcher(message);
+        if (matcher.matches()) {
+            result = matcher.group(1) + matcher.group(2);
+        }
+        else {
+            result = message;
+        }
+        result = StringUtils.replace(result, "\n", "\\n");
+        result = StringUtils.replace(result, "\r", "");
+        return result;
+    }
+
 }

Added: sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml?rev=1811313&view=auto
==============================================================================
--- sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml (added)
+++ sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml Fri Oct  6 10:00:52 2017
@@ -0,0 +1,35 @@
+<!-- 
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+ 
+    http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License. 
+-->
+<lifecycleMappingMetadata>
+  <pluginExecutions>
+    <pluginExecution>
+      <pluginExecutionFilter>
+        <goals>
+          <goal>validate</goal>
+        </goals>
+      </pluginExecutionFilter>
+      <action>
+        <execute>
+          <runOnIncremental>true</runOnIncremental>
+          <runOnConfiguration>false</runOnConfiguration>
+        </execute>
+      </action>
+    </pluginExecution>
+  </pluginExecutions>
+</lifecycleMappingMetadata>

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Oct  6 10:00:52 2017
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java?rev=1811313&view=auto
==============================================================================
--- sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java (added)
+++ sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java Fri Oct  6 10:00:52 2017
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.maven.bundlesupport;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class ValidationMojoTest {
+    
+    private static String MESSAGE = "JSON validation failed: Unexpected character '\n' (Codepoint: 10) "
+            + "on [lineNumber=47, columnNumber=1434, streamOffset=2866]. "
+            + "Reason is [[End of file hit too early]]";
+
+    @Test
+    public void testParseLineNumber() {
+        assertEquals(47, ValidationMojo.parseLineNumber(MESSAGE));
+    }
+
+    @Test
+    public void testParseColumnNumber() throws Exception {
+        assertEquals(1434, ValidationMojo.parseColumnNumber(MESSAGE));
+    }
+
+    @Test
+    public void testCleanupMessage() throws Exception {
+        assertEquals("JSON validation failed: Unexpected character '\\n' (Codepoint: 10). Reason is [[End of file hit too early]]",
+                ValidationMojo.cleanupMessage(MESSAGE));
+    }
+
+}

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Fri Oct  6 10:00:52 2017
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/tooling/maven/maven-sling-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain