You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by st...@apache.org on 2012/09/21 14:34:06 UTC

svn commit: r1388456 - in /maven/shared/trunk: ./ maven-shared-incremental/ maven-shared-incremental/src/ maven-shared-incremental/src/main/ maven-shared-incremental/src/main/java/ maven-shared-incremental/src/main/java/org/ maven-shared-incremental/sr...

Author: struberg
Date: Fri Sep 21 12:34:05 2012
New Revision: 1388456

URL: http://svn.apache.org/viewvc?rev=1388456&view=rev
Log:
MSHARED-245 add IncremtalBuildHelper

Added:
    maven/shared/trunk/maven-shared-incremental/   (with props)
    maven/shared/trunk/maven-shared-incremental/pom.xml   (with props)
    maven/shared/trunk/maven-shared-incremental/src/
    maven/shared/trunk/maven-shared-incremental/src/main/
    maven/shared/trunk/maven-shared-incremental/src/main/java/
    maven/shared/trunk/maven-shared-incremental/src/main/java/org/
    maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/
    maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/
    maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/
    maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/
    maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java   (with props)
    maven/shared/trunk/maven-shared-incremental/src/main/resources/
    maven/shared/trunk/maven-shared-incremental/src/test/
    maven/shared/trunk/maven-shared-incremental/src/test/java/
    maven/shared/trunk/maven-shared-incremental/src/test/resources/
Modified:
    maven/shared/trunk/pom.xml

Propchange: maven/shared/trunk/maven-shared-incremental/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Sep 21 12:34:05 2012
@@ -0,0 +1,10 @@
+target
+*~
+*.log
+.classpath
+.project
+.settings
+*.ipr
+*.iws
+*.iml
+.idea

