简体   繁体   English

将战争应用程序名称与战争文件名分开

[英]Separating war application name from war file name

Currently if i deploy a war file on tomcat named say myapp.war , I can access its url by http://localhost/myapp/MyServlet .目前,如果我在 tomcat 上部署一个名为myapp.war的战争文件,我可以通过http://localhost/myapp/MyServlet 访问它的 url

However what I want is to deploy a war with a version number in the war file name and still have the same url.但是我想要的是部署一个战争文件名中带有版本号的战争,并且仍然具有相同的 url。 For eg, I want to deploy myapp-1.1.0.war and still have the url be http://localhost/myapp/MyServlet例如,我想部署myapp-1.1.0.war并且仍然让 url 成为http://localhost/myapp/MyServlet

Of course I need to keep updating the war and the version number will keep changing, so I cant hardcode the war filename anywhere.当然我需要不断更新战争,版本号会不断变化,所以我不能在任何地方硬编码战争文件名。 Is there any setting in web.xml I can use to keep the same url for the app regardless of the war filename? web.xml中是否有任何设置可以用来为应用程序保留相同的 url,而不管战争文件名如何?

The solution is to stop using the automatic deployment feature of Tomcat, which takes the shortcut of setting the "context name" (the /myapp part of the URL) to the portion of the WAR filename before ".war".解决方法是停止使用 Tomcat 的自动部署功能,该功能采用将“上下文名称”(URL 的/myapp部分)设置为“.war”之前的 WAR 文件名部分的快捷方式。

Instead, extract the WAR contents to the filesystem yourself and setup an XML file at TOMCAT_HOME/conf/[enginename]/[hostname]/[contextname].xml which points the desired context path (such as /myapp ) to the location of the application on disk (such as /opt/webapps/myapp-1.1.0/ ).相反,自己将 WAR 内容提取到文件系统并在TOMCAT_HOME/conf/[enginename]/[hostname]/[contextname].xml中设置一个 XML 文件,它将所需的上下文路径(例如/myapp )指向磁盘上的应用程序(例如/opt/webapps/myapp-1.1.0/ )。

The Tomcat reference docs provide a good explanation of how Tomcat deploys applications automatically, and how you can configure customized logic for the mapping of context path to application file location (there are a handful of alternate ways to set this up other than the one I suggest above). Tomcat 参考文档很好地解释了 Tomcat 如何自动部署应用程序,以及如何配置自定义逻辑以将上下文路径映射到应用程序文件位置(除了我建议的方法之外,还有一些替代方法可以设置它以上)。

You can use YOUR_WAR/META-INF/context.xml for this.您可以为此使用 YOUR_WAR/META-INF/context.xml。 Here is a sample:这是一个示例:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/MyServlet"/>

When using Maven you can control your deployment's path by doing the followings:使用Maven时,您可以通过执行以下操作来控制部署路径:

Tomcat's conf/tomcat-users.xml: Tomcat的conf/tomcat-users.xml:

<tomcat-users>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <role rolename="manager-jmx"/>
  <role rolename="manager-status"/>
  <role rolename="admin-gui"/>
  <role rolename="admin-script"/>

  <user username="root" password="root" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

</tomcat-users>

~/.m2/settings.xml: ~/.m2/settings.xml:

...
<server>
  <id>tomcat</id>
  <username>root</username>
  <password>root</password>
</server>
...

pom.xml: pom.xml:

...
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myapp</artifactId>
  <version>1.1.0</version>
  <packaging>war</packaging>
...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
          <!-- neglect /html below Tomcat7: -->
          <url>http://server:8080/manager/html</url>
          <!-- Refer to the server settings in your ~/.m2/settings.xml -->
          <server>tomcat</server>
          <path>/myWebApp</path>
        </configuration>
      </plugin>
      ....
    </plugins>
  </build>
...

Start your tomcat first Then build and deploy your application..首先启动您的 tomcat 然后构建和部署您的应用程序..

mvn clean install tomcat:deploy

..it will be accessible under http://server:8080/myWebApp ..它可以在http://server:8080/myWebApp下访问

I prefer to use " ## " symbols for noting version of *.war files in the tomcat.我更喜欢使用“ ## ”符号来标注 tomcat 中 *.war 文件的版本。
For example:例如:
myapp.war -> URL: http://localhost:8080/myapp/MyServlet myapp.war -> URL: http://localhost:8080/myapp/MyServlet
myapp##1.1.0 -> URL: http://localhost:8080/myapp/MyServlet (still the same, because all symbols after " ## " are ignoring by tomcat) myapp##1.1.0 -> URL: http://localhost:8080/myapp/MyServlet (还是一样,因为“ ## ”之后的所有符号都被tomcat忽略了)

