You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2016/07/19 12:13:53 UTC

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

Author: rombert
Date: Tue Jul 19 12:13:53 2016
New Revision: 1753372

URL: http://svn.apache.org/viewvc?rev=1753372&view=rev
Log:
SLING-5863 - Make the sightly-maven-plugin m2e compatible

Use the plexus-build-api to achieve compatibility with m2e/Eclipse.

Added:
    sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/
    sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/
    sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/m2e/
    sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
Modified:
    sling/trunk/tooling/maven/sightly-maven-plugin/pom.xml
    sling/trunk/tooling/maven/sightly-maven-plugin/src/main/java/org/apache/sling/maven/sightly/ValidateMojo.java
    sling/trunk/tooling/maven/sightly-maven-plugin/src/test/java/org/apache/sling/maven/sightly/ValidateMojoTest.java

Modified: sling/trunk/tooling/maven/sightly-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/sightly-maven-plugin/pom.xml?rev=1753372&r1=1753371&r2=1753372&view=diff
==============================================================================
--- sling/trunk/tooling/maven/sightly-maven-plugin/pom.xml (original)
+++ sling/trunk/tooling/maven/sightly-maven-plugin/pom.xml Tue Jul 19 12:13:53 2016
@@ -85,6 +85,12 @@
             <artifactId>plexus-utils</artifactId>
             <version>3.0.24</version>
         </dependency>
+        <dependency>
+            <groupId>org.sonatype.plexus</groupId>
+            <artifactId>plexus-build-api</artifactId>
+            <version>0.0.7</version>
+        </dependency>
+        
 
         <!-- Testing Dependencies -->
         <dependency>

Modified: sling/trunk/tooling/maven/sightly-maven-plugin/src/main/java/org/apache/sling/maven/sightly/ValidateMojo.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/sightly-maven-plugin/src/main/java/org/apache/sling/maven/sightly/ValidateMojo.java?rev=1753372&r1=1753371&r2=1753372&view=diff
==============================================================================
--- sling/trunk/tooling/maven/sightly-maven-plugin/src/main/java/org/apache/sling/maven/sightly/ValidateMojo.java (original)
+++ sling/trunk/tooling/maven/sightly-maven-plugin/src/main/java/org/apache/sling/maven/sightly/ValidateMojo.java Tue Jul 19 12:13:53 2016
@@ -21,6 +21,7 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.Reader;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -29,7 +30,7 @@ import java.util.Map;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugin.logging.Log;
+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;
@@ -38,8 +39,9 @@ import org.apache.sling.scripting.sightl
 import org.apache.sling.scripting.sightly.compiler.CompilationUnit;
 import org.apache.sling.scripting.sightly.compiler.CompilerMessage;
 import org.apache.sling.scripting.sightly.compiler.SightlyCompiler;
-import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.Scanner;
 import org.codehaus.plexus.util.StringUtils;
+import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
  * This goal validates Sightly scripts syntax.
@@ -53,6 +55,9 @@ public class ValidateMojo extends Abstra
 
     private static final String DEFAULT_INCLUDES = "**/*.html";
     private static final String DEFAULT_EXCLUDES = "";
+    
+    @Component
+    private BuildContext buildContext;
 
     @Parameter(defaultValue = "${project}", readonly = true, required = true)
     protected MavenProject project;
@@ -92,6 +97,9 @@ public class ValidateMojo extends Abstra
     private int sourceDirectoryLength = 0;
 
     public void execute() throws MojoExecutionException, MojoFailureException {
+        
+        long start = System.currentTimeMillis();
+        
         if (!sourceDirectory.isAbsolute()) {
             sourceDirectory = new File(project.getBasedir(), sourceDirectory.getPath());
         }
@@ -103,34 +111,55 @@ public class ValidateMojo extends Abstra
             throw new MojoExecutionException(
                     String.format("Configured sourceDirectory={%s} is not a directory.", sourceDirectory.getAbsolutePath()));
         }
