【问题标题】:Why not to use / as a context path in maven tomcat?为什么不在 maven tomcat 中使用 / 作为上下文路径?
【发布时间】:2019-08-10 09:13:53
【问题描述】:

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

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

为什么不在maven tomcat中使用/作为路径?

【问题讨论】:

    标签: maven tomcat tomcat7


    【解决方案1】:

    在此提交中引入了指出“不要使用 /”的 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 声明了空上下文路径的基本文件名 (aka "/") 应该是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
    ...
    

    结果:

    【讨论】:

    • 感谢塞拉隆的回答。不幸的是,&lt;path&gt;ROOT&lt;path&gt; 无法在根上下文中部署。我想知道当路径为“/”时tomcat插件是否应该生成ROOT.war而不是限制它。
    • @TheKojuEffect 使用他们的simple integration test app 作为minimal reproducible example 在应用一些修改后它工作。我以“构建可执行 jar”的方式使用它,它创建了一个 ROOT.war 并成功部署。有关详细信息,请参见我上面的编辑。也许在更复杂的应用程序中还有其他设置与&lt;path&gt;ROOT&lt;/path&gt; 冲突?
    • @TheKojuEffect 现在可以用了吗,找到根本原因了吗?
    • 使用 &lt;path&gt;ROOT&lt;/path&gt; 在我的情况下不起作用。它仍然尝试在/ROOT 中部署并输出错误。
    猜你喜欢
    • 2018-11-18
    • 2022-01-17
    • 2011-10-22
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多