You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2020/06/05 22:31:12 UTC

[maven-war-plugin] branch MWAR-430 created (now 631d9c9)

This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a change to branch MWAR-430
in repository https://gitbox.apache.org/repos/asf/maven-war-plugin.git.


      at 631d9c9  [MWAR-430] Discover Jakarta Servlet API as excuse for not having web.xml

This branch includes the following new commits:

     new 631d9c9  [MWAR-430] Discover Jakarta Servlet API as excuse for not having web.xml

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-war-plugin] 01/01: [MWAR-430] Discover Jakarta Servlet API as excuse for not having web.xml

Posted by hb...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to branch MWAR-430
in repository https://gitbox.apache.org/repos/asf/maven-war-plugin.git

commit 631d9c9e16b27b9a8525f5c7d3c8b7dd302cda5f
Author: Piotrek Żygieło <pz...@users.noreply.github.com>
AuthorDate: Thu Jun 4 18:11:04 2020 +0200

    [MWAR-430] Discover Jakarta Servlet API as excuse for not having web.xml
    
    - Use MWAR-396_servlet30 as IT for this issue
    - Extract method to check for Servlet 3.0+
    - Extract method to check for class
    - Check for Jakarta annotation as well
    - Require at least JDK8 to use jakarta servlet API
---
 src/it/MWAR-430_jakarta-servlet/invoker.properties | 19 +++++++
 src/it/MWAR-430_jakarta-servlet/pom.xml            | 66 ++++++++++++++++++++++
 src/it/MWAR-430_jakarta-servlet/verify.bsh         | 49 ++++++++++++++++
 .../java/org/apache/maven/plugins/war/WarMojo.java | 25 ++++++--
 4 files changed, 154 insertions(+), 5 deletions(-)

diff --git a/src/it/MWAR-430_jakarta-servlet/invoker.properties b/src/it/MWAR-430_jakarta-servlet/invoker.properties
new file mode 100644
index 0000000..9695a95
--- /dev/null
+++ b/src/it/MWAR-430_jakarta-servlet/invoker.properties
@@ -0,0 +1,19 @@
+# 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.
+
+invoker.java.version = 1.8+
+
diff --git a/src/it/MWAR-430_jakarta-servlet/pom.xml b/src/it/MWAR-430_jakarta-servlet/pom.xml
new file mode 100644
index 0000000..8f773b1
--- /dev/null
+++ b/src/it/MWAR-430_jakarta-servlet/pom.xml
@@ -0,0 +1,66 @@
+<?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.war</groupId>
+  <artifactId>maven-it-mwar-430</artifactId>
+  <version>1.0</version>
+  <packaging>war</packaging>
+
+  <name>Maven Integration Test :: MWAR-430</name> 
+  <description>Test that no web.xml is required when project depends on Jakarta Servlet 5.0 or newer</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>jakarta.servlet</groupId>
+      <artifactId>jakarta.servlet-api</artifactId>
+      <version>5.0.0-M1</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.0.3</version>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.2</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-war-plugin</artifactId>
+        <version>@project.version@</version>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MWAR-430_jakarta-servlet/verify.bsh b/src/it/MWAR-430_jakarta-servlet/verify.bsh
new file mode 100644
index 0000000..3c7e8b5
--- /dev/null
+++ b/src/it/MWAR-430_jakarta-servlet/verify.bsh
@@ -0,0 +1,49 @@
+/*
+ * 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.*;
+
+try
+{
+    File explodedDir = new File( basedir, "target/maven-it-mwar-430-1.0" );
+    System.out.println( "Checking for existence of exploded directory " + explodedDir );
+    if ( !explodedDir.exists() )
+    {
+        System.out.println( "FAILURE! The directory " + explodedDir + " does not exist." );
+        return false;
+    }
+    
+    File webInfFile = new File( explodedDir, "WEB-INF/web.xml" );
+    if ( webInfFile.exists() )
+    {
+        System.err.println( "FAILURE! The file web.xml should not be present." );
+        return false;
+    }
+
+}
+catch( Throwable t )
+{
+    t.printStackTrace();
+    return false;
+}
+
+return true;
diff --git a/src/main/java/org/apache/maven/plugins/war/WarMojo.java b/src/main/java/org/apache/maven/plugins/war/WarMojo.java
index 261ef04..0df35ef 100644
--- a/src/main/java/org/apache/maven/plugins/war/WarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/war/WarMojo.java
@@ -301,13 +301,16 @@ public class WarMojo
     }
 
     /**
-     * Determines if the current Maven project being built uses the Servlet 3.0 API (JSR 315). If it does then the
-     * <code>web.xml</code> file can be omitted.
+     * Determines if the current Maven project being built uses the Servlet 3.0 API (JSR 315)
+     * or Jakarta Servlet API.
+     * If it does then the <code>web.xml</code> file can be omitted.
      * <p>
-     * This is done by checking if the interface <code>javax.servlet.annotation.WebServlet</code> is in the compile-time
+     * This is done by checking if the interface <code>javax.servlet.annotation.WebServlet</code>
+     * or <code>jakarta.servlet.annotation.WebServlet</code> is in the compile-time
      * dependencies (which includes provided dependencies) of the Maven project.
      *
-     * @return <code>true</code> if the project being built depends on Servlet 3.0 API, <code>false</code> otherwise.
+     * @return <code>true</code> if the project being built depends on Servlet 3.0 API or Jakarta Servlet API,
+     *         <code>false</code> otherwise.
      * @throws DependencyResolutionRequiredException if the compile elements can't be resolved.
      * @throws MalformedURLException if the path to a dependency file can't be transformed to a URL.
      */
@@ -321,9 +324,21 @@ public class WarMojo
             urls[i] = new File( classpathElements.get( i ) ).toURI().toURL();
         }
         ClassLoader loader = new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
+
+        return hasWebServletAnnotationClassInClasspath( loader );
+    }
+
+    private static boolean hasWebServletAnnotationClassInClasspath( ClassLoader loader )
+    {
+        return hasClassInClasspath( loader, "javax.servlet.annotation.WebServlet" )
+                || hasClassInClasspath( loader, "jakarta.servlet.annotation.WebServlet" );
+    }
+
+    private static boolean hasClassInClasspath( ClassLoader loader, String clazz )
+    {
         try
         {
-            Class.forName( "javax.servlet.annotation.WebServlet", false, loader );
+            Class.forName( clazz, false, loader );
             return true;
         }
         catch ( ClassNotFoundException e )