[MEAR-243] Skinny WARs - JAR not removed from WAR if scope in EAR is set to provided

When creating skinny wars, only JAR runtime modules were considered when removing libraries from WEB-INF/lib of an EarModule. If a JAR dependency is provided, it should also remove it from WEB-INF/lib.

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1763333 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/MEAR-243-skinny-wars-provided/ear-module/pom.xml b/src/it/MEAR-243-skinny-wars-provided/ear-module/pom.xml
new file mode 100644
index 0000000..7709a45
--- /dev/null
+++ b/src/it/MEAR-243-skinny-wars-provided/ear-module/pom.xml
@@ -0,0 +1,57 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.ear.skinnywars</groupId>
+  <artifactId>ear-module</artifactId>
+  <version>1.0</version>
+  <packaging>ear</packaging>
+
+  <dependencies>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.5</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.its.ear.skinnywars</groupId>
+      <artifactId>war-module</artifactId>
+      <version>1.0</version>
+      <type>war</type>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <skinnyWars>true</skinnyWars>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MEAR-243-skinny-wars-provided/pom.xml b/src/it/MEAR-243-skinny-wars-provided/pom.xml
new file mode 100644
index 0000000..f427da9
--- /dev/null
+++ b/src/it/MEAR-243-skinny-wars-provided/pom.xml
@@ -0,0 +1,37 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.ear.skinnywars</groupId>
+  <artifactId>pom</artifactId>
+  <version>1.0</version>
+  <packaging>pom</packaging>
+
+  <description>Test that provided JAR dependencies in EAR are removed from skinny WAR</description>
+  <url>https://issues.apache.org/jira/browse/MEAR-243</url>
+
+  <modules>
+      <module>ear-module</module>
+      <module>war-module</module>
+  </modules>
+</project>
diff --git a/src/it/MEAR-243-skinny-wars-provided/verify.bsh b/src/it/MEAR-243-skinny-wars-provided/verify.bsh
new file mode 100644
index 0000000..b379361
--- /dev/null
+++ b/src/it/MEAR-243-skinny-wars-provided/verify.bsh
@@ -0,0 +1,113 @@
+/*
+ * 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 java.io.*;
+import java.util.*;
+import java.util.jar.*;
+import java.util.regex.*;
+
+File jarFile = new File( basedir, "ear-module/target/ear-module-1.0/war-module-1.0.war" );
+System.out.println( "Checking for existence of " + jarFile );
+if ( !jarFile.isFile() )
+{
+    throw new IllegalStateException( "Missing file: " + jarFile );
+}
+
+JarFile jar = new JarFile( jarFile );
+
+String[] includedEntries = {
+    "WEB-INF/web.xml",
+    "META-INF/MANIFEST.MF"
+};
+for ( String included : includedEntries )
+{
+    System.out.println( "Checking for included archive entry " + included );
+    if ( jar.getEntry( included ) == null )
+    {
+        throw new IllegalStateException( "Missing archive entry: " + included );
+    }
+}
+
+Manifest manifest = jar.getManifest();
+String manifestClassPath = manifest.getMainAttributes().getValue("Class-Path");
+if ( manifestClassPath.contains("commons-lang-2.5.jar") )
+{
+    throw new IllegalStateException( "Surplus entry in war MANIFEST.MF: commons-lang-2.5.jar");
+}
+
+String[] excludedEntries = {
+    "WEB-INF/lib/commons-lang-2.5.jar"
+};
+for ( String excluded : excludedEntries )
+{
+    System.out.println( "Checking for excluded artifact " + excluded );
+    if ( jar.getEntry( excluded ) != null )
+    {
+        throw new IllegalStateException( "Archive entry should be excluded: " + excluded );
+    }
+}
+
+jar.close();
+
+
+File jarFile = new File( basedir, "war-module/target/war-module-1.0.war" );
+System.out.println( "Checking for existence of " + jarFile );
+if ( !jarFile.isFile() )
+{
+    throw new IllegalStateException( "Missing file: " + jarFile );
+}
+
+JarFile jar = new JarFile( jarFile );
+
+String[] includedEntries = {
+    "WEB-INF/web.xml",
+    "META-INF/MANIFEST.MF",
+    "WEB-INF/lib/commons-lang-2.5.jar"
+};
+for ( String included : includedEntries )
+{
+    System.out.println( "Checking for included archive entry " + included );
+    if ( jar.getEntry( included ) == null )
+    {
+        throw new IllegalStateException( "Missing archive entry: " + included );
+    }
+}
+
+jar.close();
+
+File earFile = new File( basedir, "ear-module/target/ear-module-1.0.ear" );
+
+JarFile ear = new JarFile( earFile );
+
+String[] excludedEntries = {
+    "commons-lang-2.5.jar",
+    "lib/commons-lang-2.5.jar",
+};
+for ( String excluded : excludedEntries )
+{
+    System.out.println( "Checking for excluded artifact " + excluded );
+    if ( ear.getEntry( excluded ) != null )
+    {
+        throw new IllegalStateException( "Archive entry should be excluded: " + excluded );
+    }
+}
+
+ear.close();
+
+return true;
diff --git a/src/it/MEAR-243-skinny-wars-provided/war-module/pom.xml b/src/it/MEAR-243-skinny-wars-provided/war-module/pom.xml
new file mode 100644
index 0000000..c6be50e
--- /dev/null
+++ b/src/it/MEAR-243-skinny-wars-provided/war-module/pom.xml
@@ -0,0 +1,47 @@
+<?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>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.ear.skinnywars</groupId>
+  <artifactId>war-module</artifactId>
+  <version>1.0</version>
+  <packaging>war</packaging>
+
+  <dependencies>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.5</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-war-plugin</artifactId>
+        <version>2.1.1</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MEAR-243-skinny-wars-provided/war-module/src/main/webapp/WEB-INF/web.xml b/src/it/MEAR-243-skinny-wars-provided/war-module/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..c1af95d
--- /dev/null
+++ b/src/it/MEAR-243-skinny-wars-provided/war-module/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,23 @@
+<?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.
+-->
+<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
+
+<web-app >
+</web-app>
diff --git a/src/main/java/org/apache/maven/plugins/ear/AbstractEarMojo.java b/src/main/java/org/apache/maven/plugins/ear/AbstractEarMojo.java
index de2b9e3..b789dfc 100644
--- a/src/main/java/org/apache/maven/plugins/ear/AbstractEarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/ear/AbstractEarMojo.java
@@ -165,7 +165,7 @@
 

     private List<EarModule> earModules;

 

-    private List<EarModule> allModules;

+    private List<JarModule> allJarModules;

 

     private JbossConfiguration jbossConfiguration;

 

@@ -211,7 +211,7 @@
         }

 

         getLog().debug( "Resolving ear modules ..." );

-        allModules = new ArrayList<EarModule>();

+        List<EarModule> allModules = new ArrayList<EarModule>();

         try

         {

             if ( modules != null && modules.length > 0 )

@@ -240,9 +240,8 @@
                     continue;

                 }

 

-                // Artifact is not yet registered and it has neither test, nor a

-                // provided scope, not is it optional

-                ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );

+                // Artifact is not yet registered and it has not test scope, nor is it optional

+                ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_COMPILE_PLUS_RUNTIME );

                 if ( !isArtifactRegistered( artifact, allModules ) && !artifact.isOptional()

                     && filter.include( artifact ) )

                 {

@@ -259,6 +258,8 @@
         }

 

         // Now we have everything let's built modules which have not been excluded

+        ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );

+        allJarModules = new ArrayList<JarModule>();

         earModules = new ArrayList<EarModule>();

         for ( EarModule earModule : allModules )

         {

@@ -268,14 +269,21 @@
             }

             else

             {

-                earModules.add( earModule );

+                if ( earModule instanceof JarModule )

+                {

+                    allJarModules.add( (JarModule) earModule );

+                }

+                if ( filter.include( earModule.getArtifact() ) )

+                {

+                    earModules.add( earModule );

+                }

             }

         }

 

     }

 

     /**

-     * @return The list of {@link #earModules}.

+     * @return The list of {@link #earModules}. This corresponds to modules needed at runtime.

      */

     protected List<EarModule> getModules()

     {

@@ -285,6 +293,18 @@
         }

         return earModules;

     }

