Skip to content

Commit

Permalink
[MWAR-430] Discover Jakarta Servlet API as excuse for not having web.xml
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
pzygielo committed Jun 5, 2020
1 parent fb71c01 commit 631d9c9
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/it/MWAR-430_jakarta-servlet/invoker.properties
Original file line number Diff line number Diff line change
@@ -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+

66 changes: 66 additions & 0 deletions src/it/MWAR-430_jakarta-servlet/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
49 changes: 49 additions & 0 deletions src/it/MWAR-430_jakarta-servlet/verify.bsh
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 20 additions & 5 deletions src/main/java/org/apache/maven/plugins/war/WarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,16 @@ else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
}

/**
* 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.
*/
Expand All @@ -321,9 +324,21 @@ private boolean isProjectUsingAtLeastServlet30()
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 )
Expand Down

0 comments on commit 631d9c9

Please sign in to comment.