You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sn...@apache.org on 2007/08/09 23:38:36 UTC
svn commit: r564391 - in /maven/plugins/branches/MWAR-97-2: ./
src/main/java/org/apache/maven/plugin/war/
src/main/java/org/apache/maven/plugin/war/packaging/
src/main/java/org/apache/maven/plugin/war/util/
src/test/java/org/apache/maven/plugin/war/
Author: snicoll
Date: Thu Aug 9 14:38:34 2007
New Revision: 564391
URL: http://svn.apache.org/viewvc?view=rev&rev=564391
Log:
- Added post packaging task framework to perform stuff once the webapp is packaged
- The webapp structure is now saved at each run to improve performance
Added:
maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/SaveWebappStructurePostPackagingTask.java
maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPostPackagingTask.java
maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/WebappStructureSerializer.java
Modified:
maven/plugins/branches/MWAR-97-2/pom.xml
maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java
maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java
Modified: maven/plugins/branches/MWAR-97-2/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/pom.xml?view=diff&rev=564391&r1=564390&r2=564391
==============================================================================
--- maven/plugins/branches/MWAR-97-2/pom.xml (original)
+++ maven/plugins/branches/MWAR-97-2/pom.xml Thu Aug 9 14:38:34 2007
@@ -59,6 +59,11 @@
<version>2.0.1</version>
</dependency>
<dependency>
+ <groupId>com.thoughtworks.xstream</groupId>
+ <artifactId>xstream</artifactId>
+ <version>1.2.2</version>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
Modified: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java?view=diff&rev=564391&r1=564390&r2=564391
==============================================================================
--- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java (original)
+++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java Thu Aug 9 14:38:34 2007
@@ -27,13 +27,16 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.war.overlay.OverlayManager;
import org.apache.maven.plugin.war.packaging.OverlayPackagingTask;
+import org.apache.maven.plugin.war.packaging.SaveWebappStructurePostPackagingTask;
import org.apache.maven.plugin.war.packaging.WarPackagingContext;
import org.apache.maven.plugin.war.packaging.WarPackagingTask;
+import org.apache.maven.plugin.war.packaging.WarPostPackagingTask;
import org.apache.maven.plugin.war.packaging.WarProjectPackagingTask;
import org.apache.maven.plugin.war.util.CompositeMap;
import org.apache.maven.plugin.war.util.PropertyUtils;
import org.apache.maven.plugin.war.util.ReflectionProperties;
import org.apache.maven.plugin.war.util.WebappStructure;
+import org.apache.maven.plugin.war.util.WebappStructureSerializer;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
@@ -150,11 +153,20 @@
/**
* The file containing the webapp structure cache.
*
- * @paramenter expression="${project.build.directory}/.webapp-cache.xml"
+ * @parameter expression="${project.build.directory}/war/work/webapp-cache.xml"
* @required
* @since 2.1
*/
- private File webappStructure;
+ private File cacheFile;
+
+ /**
+ * Whether the cache should be used to save the status of the webapp
+ * accross multiple runs.
+ *
+ * @parameter expression="${useCache}" default-value="true"
+ * @since 2.1
+ */
+ private boolean useCache = true;
/**
* To look up Archiver/UnArchiver implementations
@@ -222,6 +234,7 @@
private static final String[] EMPTY_STRING_ARRAY = {};
+ private final WebappStructureSerializer webappStructureSerialier = new WebappStructureSerializer();
/**
* Returns a string array of the excludes to be used
@@ -329,10 +342,14 @@
throws MojoExecutionException, MojoFailureException, IOException
{
- WebappStructure cache = new WebappStructure( null );
- if ( webappStructure != null && webappStructure.exists() )
+ WebappStructure cache;
+ if ( useCache && cacheFile.exists() )
+ {
+ cache = new WebappStructure( webappStructureSerialier.fromXml( cacheFile ) );
+ }
+ else
{
- // TODO: LOAD the webapp structure thingy using xstream
+ cache = new WebappStructure( null );
}
final long startTime = System.currentTimeMillis();
@@ -348,9 +365,18 @@
WarPackagingTask warPackagingTask = (WarPackagingTask) it.next();
warPackagingTask.performPackaging( context );
}
+
+ // Post packaging
+ final List postPackagingTasks = getPostPackagingTasks();
+ final Iterator it2 = postPackagingTasks.iterator();
+ while ( it2.hasNext() )
+ {
+ WarPostPackagingTask task = (WarPostPackagingTask) it2.next();
+ task.performPostPackaging( context );
+
+ }
getLog().info( "Webapp assembled in[" + ( System.currentTimeMillis() - startTime ) + " msecs]" );
- //TODO save the cache
}
/**
@@ -382,6 +408,24 @@
return packagingTasks;
}
+
+ /**
+ * Returns a <tt>List</tt> of the {@link org.apache.maven.plugin.war.packaging.WarPostPackagingTask}
+ * instances to invoke to perform the post-packaging.
+ *
+ * @return the list of post packaging tasks
+ */
+ private List getPostPackagingTasks()
+ {
+ final List postPackagingTasks = new ArrayList();
+ if ( useCache )
+ {
+ postPackagingTasks.add( new SaveWebappStructurePostPackagingTask( cacheFile ) );
+ }
+ // TODO add lib scanning to detect duplicates
+ return postPackagingTasks;
+ }
+
// War packaging implementation
private class DefaultWarPackagingContext
@@ -659,14 +703,14 @@
this.workDirectory = workDirectory;
}
- public File getWebappStructure()
+ public File getCacheFile()
{
- return webappStructure;
+ return cacheFile;
}
- public void setWebappStructure( File webappStructure )
+ public void setCacheFile( File cacheFile )
{
- this.webappStructure = webappStructure;
+ this.cacheFile = cacheFile;
}
public String getWarSourceIncludes()
@@ -687,5 +731,16 @@
public void setWarSourceExcludes( String warSourceExcludes )
{
this.warSourceExcludes = warSourceExcludes;
+ }
+
+
+ public boolean isUseCache()
+ {
+ return useCache;
+ }
+
+ public void setUseCache( boolean useCache )
+ {
+ this.useCache = useCache;
}
}
Added: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/SaveWebappStructurePostPackagingTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/SaveWebappStructurePostPackagingTask.java?view=auto&rev=564391
==============================================================================
--- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/SaveWebappStructurePostPackagingTask.java (added)
+++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/SaveWebappStructurePostPackagingTask.java Thu Aug 9 14:38:34 2007
@@ -0,0 +1,50 @@
+package org.apache.maven.plugin.war.packaging;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.war.util.WebappStructureSerializer;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Saves the webapp structure cache.
+ *
+ * @author Stephane Nicoll
+ */
+public class SaveWebappStructurePostPackagingTask
+ implements WarPostPackagingTask
+{
+
+ private final File targetFile;
+
+ private final WebappStructureSerializer serialier;
+
+
+ public SaveWebappStructurePostPackagingTask( File targetFile )
+ {
+ this.targetFile = targetFile;
+ this.serialier = new WebappStructureSerializer();
+ }
+
+ public void performPostPackaging( WarPackagingContext context )
+ throws MojoExecutionException, MojoFailureException
+ {
+ if ( targetFile == null )
+ {
+ context.getLog().debug( "Cache usage is disabled, not saving webapp structure." );
+ }
+ else
+ {
+ try
+ {
+ serialier.toXml( context.getWebappStructure(), targetFile );
+ context.getLog().info( "Cache saved successfully." );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Could not save webapp structure", e );
+ }
+ }
+ }
+}
Added: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPostPackagingTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPostPackagingTask.java?view=auto&rev=564391
==============================================================================
--- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPostPackagingTask.java (added)
+++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPostPackagingTask.java Thu Aug 9 14:38:34 2007
@@ -0,0 +1,27 @@
+package org.apache.maven.plugin.war.packaging;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+
+/**
+ * Defines tasks that should be performed after the packaging.
+ *
+ * @author Stephane Nicoll
+ */
+public interface WarPostPackagingTask
+{
+
+ /**
+ * Executes the post packaging task.
+ * <p/>
+ * The packaging context hold all information regarding the webapp that
+ * has been packaged.
+ *
+ * @param context the packaging context
+ * @throws MojoExecutionException if an error occured
+ * @throws MojoFailureException if a falure occured
+ */
+ void performPostPackaging( WarPackagingContext context )
+ throws MojoExecutionException, MojoFailureException;
+
+}
Added: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/WebappStructureSerializer.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/WebappStructureSerializer.java?view=auto&rev=564391
==============================================================================
--- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/WebappStructureSerializer.java (added)
+++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/WebappStructureSerializer.java Thu Aug 9 14:38:34 2007
@@ -0,0 +1,87 @@
+package org.apache.maven.plugin.war.util;
+
+import com.thoughtworks.xstream.XStream;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+/**
+ * Serializes {@link WebappStructure} back and forth.
+ *
+ * @author Stephane Nicoll
+ */
+public class WebappStructureSerializer
+{
+
+ private final XStream xStream;
+
+ /**
+ * Creates a new instance of the serializer.
+ */
+ public WebappStructureSerializer()
+ {
+ this.xStream = new XStream();
+
+ // Register aliases
+ xStream.alias( "webapp-structure", WebappStructure.class );
+ xStream.alias( "path-set", PathSet.class );
+ }
+
+
+ /**
+ * Reads the {@link WebappStructure} from the specified file.
+ *
+ * @param file the file containing the webapp structure
+ * @return the webapp structure
+ * @throws IOException if an error occured while reading the structure
+ */
+ public WebappStructure fromXml( File file )
+ throws IOException
+ {
+ FileReader reader = null;
+
+ try
+ {
+ reader = new FileReader( file );
+ return (WebappStructure) xStream.fromXML( reader );
+ }
+ finally
+ {
+ if ( reader != null )
+ {
+ reader.close();
+ }
+ }
+ }
+
+ /**
+ * Saves the {@link WebappStructure} to the specified file.
+ *
+ * @param webappStructure the structure to save
+ * @param targetFile the file to use to save the structure
+ * @throws IOException if an error occured while saving the webapp structure
+ */
+ public void toXml( WebappStructure webappStructure, File targetFile )
+ throws IOException
+ {
+ FileWriter writer = null;
+ try
+ {
+ if ( !targetFile.exists() && !targetFile.createNewFile() )
+ {
+ throw new IOException( "Could not create file[" + targetFile.getAbsolutePath() + "]" );
+ }
+ writer = new FileWriter( targetFile );
+ xStream.toXML( webappStructure, writer );
+ }
+ finally
+ {
+ if ( writer != null )
+ {
+ writer.close();
+ }
+ }
+ }
+}
Modified: maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java?view=diff&rev=564391&r1=564390&r2=564391
==============================================================================
--- maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java (original)
+++ maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/AbstractWarMojoTest.java Thu Aug 9 14:38:34 2007
@@ -61,6 +61,7 @@
throws Exception
{
setVariableValueToObject( mojo, "filters", filters );
+ setVariableValueToObject( mojo, "useCache", Boolean.FALSE );
mojo.setClassesDirectory( classesDir );
mojo.setWarSourceDirectory( webAppSource );
mojo.setWebappDirectory( webAppDir );
@@ -103,7 +104,7 @@
* @return the source directory for that test
* @throws Exception if an exception occurs
*/
- protected File getWebAppSource(String id)
+ protected File getWebAppSource( String id )
throws Exception
{
return new File( getTestDirectory(), "/" + id + "-test-data/source" );
@@ -119,7 +120,7 @@
protected File createWebAppSource( String id, boolean createSamples )
throws Exception
{
- File webAppSource = getWebAppSource( id);
+ File webAppSource = getWebAppSource( id );
if ( createSamples )
{
File simpleJSP = new File( webAppSource, "pansit.jsp" );
@@ -234,7 +235,8 @@
throws Exception
{
final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
- if (destFile.exists()) {
+ if ( destFile.exists() )
+ {
return destFile;
}
@@ -267,7 +269,8 @@
{
// Create war file
final File destFile = new File( OVERLAYS_TEMP_DIR, id + ".war" );
- if (!destFile.exists()) {
+ if ( !destFile.exists() )
+ {
createArchive( new File( OVERLAYS_ROOT_DIR, id ), destFile );
}