MEAR-146: add an option to tune the way the library-directory element is generated in the application.xml. Based on patch provided by Alex Halovanic. Also fixes MEAR-151.

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1377814 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java b/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java
index 57307e8..d4b7f54 100644
--- a/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java
+++ b/src/main/java/org/apache/maven/plugin/ear/GenerateApplicationXmlMojo.java
@@ -33,6 +33,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -47,6 +48,11 @@
     extends AbstractEarMojo
 {
 
+    public static final String DEFAULT = "DEFAULT";
+
+    public static final String EMPTY = "EMPTY";
+
+    public static final String NONE = "NONE";
 
     /**
      * Whether the application.xml should be generated or not.
@@ -82,6 +88,24 @@
     private String description;
 
     /**
+     * Defines how the <tt>library-directory</tt> element should be written in the application.xml file.
+     * <p/>
+     * Three special values can be set:
+     * <ul>
+     * <li><code>DEFAULT</code> (default) generates a <tt>library-directory</tt> element with
+     * the value of the <tt>defaultLibBundleDir</tt>  parameter</li>
+     * <li><code>EMPTY</code> generates an empty <tt>library-directory</tt> element. Per spec, this disables
+     * the scanning of jar files in the <tt>lib</tt> directory of the ear file</li>
+     * <li><code>NONE</code> does not write the library-directory element at all. A corner case that can
+     * be used in Oracle Weblogic to delegate the classloading to the container</li>
+     * </ul>
+     * <p/>
+     * Since JavaEE5.
+     */
+    @Parameter( defaultValue = DEFAULT )
+    private String libraryDirectoryMode;
+
+    /**
      * Defines the value of the initialize in order element to be used when
      * the application.xml file is auto-generated. When set to true, modules
      * must be initialized in the order they're listed in this deployment descriptor,
@@ -192,7 +216,7 @@
         final ApplicationXmlWriter writer = new ApplicationXmlWriter( javaEEVersion, encoding, generateModuleId );
         final ApplicationXmlWriterContext context =
             new ApplicationXmlWriterContext( descriptor, getModules(), buildSecurityRoles(), buildEnvEntries(),
-                                             displayName, description, defaultLibBundleDir, applicationName,
+                                             displayName, description, getActualLibraryDirectory(), applicationName,
                                              initializeInOrder );
         writer.write( context );
     }
@@ -304,4 +328,32 @@
         }
 
     }
+
+    /**
+     * Returns the value to use for the <tt>library-directory</tt> element, based on the library directory mode.
+     */
+    private String getActualLibraryDirectory()
+        throws EarPluginException
+    {
+        final String mode = libraryDirectoryMode == null ? DEFAULT : libraryDirectoryMode.toUpperCase();
+
+        if ( DEFAULT.equals( mode ) )
+        {
+            return defaultLibBundleDir;
+        }
+        else if ( EMPTY.equals( mode ) )
+        {
+            return "";
+        }
+        else if ( NONE.equals( mode ) )
+        {
+            return null;
+        }
+        else
+        {
+            throw new EarPluginException(
+                "Unsupported library directory mode [" + libraryDirectoryMode + "] Supported modes " +
+                    ( Arrays.asList( DEFAULT, EMPTY, NONE ) ) );
+        }
+    }
 }
\ No newline at end of file
diff --git a/src/site/apt/tests.apt b/src/site/apt/tests.apt
index 8a45549..02068b2 100644
--- a/src/site/apt/tests.apt
+++ b/src/site/apt/tests.apt
@@ -185,3 +185,9 @@
   * project-077: builds an EAR with custom env entries settings and JavaEE 6
 
   * project-078: builds an EAR with the no version for ejb file name mapping
+
+  * project-079: builds an EAR with the 'default' library directory mode. Uses the value of the defaultLibBundleDir
+
+  * project-080: builds an EAR with the 'empty' library directory mode. Generate an empty library-directory element
+
+  * project-081: builds an EAR with the 'none' library directory mode. Does not generate an library-directory element
diff --git a/src/test/java/org/apache/maven/plugin/ear/it/EarMojoIT.java b/src/test/java/org/apache/maven/plugin/ear/it/EarMojoIT.java
index 660de35..04565eb 100644
--- a/src/test/java/org/apache/maven/plugin/ear/it/EarMojoIT.java
+++ b/src/test/java/org/apache/maven/plugin/ear/it/EarMojoIT.java
@@ -822,4 +822,31 @@
                        new String[]{ "ejb-sample-one.jar", "war-sample-one-1.0.war", "jar-sample-two-1.0.jar" } );
     }
 