+        
+        if ( !buildContext.hasDelta(sourceDirectory )) {
+            getLog().info("No files found to validate, skipping");
+            return;
+        }
+        
         sourceDirectoryLength = sourceDirectory.getAbsolutePath().length();
         processedIncludes = processIncludes();
         processedExcludes = processExcludes();
         try {
             SightlyCompiler compiler = new SightlyCompiler();
-            processedFiles = FileUtils.getFiles(sourceDirectory, processedIncludes, processedExcludes);
-            Map<String, CompilationResult> compilationResults = new HashMap<>();
+            
+            Scanner scanner = buildContext.newScanner(sourceDirectory);
+            scanner.setExcludes(new String[] { processedExcludes } );
+            scanner.setIncludes(new String[] { processedIncludes } );
+            scanner.scan();
+            
+            String[] includedFiles = scanner.getIncludedFiles();
+            
+            processedFiles = new ArrayList<>(includedFiles.length);
+            for ( String includedFile : includedFiles ) {
+                processedFiles.add(new File(sourceDirectory, includedFile));
+            }
+            Map<File, CompilationResult> compilationResults = new HashMap<>();
             for (File script : processedFiles) {
-                compilationResults.put(script.getAbsolutePath(), compiler.compile(getCompilationUnit(script)));
+                compilationResults.put(script, compiler.compile(getCompilationUnit(script)));
             }
-            Log log = getLog();
-            for (Map.Entry<String, CompilationResult> entry : compilationResults.entrySet()) {
-                String script = entry.getKey();
+            for (Map.Entry<File, CompilationResult> entry : compilationResults.entrySet()) {
+                File script = entry.getKey();
                 CompilationResult result = entry.getValue();
+                buildContext.removeMessages(script);
+                
                 if (result.getWarnings().size() > 0) {
                     for (CompilerMessage message : result.getWarnings()) {
-                        log.warn(String.format("%s:[%d,%d] %s", script, message.getLine(), message.getColumn(), message.getMessage()));
+                        buildContext.addMessage(script, message.getLine(), message.getColumn(), message.getMessage(), BuildContext.SEVERITY_WARNING, null);
                     }
                     hasWarnings = true;
                 }
                 if (result.getErrors().size() > 0) {
                     for (CompilerMessage message : result.getErrors()) {
                         String messageString = message.getMessage().replaceAll(System.lineSeparator(), "");
-                        log.error(String.format("%s:[%d,%d] %s", script, message.getLine(), message.getColumn(), messageString));
+                        buildContext.addMessage(script, message.getLine(), message.getColumn(), messageString, BuildContext.SEVERITY_ERROR, null);
                     }
                     hasErrors = true;
                 }
             }
+            
+            getLog().info("Processed " + processedFiles.size() + " files in " + ( System.currentTimeMillis() - start ) + " milliseconds");
+            
             if (hasWarnings && failOnWarnings) {
                 throw new MojoFailureException("Compilation warnings were configured to fail the build.");
             }
@@ -209,4 +238,10 @@ public class ValidateMojo extends Abstra
             }
         };
     }
+
+    // visible for testing only
+    void setBuildContext(BuildContext buildContext) {
+        
+        this.buildContext = buildContext;
+    }
 }

Added: sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml?rev=1753372&view=auto
==============================================================================
--- sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml (added)
+++ sling/trunk/tooling/maven/sightly-maven-plugin/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml Tue Jul 19 12:13:53 2016
@@ -0,0 +1,33 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ 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>
\ No newline at end of file

Modified: sling/trunk/tooling/maven/sightly-maven-plugin/src/test/java/org/apache/sling/maven/sightly/ValidateMojoTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/maven/sightly-maven-plugin/src/test/java/org/apache/sling/maven/sightly/ValidateMojoTest.java?rev=1753372&r1=1753371&r2=1753372&view=diff
==============================================================================
--- sling/trunk/tooling/maven/sightly-maven-plugin/src/test/java/org/apache/sling/maven/sightly/ValidateMojoTest.java (original)
+++ sling/trunk/tooling/maven/sightly-maven-plugin/src/test/java/org/apache/sling/maven/sightly/ValidateMojoTest.java Tue Jul 19 12:13:53 2016
@@ -25,6 +25,7 @@ import org.apache.maven.plugin.testing.S
 import org.apache.maven.project.MavenProject;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -99,12 +100,17 @@ public class ValidateMojoTest {
     }
 
     private ValidateMojo getMojo(File baseDir, String pomFile) throws Exception {
+        SilentLog log = new SilentLog();
+        DefaultBuildContext buildContext = new DefaultBuildContext();
+        
         File pom = new File(baseDir, pomFile);
         ValidateMojo validateMojo = new ValidateMojo();
         mojoRule.configureMojo(validateMojo, mojoRule.extractPluginConfiguration("sightly-maven-plugin", pom));
         MavenProject mavenProject = new ProjectStub(pom);
         mojoRule.setVariableValueToObject(validateMojo, "project", mavenProject);
-        validateMojo.setLog(new SilentLog());
+        validateMojo.setLog(log);
+        buildContext.enableLogging(log);
+        validateMojo.setBuildContext(buildContext);
         return validateMojo;
     }