+    

+    /**

+     * @return The list of {@link #allJarModules}. This corresponds to all JAR modules (compile + runtime).

+     */

+    protected List<JarModule> getAllJarModules()

+    {

+        if ( allJarModules == null )

+        {

+            throw new IllegalStateException( "Jar modules have not been initialized" );

+        }

+        return allJarModules;

+    }

 

     /**

      * @return {@link MavenProject}

diff --git a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
index 6ce69e6..3ee2903 100644
--- a/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/ear/EarMojo.java
@@ -803,32 +803,35 @@
                 classPath = new Attribute( "Class-Path", "" );

             }

 

+            // Remove JAR modules

+            for ( JarModule jm : getAllJarModules() )

+            {

+                if ( module.getLibDir() != null )

+                {

+                    // MEAR-189:

+                    // We use the original name, cause in case of fileNameMapping to no-version/full

+                    // we could not not delete it and it will end up in the resulting EAR and the WAR

+                    // will not be cleaned up.

+                    File artifact =

+                        new File( new File( workDirectory, module.getLibDir() ), jm.getOriginalBundleFileName() );

+

+                    if ( artifact.exists() )

+                    {

+                        getLog().debug( " -> Artifact to delete: " + artifact );

+                        if ( !artifact.delete() )

+                        {

+                            getLog().error( "Could not delete '" + artifact + "'" );

+                        }

+                    }

+                }

+            }

+

             // Modify the classpath entries in the manifest

             for ( EarModule o : getModules() )

             {

                 if ( o instanceof JarModule )

                 {

                     JarModule jm = (JarModule) o;

-

-                    if ( module.getLibDir() != null )

-                    {

-                        // MEAR-189:

-                        // We use the original name, cause in case of fileNameMapping to no-version/full

-                        // we could not not delete it and it will end up in the resulting EAR and the WAR

-                        // will not be cleaned up.

-                        File artifact =

-                            new File( new File( workDirectory, module.getLibDir() ), jm.getOriginalBundleFileName() );

-

-                        if ( artifact.exists() )

-                        {

-                            getLog().debug( " -> Artifact to delete: " + artifact );

-                            if ( !artifact.delete() )

-                            {

-                                getLog().error( "Could not delete '" + artifact + "'" );

-                            }

-                        }

-                    }

-

                     if ( classPathElements.contains( jm.getBundleFileName() ) )

                     {

                         classPathElements.set( classPathElements.indexOf( jm.getBundleFileName() ), jm.getUri() );