+    /**
+     * Builds an EAR with the 'default' library directory mode. Uses the value of the defaultLibBundleDir.
+     */
+    public void testProject079()
+        throws Exception
+    {
+        doTestProject( "project-079", new String[]{ "ejb-sample-one-1.0.jar", "myLibs/jar-sample-one-1.0.jar" } );
+    }
+
+    /**
+     * Builds an EAR with the 'empty' library directory mode. Generate an empty library-directory element.
+     */
+    public void testProject080()
+        throws Exception
+    {
+        doTestProject( "project-080", new String[]{ "ejb-sample-one-1.0.jar", "myLibs/jar-sample-one-1.0.jar" } );
+    }
+
+    /**
+     * Builds an EAR with the 'none' library directory mode. Does not generate an library-directory element.
+     */
+    public void testProject081()
+        throws Exception
+    {
+        doTestProject( "project-081", new String[]{ "ejb-sample-one-1.0.jar", "myLibs/jar-sample-one-1.0.jar" } );
+    }
+
 }
diff --git a/src/test/resources/projects/project-079/expected-META-INF/application.xml b/src/test/resources/projects/project-079/expected-META-INF/application.xml
new file mode 100644
index 0000000..6607a0f
--- /dev/null
+++ b/src/test/resources/projects/project-079/expected-META-INF/application.xml
@@ -0,0 +1,26 @@
+<?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.
+-->
+<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
+  <display-name>maven-ear-plugin-test-project-079</display-name>
+  <module>
+    <ejb>ejb-sample-one-1.0.jar</ejb>
+  </module>
+  <library-directory>myLibs</library-directory>
+</application>
diff --git a/src/test/resources/projects/project-079/pom.xml b/src/test/resources/projects/project-079/pom.xml
new file mode 100644
index 0000000..ccff272
--- /dev/null
+++ b/src/test/resources/projects/project-079/pom.xml
@@ -0,0 +1,56 @@
+<?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/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>ear</groupId>
+  <artifactId>maven-ear-plugin-test-project-079</artifactId>
+  <version>99.0</version>
+  <name>Maven</name>
+  <packaging>ear</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>ejb-sample-one</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>jar-sample-one</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <version>6</version>
+          <defaultLibBundleDir>myLibs</defaultLibBundleDir>
+          <libraryDirectoryMode>default</libraryDirectoryMode>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/test/resources/projects/project-080/expected-META-INF/application.xml b/src/test/resources/projects/project-080/expected-META-INF/application.xml
new file mode 100644
index 0000000..5277692
--- /dev/null
+++ b/src/test/resources/projects/project-080/expected-META-INF/application.xml
@@ -0,0 +1,26 @@
+<?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.
+-->
+<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
+  <display-name>maven-ear-plugin-test-project-080</display-name>
+  <module>
+    <ejb>ejb-sample-one-1.0.jar</ejb>
+  </module>
+  <library-directory/>
+</application>
diff --git a/src/test/resources/projects/project-080/pom.xml b/src/test/resources/projects/project-080/pom.xml
new file mode 100644
index 0000000..a3c10e8
--- /dev/null
+++ b/src/test/resources/projects/project-080/pom.xml
@@ -0,0 +1,56 @@
+<?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/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>ear</groupId>
+  <artifactId>maven-ear-plugin-test-project-080</artifactId>
+  <version>99.0</version>
+  <name>Maven</name>
+  <packaging>ear</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>ejb-sample-one</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>jar-sample-one</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <version>6</version>
+          <defaultLibBundleDir>myLibs</defaultLibBundleDir>
+          <libraryDirectoryMode>empty</libraryDirectoryMode>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/test/resources/projects/project-081/expected-META-INF/application.xml b/src/test/resources/projects/project-081/expected-META-INF/application.xml
new file mode 100644
index 0000000..cb9f96b
--- /dev/null
+++ b/src/test/resources/projects/project-081/expected-META-INF/application.xml
@@ -0,0 +1,25 @@
+<?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.
+-->
+<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
+  <display-name>maven-ear-plugin-test-project-081</display-name>
+  <module>
+    <ejb>ejb-sample-one-1.0.jar</ejb>
+  </module>
+</application>
diff --git a/src/test/resources/projects/project-081/pom.xml b/src/test/resources/projects/project-081/pom.xml
new file mode 100644
index 0000000..4b8a0eb
--- /dev/null
+++ b/src/test/resources/projects/project-081/pom.xml
@@ -0,0 +1,56 @@
+<?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/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>ear</groupId>
+  <artifactId>maven-ear-plugin-test-project-081</artifactId>
+  <version>99.0</version>
+  <name>Maven</name>
+  <packaging>ear</packaging>
+  <dependencies>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>ejb-sample-one</artifactId>
+      <version>1.0</version>
+      <type>ejb</type>
+    </dependency>
+    <dependency>
+      <groupId>eartest</groupId>
+      <artifactId>jar-sample-one</artifactId>
+      <version>1.0</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-ear-plugin</artifactId>
+        <version>@project.version@</version>
+        <configuration>
+          <version>6</version>
+          <defaultLibBundleDir>myLibs</defaultLibBundleDir>
+          <libraryDirectoryMode>none</libraryDirectoryMode>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>