为什么不使用/作为Maven Tomcat中的上下文路径?

时间:2019-03-19 18:32:21

标签: maven tomcat tomcat7

我正在使用maven tomcat插件来部署应用程序。如果我将路径设置为<path>/<path>,它确实会在根上下文中部署,但是我想知道它是否会引起任何问题,因为the docs明确表示“ 请勿使用/ ”。

相关问题:How to set context path to root(“/”) in Tomcat 7.0 when using Maven

为什么不使用/作为Maven Tomcat中的路径?

1 个答案:

答案 0 :(得分:2)

此提交中引入了指向“请勿使用/”的JavaDoc:

(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

向下滚动提交时,您会看到path属性值用于创建JarArchiveEntry

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

已配置<path>/<path>

转换为文件名“ /.war”,看起来有点无效。另一方面,提取后可能会产生“ .war”文件名。

今天对于版本2.2,这段代码看起来像是针对错误MTOMCAT-103的更改:

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

对于配置了<path>/<path>的文件,它会转换为“ .war”,因为文件名看起来更合法,但仍然不是真正希望的恕我直言。

因为Tomcat 7 Documentation声明了空上下文路径的基本文件名 (又名“ /”)应为ROOT,最好的选择是选择<path>ROOT<path>以获得理想的结果。

编辑:

为重现此内容,我签出了tomcat-maven-plugin.git并使用了their integration test。在从他们的server.xml中删除了一些东西并将tomcat7-maven-plugin版本更改为2.1以后,我一点都不愿意获得功能构建。

这是应用于他们的集成测试应用程序的区别:

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>

然后从文件夹/tomcat-maven-plugin/tomcat7-maven-plugin/src/it/simple-war-exec-project中执行操作:

$ 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
...

结果:

enter image description here