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/11/17 23:22:53 UTC
svn commit: r596014 - in /maven/plugins/trunk/maven-war-plugin/src:
main/java/org/apache/maven/plugin/war/
main/java/org/apache/maven/plugin/war/packaging/
main/java/org/apache/maven/plugin/war/util/
test/java/org/apache/maven/plugin/war/
Author: snicoll
Date: Sat Nov 17 14:22:53 2007
New Revision: 596014
URL: http://svn.apache.org/viewvc?rev=596014&view=rev
Log:
MWAR-131: Ability to deploy the content of WEB-INF/classes as a classified artifact
Added:
maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/ClassesPackager.java
Modified:
maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/WarMojo.java
maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/ClassesPackagingTask.java
maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarMojoTest.java
Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/WarMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/WarMojo.java?rev=596014&r1=596013&r2=596014&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/WarMojo.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/WarMojo.java Sat Nov 17 14:22:53 2007
@@ -24,6 +24,7 @@
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.war.util.ClassesPackager;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.ManifestException;
@@ -96,31 +97,25 @@
*/
private boolean failOnMissingWebXml = true;
- // ----------------------------------------------------------------------
- // Implementation
- // ----------------------------------------------------------------------
+ /**
+ * Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the
+ * project.
+ *
+ * @parameter default-value="false"
+ */
+ private boolean attachClasses = false;
/**
- * Overload this to produce a test-war, for example.
+ * The classifier to use for the attached classes artifact.
+ *
+ * @parameter default-value="classes"
*/
- protected String getClassifier()
- {
- return classifier;
- }
+ private String classesClassifier = "classes";
- protected static File getWarFile( File basedir, String finalName, String classifier )
- {
- if ( classifier == null )
- {
- classifier = "";
- }
- else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
- {
- classifier = "-" + classifier;
- }
+ // ----------------------------------------------------------------------
+ // Implementation
+ // ----------------------------------------------------------------------
- return new File( basedir, finalName + classifier + ".war" );
- }
/**
* Executes the WarMojo on the current project.
@@ -130,7 +125,7 @@
public void execute()
throws MojoExecutionException, MojoFailureException
{
- File warFile = getWarFile( new File( outputDirectory ), warName, classifier );
+ File warFile = getTargetWarFile();
try
{
@@ -158,11 +153,13 @@
* Generates the webapp according to the <tt>mode</tt> attribute.
*
* @param warFile the target war file
- * @throws IOException
- * @throws ArchiverException
- * @throws ManifestException
+ * @throws IOException if an error occured while copying files
+ * @throws ArchiverException if the archive could not be created
+ * @throws ManifestException if the manifest could not be created
* @throws DependencyResolutionRequiredException
- *
+ * if an error occured while resolving the dependencies
+ * @throws MojoExecutionException if the execution failed
+ * @throws MojoFailureException if a fatal exception occured
*/
private void performPackaging( File warFile )
throws IOException, ArchiverException, ManifestException, DependencyResolutionRequiredException,
@@ -195,6 +192,20 @@
// create archive
archiver.createArchive( getProject(), archive );
+ // create the classes to be attached if necessary
+ if ( isAttachClasses() )
+ {
+ ClassesPackager packager = new ClassesPackager();
+ final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
+ if ( classesDirectory.exists() )
+ {
+ getLog().info( "Packaging classes" );
+ packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getProject(),
+ archive );
+ projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
+ }
+ }
+
String classifier = this.classifier;
if ( classifier != null )
{
@@ -212,6 +223,115 @@
artifact.setFile( warFile );
}
}
+ }
+
+
+ protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
+ {
+ if ( classifier == null )
+ {
+ classifier = "";
+ }
+ else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
+ {
+ classifier = "-" + classifier;
+ }
+
+ return new File( basedir, finalName + classifier + "." + type );
+ }
+
+
+ protected File getTargetWarFile()
+ {
+ return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
+
+ }
+
+ protected File getTargetClassesFile()
+ {
+ return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
+ }
+
+ // Getters and Setters
+
+ public String getClassifier()
+ {
+ return classifier;
+ }
+
+ public void setClassifier( String classifier )
+ {
+ this.classifier = classifier;
+ }
+
+ public String getOutputDirectory()
+ {
+ return outputDirectory;
+ }
+
+ public void setOutputDirectory( String outputDirectory )
+ {
+ this.outputDirectory = outputDirectory;
+ }
+
+ public String getWarName()
+ {
+ return warName;
+ }
+
+ public void setWarName( String warName )
+ {
+ this.warName = warName;
+ }
+
+ public WarArchiver getWarArchiver()
+ {
+ return warArchiver;
+ }
+
+ public void setWarArchiver( WarArchiver warArchiver )
+ {
+ this.warArchiver = warArchiver;
+ }
+
+ public MavenProjectHelper getProjectHelper()
+ {
+ return projectHelper;
+ }
+
+ public void setProjectHelper( MavenProjectHelper projectHelper )
+ {
+ this.projectHelper = projectHelper;
+ }
+
+ public boolean isPrimaryArtifact()
+ {
+ return primaryArtifact;
+ }
+
+ public void setPrimaryArtifact( boolean primaryArtifact )
+ {
+ this.primaryArtifact = primaryArtifact;
+ }
+
+ public boolean isAttachClasses()
+ {
+ return attachClasses;
+ }
+
+ public void setAttachClasses( boolean attachClasses )
+ {
+ this.attachClasses = attachClasses;
+ }
+
+ public String getClassesClassifier()
+ {
+ return classesClassifier;
+ }
+
+ public void setClassesClassifier( String classesClassifier )
+ {
+ this.classesClassifier = classesClassifier;
}
public boolean isFailOnMissingWebXml()
Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/ClassesPackagingTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/ClassesPackagingTask.java?rev=596014&r1=596013&r2=596014&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/ClassesPackagingTask.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/ClassesPackagingTask.java Sat Nov 17 14:22:53 2007
@@ -1,12 +1,29 @@
package org.apache.maven.plugin.war.packaging;
-import org.apache.maven.archiver.MavenArchiver;
-import org.apache.maven.artifact.DependencyResolutionRequiredException;
+/*
+ * 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.plugin.MojoExecutionException;
import org.apache.maven.plugin.war.Overlay;
+import org.apache.maven.plugin.war.util.ClassesPackager;
import org.apache.maven.plugin.war.util.PathSet;
-import org.codehaus.plexus.archiver.ArchiverException;
-import org.codehaus.plexus.archiver.jar.ManifestException;
import java.io.File;
import java.io.IOException;
@@ -68,32 +85,9 @@
final File libDirectory = new File( context.getWebappDirectory(), LIB_PATH );
final File jarFile = new File( libDirectory, archiveName );
-
- try
- {
- final MavenArchiver archiver = new MavenArchiver();
- archiver.setArchiver( context.getJarArchiver() );
- archiver.setOutputFile( jarFile );
- archiver.getArchiver().addDirectory( context.getClassesDirectory(), context.getWebappSourceIncludes(),
- context.getWebappSourceExcludes() );
- archiver.createArchive( context.getProject(), context.getArchive() );
- }
- catch ( ArchiverException e )
- {
- throw new MojoExecutionException( "Could not create classes archive", e );
- }
- catch ( ManifestException e )
- {
- throw new MojoExecutionException( "Could not create classes archive", e );
- }
- catch ( IOException e )
- {
- throw new MojoExecutionException( "Could not create classes archive", e );
- }
- catch ( DependencyResolutionRequiredException e )
- {
- throw new MojoExecutionException( "Could not create classes archive", e );
- }
+ final ClassesPackager packager = new ClassesPackager();
+ packager.packageClasses( context.getClassesDirectory(), jarFile, context.getJarArchiver(),
+ context.getProject(), context.getArchive() );
}
else
{
Added: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/ClassesPackager.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/ClassesPackager.java?rev=596014&view=auto
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/ClassesPackager.java (added)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/ClassesPackager.java Sat Nov 17 14:22:53 2007
@@ -0,0 +1,103 @@
+package org.apache.maven.plugin.war.util;
+
+import org.apache.maven.archiver.MavenArchiveConfiguration;
+import org.apache.maven.archiver.MavenArchiver;
+import org.apache.maven.artifact.DependencyResolutionRequiredException;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.war.packaging.AbstractWarPackagingTask;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.archiver.ArchiverException;
+import org.codehaus.plexus.archiver.jar.JarArchiver;
+import org.codehaus.plexus.archiver.jar.ManifestException;
+
+import java.io.File;
+import java.io.IOException;
+
+/*
+ * 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.
+ */
+
+
+/**
+ * Packages the content of the classes directory.
+ *
+ * @author Stephane Nicoll
+ */
+public class ClassesPackager
+{
+
+ /**
+ * Creates a new instance.
+ */
+ public ClassesPackager()
+ {
+ super();
+ }
+
+ /**
+ * Package the classes
+ *
+ * @param classesDirectory the classes directory
+ * @param targetFile the target file
+ * @param jarArchiver the jar archiver to use
+ * @param project the related project
+ * @param archiveConfiguration the archive configuration to use
+ * @throws MojoExecutionException if an error occured while creating the archive
+ */
+ public void packageClasses( File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenProject project,
+ MavenArchiveConfiguration archiveConfiguration )
+ throws MojoExecutionException
+ {
+
+ try
+ {
+ final MavenArchiver archiver = new MavenArchiver();
+ archiver.setArchiver( jarArchiver );
+ archiver.setOutputFile( targetFile );
+ archiver.getArchiver().addDirectory( classesDirectory );
+ archiver.createArchive( project, archiveConfiguration );
+ }
+ catch ( ArchiverException e )
+ {
+ throw new MojoExecutionException( "Could not create classes archive", e );
+ }
+ catch ( ManifestException e )
+ {
+ throw new MojoExecutionException( "Could not create classes archive", e );
+ }
+ catch ( IOException e )
+ {
+ throw new MojoExecutionException( "Could not create classes archive", e );
+ }
+ catch ( DependencyResolutionRequiredException e )
+ {
+ throw new MojoExecutionException( "Could not create classes archive", e );
+ }
+ }
+
+ /**
+ * Returns the classes directory from the specified webapp directory.
+ *
+ * @param webappDirectory the webapp directory
+ * @return the classes directory of the specified webapp directory
+ */
+ public File getClassesDirectory( File webappDirectory )
+ {
+ return new File( webappDirectory, AbstractWarPackagingTask.CLASSES_PATH );
+ }
+}
Modified: maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarMojoTest.java?rev=596014&r1=596013&r2=596014&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarMojoTest.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarMojoTest.java Sat Nov 17 14:22:53 2007
@@ -323,6 +323,63 @@
}
}
+ public void testAttachClasses()
+ throws Exception
+ {
+ String testId = "AttachClasses";
+ MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
+ String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
+ File webAppDirectory = new File( getTestDirectory(), testId );
+ WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
+ String warName = "simple";
+ File webAppSource = createWebAppSource( testId );
+ File classesDir = createClassesDir( testId, false );
+ File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
+
+ project.setArtifact( warArtifact );
+ this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
+ setVariableValueToObject( mojo, "outputDirectory", outputDir );
+ setVariableValueToObject( mojo, "warName", warName );
+ mojo.setWebXml( new File( xmlSource, "web.xml" ) );
+ mojo.setAttachClasses( true );
+
+ mojo.execute();
+
+ //validate jar file
+ File expectedJarFile = new File( outputDir, "simple-classes.jar" );
+ assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "sample-servlet.class"},
+ new String[]{null, null} );
+ }
+
+ public void testAttachClassesWithCustomClassifier()
+ throws Exception
+ {
+ String testId = "AttachClassesCustomClassifier";
+ MavenProject4CopyConstructor project = new MavenProject4CopyConstructor();
+ String outputDir = getTestDirectory().getAbsolutePath() + "/" + testId + "-output";
+ File webAppDirectory = new File( getTestDirectory(), testId );
+ WarArtifact4CCStub warArtifact = new WarArtifact4CCStub( getBasedir() );
+ String warName = "simple";
+ File webAppSource = createWebAppSource( testId );
+ File classesDir = createClassesDir( testId, false );
+ File xmlSource = createXMLConfigDir( testId, new String[]{"web.xml"} );
+
+ project.setArtifact( warArtifact );
+ this.configureMojo( mojo, new LinkedList(), classesDir, webAppSource, webAppDirectory, project );
+ setVariableValueToObject( mojo, "outputDirectory", outputDir );
+ setVariableValueToObject( mojo, "warName", warName );
+ mojo.setWebXml( new File( xmlSource, "web.xml" ) );
+ mojo.setAttachClasses( true );
+ mojo.setClassesClassifier( "mystuff" );
+
+ mojo.execute();
+
+ //validate jar file
+ File expectedJarFile = new File( outputDir, "simple-mystuff.jar" );
+ assertJarContent( expectedJarFile, new String[]{"META-INF/MANIFEST.MF", "sample-servlet.class"},
+ new String[]{null, null} );
+ }
+
protected Map assertJarContent( final File expectedJarFile, final String[] files, final String[] filesContent )
throws IOException