There is no setting in web.xml for this. web.xml 中没有为此设置。 I do not believe that it is possible to set this inside the war file in a cross-container way - there is no mention of it in the spec anyway - so each container does it differently.我不相信有可能以跨容器的方式在war文件中设置它——无论如何在规范中都没有提到它——所以每个容器的做法都不同。 jboss-web.xml , sun-web.xml , context.xml etc. jboss-web.xmlsun-web.xmlcontext.xml等。

I run into the same issue, and indeed, as @matt mentioned, the Tomcat reference docs provide a good explanation of how Tomcat deploys applications automatically, and how you can configure customized logic for the mapping of context path to application file location.我遇到了同样的问题,事实上,正如@matt提到的, Tomcat 参考文档很好地解释了 Tomcat 如何自动部署应用程序,以及如何配置自定义逻辑以将上下文路径映射到应用程序文件位置。

In my case, I used this advice (in 'path' explanation):就我而言,我使用了这个建议(在“路径”解释中):

Even when statically defining a Context in server.xml, this attribute (/path) must not be set unless either the docBase is not located under the Host's appBase or both deployOnStartup and autoDeploy are false .即使在 server.xml 中静态定义 Context 时,也不得设置此属性 (/path),除非 docBase 不在主机的 appBase 下,或者deployOnStartup 和 autoDeploy 均为 false If this rule is not followed, double deployment is likely to result.如果不遵守此规则,很可能导致双重部署。

so in my case, I switched both deployOnStartup and autoDeploy to false, so my WAR (egaWAR) was not auto-exploded to 'a' directory under webapps, but instead into 'b' directory, due to these settings:所以在我的情况下,我将deployOnStartupautoDeploy都切换为 false,所以我的 WAR(egaWAR)没有自动展开到 webapps 下的“a”目录,而是因为这些设置而进入“b”目录:

<Host name="localhost"  appBase="webapps"
            autoDeploy="false" deployOnStartup="false" 
            unpackWARs="true" deployIgnore="${ignore.context}">

   <Context docBase="a" path="/b" />

</Host>

I found all the advice on this subject somewhat terse and confusing.我发现关于这个主题的所有建议都有些简洁和混乱。 The below method works for me using a default tomcatee installation.以下方法适用于我使用默认的 tomcatee 安装。

Suppose I have a war file named mywar.war.假设我有一个名为 mywar.war 的战争文件。 This holds a servlet mapping to '/hello'.这包含一个到“/hello”的 servlet 映射。 I want to access this servlet from a url like localhost:8080/myapp/hello我想从像 localhost:8080/myapp/hello 这样的 url 访问这个 servlet

This method avoids any changes to server.xml which is not advised as this requires tomcat restarts.此方法可避免对 server.xml 进行任何更改,不建议这样做,因为这需要重新启动 tomcat。

This method will fail is you deploy to /webapps as it will try and deploy the same war twice.如果您部署到 /webapps,此方法将失败,因为它将尝试部署相同的战争两次。 Therefore I start by creating a new directory to deploy into.因此,我首先创建一个要部署到的新目录。 I use /apps but the dir name is arbitrary.我使用 /apps 但目录名称是任意的。

I now copy mywar.war into apps.我现在将 mywar.war 复制到应用程序中。 Tomcat ignores this action. Tomcat 忽略此操作。 I now create a simple deployment file named myapp.xml.我现在创建一个名为 myapp.xml 的简单部署文件。 Note that the name of this file defines the URL context the war will be deployed under.请注意,此文件的名称定义了将部署战争的 URL 上下文。 I believe you can use ROOT.xml to deploy under.相信你可以使用ROOT.xml来部署下。 The contents are as follows:内容如下:

As described this needs to be copied into /conf/Catalina/localhost This causes tomcat to expand and deploy the /apps/mywar.war and set the correct URL context.如上所述,这需要复制到 /conf/Catalina/localhost 这会导致 tomcat 扩展和部署 /apps/mywar.war 并设置正确的 URL 上下文。 The servlet is now available on localhost://myapp/hello (or:8080//myapp/hello from a remote client). servlet 现在可以在 localhost://myapp/hello (或远程客户端:8080//myapp/hello)上使用。

Note that for a relative address the docBase URL identifies the location of the war from the webapps directory - hence the leading..请注意,对于相对地址,docBase URL 从 webapps 目录中识别战争的位置 - 因此前导..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM