Why not to use / as a context path in maven tomcat?
Asked Answered
R

1

1

I'm using maven tomcat plugin to deploy an app. It does deploy in root context if I set path to <path>/<path> but I want to know if it causes any problems because the docs explicitly says "Do not use /".

Related question: How to set context path to root(“/”) in Tomcat 7.0 when using Maven

Why not to use / as path in maven tomcat?

Raver answered 19/3, 2019 at 18:32 Comment(0)
E
2

The JavaDoc pointing out to "Do not use /" was introduced in this commit:

(MTOMCAT-102) Add a mojo to build a standalone jar to run a web application: use path field to store webapp to have a more nice name than projet.build.finalName

When scrolling down the commit you see that the path property value is used to create a JarArchiveEntry:

os.putArchiveEntry( new JarArchiveEntry( path + ".war" ) );

which for configured <path>/<path> would translate to a file name of "/.war" looking kind of invalid. On the other this would possibly yield to ".war" file name when extracted.

Today for version 2.2 this piece of code looks like this changed for bug MTOMCAT-103:

os.putArchiveEntry( 
    new JarArchiveEntry( 
        StringUtils.removeStart( path, "/" ) + ".war" 
    ) 
);

which for configured <path>/<path> would translate to ".war" as file name looking way more legal but still not really desirable imho.

As the Tomcat 7 Documentation states the base file name for the empty context path (aka "/") should be ROOT your best bet is to choose <path>ROOT<path> to achieve the desired result.

Edit:

For reproducing this, I checked out tomcat-maven-plugin.git and used their integration test. I had some truble to get a functional build at all, bit after removing some stuff from their server.xml and changing tomcat7-maven-plugin version to 2.1 it worked:

This is the diff applyied to their integration test app:

diff --git a/tomcat7-maven-plugin/src/it/simple-war-exec-project/pom.xml b/tomcat7-maven-plugin/src/it/simple-war-exec-project/pom.xml
index 8ce51b7..e00f0ea 100644
--- a/tomcat7-maven-plugin/src/it/simple-war-exec-project/pom.xml
+++ b/tomcat7-maven-plugin/src/it/simple-war-exec-project/pom.xml
@@ -42,7 +42,7 @@
         <plugin>
           <groupId>org.apache.tomcat.maven</groupId>
           <artifactId>tomcat7-maven-plugin</artifactId>
-          <version>@pom.version@</version>
+          <version>2.1</version>
         </plugin>
       </plugins>
     </pluginManagement>
@@ -65,7 +65,7 @@
             </goals>
             <phase>package</phase>
             <configuration>
-              <path>foo</path>
+              <path>ROOT</path>
               <serverXml>src/main/tomcatconf/server.xml</serverXml>
             </configuration>
           </execution>
diff --git a/tomcat7-maven-plugin/src/it/simple-war-exec-project/src/main/tomcatconf/server.xml b/tomcat7-maven-plugin/src/it/simple-war-exec-project/src/main/tomcatconf/server.xml
index 76ab562..de086fc 100644
--- a/tomcat7-maven-plugin/src/it/simple-war-exec-project/src/main/tomcatconf/server.xml
+++ b/tomcat7-maven-plugin/src/it/simple-war-exec-project/src/main/tomcatconf/server.xml
@@ -19,14 +19,7 @@

 <Server port="8010" shutdown="SHUTDOWN">

-  <GlobalNamingResources>
-    <!-- Used by Manager webapp -->
-    <Resource name="UserDatabase" auth="Container"
-              type="org.apache.catalina.UserDatabase"
-       description="User database that can be updated and saved"
-           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
-          pathname="conf/tomcat-users.xml" />
-  </GlobalNamingResources>
+  

   <Service name="Catalina">
     <Connector port="8080" keepAliveTimeout="1800000" maxKeepAliveRequests="30000" maxThreads="300" />
@@ -34,8 +27,6 @@
     <Engine name="Catalina" defaultHost="localhost">
       <Valve className="org.apache.catalina.valves.AccessLogValve" 
              resolveHosts="false" pattern="%t-ip:%a-protocol:%H-localPort:%p-path:%U-time:%D ms"/> 
-      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
-             resourceName="UserDatabase" />
       <Host name="localhost" appBase="webapps" />
     </Engine>

Then from within the folder /tomcat-maven-plugin/tomcat7-maven-plugin/src/it/simple-war-exec-project I did:

$ mvn clean package
...
[INFO] BUILD SUCCESS
...
$ cd target/
$ java -jar simple-war-exec-project-1.0-SNAPSHOT-war-exec.jar
...
INFORMATION: Deploying web application archive C:\Temp\tomcat-maven-plugin\tomcat7-maven-plugin\src\it\simple-war-exec-project\target\.extract\webapps\ROOT.war
...

And the result:

enter image description here

Estimation answered 19/3, 2019 at 21:25 Comment(4)
Thanks for the answer Selaron. Unfortunately <path>ROOT<path> doesn't work to deploy in root context. I wonder if tomcat plugin should generate ROOT.war when path is "/" instead of restricting it.Raver
@Raver Using their simple integration test app as a minimal reproducible example it worked after applying some modifications. I used it the "build executable jar" way and it created a ROOT.war and deployed successfuly. Just see my edit above for details. Maybe in a more complex app there are other settings conflicting with <path>ROOT</path> ?Estimation
@Raver does it work now, did you find the root cause?Estimation
Using <path>ROOT</path> didn't work in my case. It still try to deploy in /ROOT and outputs an error.Raver

© 2022 - 2024 — McMap. All rights reserved.