This is an automated email from the ASF dual-hosted git repository. hboutemy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-shared-incremental.git
commit 86b8f567f4cb7f16d68c1a99d8f76376dad71b55 Author: Mark Struberg <strub...@apache.org> AuthorDate: Fri Sep 21 12:34:05 2012 +0000 MSHARED-245 add IncremtalBuildHelper git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1388456 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 135 ++++++++++++++ .../shared/incremental/IncrementalBuildHelper.java | 193 +++++++++++++++++++++ 2 files changed, 328 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8ce1d7f --- /dev/null +++ b/pom.xml @@ -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> diff --git a/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java b/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java new file mode 100644 index 0000000..5602b08 --- /dev/null +++ b/src/main/java/org/apache/maven/shared/incremental/IncrementalBuildHelper.java @@ -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 ); + } + + } +} -- To stop receiving notification emails like this one, please contact "commits@maven.apache.org" <commits@maven.apache.org>.