Added: maven/shared/trunk/maven-shared-incremental/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-incremental/pom.xml?rev=1388456&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-incremental/pom.xml (added)
+++ maven/shared/trunk/maven-shared-incremental/pom.xml Fri Sep 21 12:34:05 2012
@@ -0,0 +1,135 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!--
+  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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.maven.shared</groupId>
+    <artifactId>maven-shared-components</artifactId>
+    <version>18</version>
+    <relativePath>../maven-shared-components/pom.xml</relativePath>
+  </parent>
+
+  <artifactId>maven-shared-incremental</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Maven Incremental Build support utilities</name>
+  <description>
+    Various utility classes and plexus components for supporting 
+    incremental build functionality in maven plugins.
+  </description>
+
+  <scm>
+    <connection>scm:svn:http://svn.apache.org/repos/asf/maven/shared/trunk/maven-shared-incremental</connection>
+    <developerConnection>scm:svn:https://svn.apache.org/repos/asf/maven/shared/trunk/maven-shared-incremental</developerConnection>
+    <url>http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-incremental</url>
+  </scm>
+
+  <issueManagement>
+    <system>jira</system>
+    <url>https://jira.codehaus.org/browse/MSHARED/component/15650</url>
+  </issueManagement>
+
+  <properties>
+    <mavenVersion>2.2.1</mavenVersion>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-plugin-api</artifactId>
+      <version>${mavenVersion}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-core</artifactId>
+      <version>${mavenVersion}</version>
+      <exclusions>
+        <exclusion>
+          <groupId>org.apache.maven.reporting</groupId>
+          <artifactId>maven-reporting-api</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.apache.maven.wagon</groupId>
+          <artifactId>wagon-file</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.apache.maven.wagon</groupId>
+          <artifactId>wagon-http-lightweight</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.apache.maven.wagon</groupId>
+          <artifactId>wagon-ssh</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.apache.maven.wagon</groupId>
+          <artifactId>wagon-ssh-external</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>commons-cli</groupId>
+          <artifactId>commons-cli</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>classworlds</groupId>
+          <artifactId>classworlds</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.codehaus.plexus</groupId>
+          <artifactId>plexus-container-default</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>org.codehaus.plexus</groupId>
+          <artifactId>plexus-interactivity-api</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-shared-utils</artifactId>
+      <version>0.1-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-component-annotations</artifactId>
+      <version>1.5.5</version>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-component-api</artifactId>
+      <version>1.0-alpha-16</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.plexus</groupId>
+        <artifactId>plexus-component-metadata</artifactId>
+        <executions>
+          <execution>
+            <goals>
+              <goal>generate-metadata</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: maven/shared/trunk/maven-shared-incremental/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java?rev=1388456&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java (added)
+++ maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java Fri Sep 21 12:34:05 2012
@@ -0,0 +1,193 @@
+package org.apache.maven.shared.incremental;
+
+/*
+ * 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.
+ */
+
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.utils.io.DirectoryScanResult;
+import org.apache.maven.shared.utils.io.DirectoryScanner;
+import org.apache.maven.shared.utils.io.FileUtils;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Various helper methods to support incremental builds
+ *
+ */
+public class IncrementalBuildHelper
+{
+    /**
+     * the root directory to store status information about maven executions in.
+     */
+    private static final String MAVEN_STATUS_ROOT = "maven-status";
+    public static final String CREATED_FILES_LST_FILENAME = "createdFiles.lst";
+
+    /**
+     * Needed for storing the status for the incremental build support.
+     */
+    private MojoExecution mojoExecution;
+
+    /**
+     * Needed for storing the status for the incremental build support.
+     */
+    private MavenProject mavenProject;
+
+    /**
+     * Used for detecting changes between the Mojo execution.
+     * @see #getDirectoryScanner();
+     */
+    private DirectoryScanner directoryScanner;
+
+    private String[] filesBeforeAction;
+
+    public IncrementalBuildHelper( MojoExecution mojoExecution, MavenSession mavenSession )
+    {
+        this( mojoExecution, mavenSession.getCurrentProject() );
+    }
+
+
+    public IncrementalBuildHelper( MojoExecution mojoExecution, MavenProject mavenProject )
+    {
+        this.mavenProject = mavenProject;
+        this.mojoExecution = mojoExecution;
+    }
+
+    public DirectoryScanner getDirectoryScanner()
+    {
+        if ( directoryScanner == null )
+        {
+            directoryScanner = new DirectoryScanner();
+        }
+
+        return directoryScanner;
+    }
+
+    /**
+     * @return the directory for storing status information of the current mojo execution.
+     */
+    public File getMojoStatusDirectory() throws MojoExecutionException
+    {
+        if ( mojoExecution == null )
+        {
+            throw new MojoExecutionException( "MojoExecution could not get resolved" );
+        }
+
+        File buildOutputDirectory = new File( mavenProject.getBuild().getDirectory() );
+
+        String mojoStatusPath = MAVEN_STATUS_ROOT + File.separator
+                                + mojoExecution.getMojoDescriptor().getPluginDescriptor().getArtifactId() + File.separator
+                                + mojoExecution.getMojoDescriptor().getGoal() + File.separator
+                                + mojoExecution.getExecutionId();
+
+        File mojoStatusDir = new File( buildOutputDirectory, mojoStatusPath );
+
+        if ( !mojoStatusDir.exists() )
+        {
+            mojoStatusDir.mkdirs();
+        }
+
+        return mojoStatusDir;
+    }
+
+    /**
+     * <p>This method shall get invoked before the actual mojo task gets triggered,
+     * e.g. the actual compile in maven-compiler-plugin.</p>
+     *
+     * <p><b>Attention:</b> This method shall only get invoked if the plugin re-creates <b>all</b> the output.</p>
+     *
+     * <p>It first picks up the list of files created in the previous build and delete them.
+     * This step is necessary to prevent left-overs. After that we take a 'directory snapshot'
+     * (list of all files which exist in the outputDirectory after the clean). </p>
+     *
+     * <p>After the actual mojo task got executed you should invoke the method
+     * {@link #afterRebuildExecution()} to collect the list of files which got changed
+     * by this task.</p>
+     *
+     *
+     * @param outputDirectory
+     * @return all files which got created in the previous build and have been deleted now.
+     * @throws MojoExecutionException
+     */
+    public String[] beforeRebuildExecution( File outputDirectory ) throws MojoExecutionException
+    {
+        File mojoConfigBase = getMojoStatusDirectory();
+        File mojoConfigFile = new File( mojoConfigBase, "createdFiles.lst" );
+
+        String[] oldFiles;
+
+        try
+        {
+            oldFiles = FileUtils.fileReadArray( mojoConfigFile );
+            for ( String oldFileName : oldFiles )
+            {
+                File oldFile = new File( oldFileName );
+                oldFile.delete();
+            }
+        }
+        catch( IOException e )
+        {
+            throw new MojoExecutionException( "Error reading old mojo status", e );
+        }
+
+        // we remember all files which currently exist in the output directory
+        DirectoryScanner diffScanner = getDirectoryScanner();
+        diffScanner.setBasedir( outputDirectory );
+        if ( outputDirectory.exists() )
+        {
+            diffScanner.scan();
+        }
+        filesBeforeAction = diffScanner.getIncludedFiles();
+
+        return oldFiles;
+    }
+
+    /**
+     * <p>This method collects and stores all information about files changed since
+     * the call to {@link #beforeRebuildExecution(java.io.File)}.</p>
+     *
+     * <p><b>Attention:</b> This method shall only get invoked if the plugin re-creates <b>all</b> the output.</p>
+     *
+     * @throws MojoExecutionException
+     */
+    public void afterRebuildExecution() throws MojoExecutionException
+    {
+        DirectoryScanner diffScanner = getDirectoryScanner();
+        // now scan the same directory again and create a diff
+        diffScanner.scan();
+        DirectoryScanResult scanResult = diffScanner.diffIncludedFiles( filesBeforeAction );
+
+        File mojoConfigBase = getMojoStatusDirectory();
+        File mojoConfigFile = new File( mojoConfigBase, CREATED_FILES_LST_FILENAME );
+
+        try
+        {
+            FileUtils.fileWriteArray( mojoConfigFile, scanResult.getFilesAdded() );
+        }
+        catch( IOException e )
+        {
+            throw new MojoExecutionException( "Error while storing the mojo status", e );
+        }
+
+    }
+}

Propchange: maven/shared/trunk/maven-shared-incremental/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: maven/shared/trunk/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/pom.xml?rev=1388456&r1=1388455&r2=1388456&view=diff
==============================================================================
--- maven/shared/trunk/pom.xml (original)
+++ maven/shared/trunk/pom.xml Fri Sep 21 12:34:05 2012
@@ -37,6 +37,7 @@ under the License.
   <modules>
     <module>maven-shared-components</module>
     <module>maven-shared-utils</module>
+    <module>maven-shared-incremental</module>
     <module>file-management</module>
     <module>maven-ant</module>
     <module>maven-archiver</module>