java Maven 战争应用程序设置上下文根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38660897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
maven war application setting up contextroot
提问by krisdigitx
i am building a war application file using the below maven config, however when i start the application in tomcat the Context Root is set to "/CommerceApi-0.0.1-SNAPSHOT/"
我正在使用下面的 maven 配置构建一个War应用程序文件,但是当我在 tomcat 中启动应用程序时,上下文根设置为“/CommerceApi-0.0.1-SNAPSHOT/”
I want this to be set to "/api",
我希望将其设置为“/api”,
any ideas?, below is the pom.xml
有什么想法吗?下面是 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CommerceApi</groupId>
<artifactId>CommerceApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>CommerceApiCommon</groupId>
<artifactId>CommerceApiCommon</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
回答by Nikhil Bhide
There are three ways to do it:
有以下三种方法:
1. If you are not using Eclipse/MyEclipse to deploy the application onto application server -
1. 如果您不使用 Eclipse/MyEclipse 将应用程序部署到应用程序服务器上 -
You need to make use of maven-war plugin, you can specify warName in configuration section.
您需要使用 maven-war 插件,您可以在配置部分指定 warName。
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warName>customwarname</warName>
</configuration>
</plugin>
2. If you are using Eclipse/MyEclipse to deploy the application onto application server -
2. 如果您使用 Eclipse/MyEclipse 将应用程序部署到应用程序服务器上 -
If you are using eclipse and deploying war using eclipse then you can use following maven configuration.
如果您使用 eclipse 并使用 eclipse 部署 war,那么您可以使用以下 maven 配置。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
<configuration>
<wtpversion>2.0</wtpversion>
<wtpContextName>customwarname</wtpContextName>
</configuration>
</plugin>
Then, run following commands to update eclipse settings.
然后,运行以下命令来更新 eclipse 设置。
mvn eclipse:eclipse -Dwtpversion=2.0
Restart Eclipse and then navigate to project properties, Properties->Web to view the reflected changes in root-context value or navigate to Deployment Assembly of the project to view the changes
重新启动 Eclipse,然后导航到项目属性,Properties->Web 以查看根上下文值中反映的更改或导航到项目的部署程序集以查看更改
Note that above can be achieved using m2eclipse by adding a new plugin.
请注意,上面可以通过添加一个新插件使用 m2eclipse 来实现。
3. Application server specific:You should prefer to follow server agnostic approach, but if are required to do it then you can configure root context url in server specific configuration file. You can find detailed approach here
3. 特定于应用程序服务器:您应该更喜欢遵循与服务器无关的方法,但如果需要这样做,那么您可以在特定于服务器的配置文件中配置根上下文 url。你可以在这里找到详细的方法
回答by Matt
Your application is not in charge to define its own context path. That's task of the container, the Tomcat in your case. Tomcat offers several options of how to set the context path. You may define the context path it in a context file or specify the context path in the manager application. If you use Jenkins or other CI tools you'd be able to specify the context path there, as well.
您的应用程序不负责定义自己的上下文路径。这是容器的任务,在您的情况下是 Tomcat。Tomcat 提供了几个关于如何设置上下文路径的选项。您可以在上下文文件中定义上下文路径或在管理器应用程序中指定上下文路径。如果您使用 Jenkins 或其他 CI 工具,您也可以在那里指定上下文路径。
Best you read up on the options you have regarding your particular Tomcat version.
最好阅读有关特定 Tomcat 版本的选项。
回答by wemu
There are several options. Some are described in Define Servlet Context in WAR-File
有几种选择。在 WAR-File中定义 Servlet 上下文中描述了一些
Using tomcat you can also define the context.xml path: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#containerConfigXMLand maybe configure it in there: https://tomcat.apache.org/tomcat-7.0-doc/config/context.html
使用 tomcat,您还可以定义 context.xml 路径:http: //maven.apache.org/plugins/maven-war-plugin/war-mojo.html#containerConfigXML并可能在那里配置它:https://tomcat。 apache.org/tomcat-7.0-doc/config/context.html
the fastest way os probably to change the final name (see other stackoverflow question).
os 可能更改最终名称的最快方法(请参阅其他 stackoverflow 问题)。