Author: dennisl
Date: Fri Nov 18 13:21:14 2011
New Revision: 1203638
URL: http://svn.apache.org/viewvc?rev=1203638&view=rev
Log:
[MWAR-81] Request enhancement to pattern matching for
packagingIncludes/packagingExcludes functionality (regular expressions?)
Submitted by: Nicolas Marcotte
Reviewed by: Dennis Lundberg
o Minor adjustments to code style
o Added a page to the site with examples
Added:
maven/plugins/trunk/maven-war-plugin/src/site/apt/examples/including-excluding-files-from-war.apt.vm
(with props)
Modified:
maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/WarMojo.java
maven/plugins/trunk/maven-war-plugin/src/site/apt/index.apt
maven/plugins/trunk/maven-war-plugin/src/site/site.xml
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=1203638&r1=1203637&r2=1203638&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
Fri Nov 18 13:21:14 2011
@@ -75,7 +75,9 @@ public class WarMojo
/**
* The comma separated list of tokens to exclude from the WAR before
* packaging. This option may be used to implement the skinny WAR use
- * case.
+ * case. Note the you can use the Java Regular Expressions engine to
+ * include and exclude specific pattern using the expression %regex[].
+ * Hint: read the about (?!Pattern).
*
* @parameter
* @since 2.1-alpha-2
@@ -85,7 +87,9 @@ public class WarMojo
/**
* The comma separated list of tokens to include in the WAR before
* packaging. By default everything is included. This option may be used
- * to implement the skinny WAR use case.
+ * to implement the skinny WAR use case. Note the you can use the
+ * Java Regular Expressions engine to include and exclude specific pattern
+ * using the expression %regex[].
*
* @parameter
* @since 2.1-beta-1
Added:
maven/plugins/trunk/maven-war-plugin/src/site/apt/examples/including-excluding-files-from-war.apt.vm
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/site/apt/examples/including-excluding-files-from-war.apt.vm?rev=1203638&view=auto
==============================================================================
---
maven/plugins/trunk/maven-war-plugin/src/site/apt/examples/including-excluding-files-from-war.apt.vm
(added)
+++
maven/plugins/trunk/maven-war-plugin/src/site/apt/examples/including-excluding-files-from-war.apt.vm
Fri Nov 18 13:21:14 2011
@@ -0,0 +1,82 @@
+ ------
+ Including and Excluding Files From the WAR
+ ------
+ Dennis Lundberg
+ ------
+ 2011-11-21
+ ------
+
+~~ 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.
+
+~~ NOTE: For help with the syntax of this file, see:
+~~ http://maven.apache.org/doxia/references/apt-format.html
+
+Including and Excluding Files From the WAR
+
+
+ It is possible to include or exclude certain files from the WAR file, by
using the <<<\<packagingIncludes\>>>> and <<<\<packagingExcludes\>>>>
configuration parameters. They each take a comma-separated list of Ant file set
patterns. You can use wildcards such as <<<**>>> to indicate multiple
directories and <<<*>>> to indicate an optional part of a file or directory
name.
+
+ Here is an example where we exclude all JAR files from <<<WEB-INF/lib>>>:
+
++-----------------+
+<project>
+ ...
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-war-plugin</artifactId>
+ <version>${project.version}</version>
+ <configuration>
+ <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ ...
+</project>
++-----------------+
+
+ Sometimes even such wildcards are not enough. In these cases you can use
regular expressions with the <<<%regex[]>>> syntax. Here is a real life use
case in which this is used. In this example we want to exclude any
commons-logging and log4j JARs, but we do not want to exclude the
log4j-over-slf4j JAR. So we want to exclude <<<log4j-\<version\>.jar>>> but
keep the <<<log4j-over-slf4j-\<version\>.jar>>>.
+
++-----------------+
+<project>
+ ...
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-war-plugin</artifactId>
+ <version>${project.version}</version>
+ <configuration>
+ <!--
+ Exclude JCL and LOG4J since all logging should go through SLF4J.
+ Note that we're excluding log4j-<version>.jar but keeping
+ log4j-over-slf4j-<version>.jar
+ -->
+ <packagingExcludes>
+ WEB-INF/lib/commons-logging-*.jar,
+ %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
+ </packagingExcludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ ...
+</project>
++-----------------+
+
+ If you have more real life examples of using regular expressions, we'd like
to know about them. Please file an issue in {{{../issue-tracking.html}our issue
tracker}} with your configuration, so we can expand this page.
Propchange:
maven/plugins/trunk/maven-war-plugin/src/site/apt/examples/including-excluding-files-from-war.apt.vm
------------------------------------------------------------------------------
svn:eol-style = native
Modified: maven/plugins/trunk/maven-war-plugin/src/site/apt/index.apt
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/site/apt/index.apt?rev=1203638&r1=1203637&r2=1203638&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/site/apt/index.apt (original)
+++ maven/plugins/trunk/maven-war-plugin/src/site/apt/index.apt Fri Nov 18
13:21:14 2011
@@ -81,6 +81,8 @@ Maven WAR Plugin
* {{{./examples/skinny-wars.html}Creating Skinny WARs}}
+ * {{{./examples/including-excluding-files-from-war.html}Including and
Excluding Files From the WAR}}
+
* {{{./examples/file-name-mapping.html}Using File Name Mapping}}
[]
Modified: maven/plugins/trunk/maven-war-plugin/src/site/site.xml
URL:
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/site/site.xml?rev=1203638&r1=1203637&r2=1203638&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/site/site.xml (original)
+++ maven/plugins/trunk/maven-war-plugin/src/site/site.xml Fri Nov 18 13:21:14
2011
@@ -38,6 +38,7 @@ under the License.
<item name="WAR Manifest Customization"
href="examples/war-manifest-guide.html"/>
<item name="Rapid Testing Using the Jetty Plugin"
href="examples/rapid-testing-jetty6-plugin.html"/>
<item name="Creating Skinny WARs" href="examples/skinny-wars.html"/>
+ <item name="Including and Excluding Files From the WAR"
href="examples/including-excluding-files-from-war.html"/>
<item name="Using File Name Mapping"
href="examples/file-name-mapping.html"/>
</menu>
</body>
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=1203638&r1=1203637&r2=1203638&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
Fri Nov 18 13:21:14 2011
@@ -93,6 +93,38 @@ public class WarMojoTest
new String[]{null,
mojo.getWebXml().toString(), null, null, null, null} );
}
+ public void testSimpleWarPackagingExcludeWithIncludesRegEx()
+ throws Exception
+ {
+ String testId = "SimpleWarPackagingExcludeWithIncludesRegEx";
+ 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, true );
+ 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" ) );
+ setVariableValueToObject(
mojo,"packagingIncludes","%regex[(.(?!exile))+]" );
+
+
+ mojo.execute();
+
+ //validate jar file
+ File expectedJarFile = new File( outputDir, "simple.war" );
+ assertJarContent( expectedJarFile, new
String[]{"META-INF/MANIFEST.MF", "WEB-INF/web.xml", "pansit.jsp",
+ "META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.xml",
+
"META-INF/maven/org.apache.maven.test/maven-test-plugin/pom.properties"},
+ new String[]{null,
mojo.getWebXml().toString(), null, null, null, },
+ new
String[]{"org/web/app/last-exile.jsp"} );
+ }
+
public void testClassifier()
throws Exception
{
@@ -384,6 +416,12 @@ public class WarMojoTest
protected Map assertJarContent( final File expectedJarFile, final String[]
files, final String[] filesContent )
throws IOException
{
+ return assertJarContent( expectedJarFile, files, filesContent, null );
+ }
+
+ protected Map assertJarContent( final File expectedJarFile, final String[]
files, final String[] filesContent, final String[] mustNotBeInJar )
+ throws IOException
+ {
// Sanity check
assertEquals( "Could not test, files and filesContent lenght does not
match", files.length,
filesContent.length );
@@ -404,6 +442,7 @@ public class WarMojoTest
for ( int i = 0; i < files.length; i++ )
{
String file = files[i];
+
assertTrue( "File[" + file + "] not found in archive",
jarContent.containsKey( file ) );
if ( filesContent[i] != null )
{
@@ -411,6 +450,16 @@ public class WarMojoTest
IOUtil.toString( jarFile.getInputStream(
(ZipEntry) jarContent.get( file ) ) ) );
}
}
+ if( mustNotBeInJar!=null )
+ {
+ for ( int i = 0; i < mustNotBeInJar.length; i++ )
+ {
+ String file = mustNotBeInJar[i];
+
+ assertFalse( "File[" + file + "] found in archive",
jarContent.containsKey( file ) );
+
+ }
+ }
return jarContent;
}