bash 如何通过终端命令将 Web 应用程序部署到 tomcat 应用程序服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19276753/
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
How can I deploy a web application to tomcat application server via Terminal command
提问by AspiringGeek
I am currently testing a large number of webservices. I would like to deploy and undeploy to tomcat application server via terminal command as fast as I can. Using the HTML GUI would not be reasonable for the large number of webservices that I need to deploy. Can anyone assist me, in how to deploy via a terminal command?
我目前正在测试大量的网络服务。我想尽可能快地通过终端命令部署和取消部署到 tomcat 应用程序服务器。对于我需要部署的大量 Web 服务,使用 HTML GUI 是不合理的。任何人都可以帮助我,如何通过终端命令进行部署?
Furthermore, I am writing a ash script that automates the deployment process, so perhaps if someone can give me some some direction it would be great.
此外,我正在编写一个自动化部署过程的 ash 脚本,所以也许如果有人能给我一些指导,那就太好了。
Ideally, I am looking to do something like this on the command line:
理想情况下,我希望在命令行上执行以下操作:
TOMCAT --parameter Specify path to WAR file --parameter2 --specify some sort of config file
TOMCAT --parameter 指定 WAR 文件的路径 --parameter2 --specify 某种配置文件
回答by cosbor11
First you need to make sure that tomcat-user.xmlis configured with the correct users and roles. The minimum role configuration is "admin,manager-script":
首先,您需要确保tomcat-user.xml配置了正确的用户和角色。最低角色配置是“admin,manager-script”:
- Find out where tomcat is installed. You can do this in bash:
catalina version
- Navigate to the CATALINA_HOME config directory which is displayed in the output from the previous command.
- 找出tomcat的安装位置。您可以在 bash 中执行此操作:
catalina version
- 导航到 CATALINA_HOME 配置目录,该目录显示在上一个命令的输出中。
cd /usr/local/Cellar/tomcat/8.0.22/libexec/conf
- Note: In my example, I used homebrew to install tomcat 8, replace this path with whatever is displayed in your command line output.
- 注意:在我的示例中,我使用自制软件来安装 tomcat 8,将此路径替换为命令行输出中显示的任何内容。
- Verify that the server.xmlcontains a UserDatabaseresource (if it doesn't add it):
- 验证server.xml 是否包含UserDatabase资源(如果未添加):
<GlobalNamingResources>
<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>
By default tomcat uses an in-memory database to store users and roles. This is configured in the conf/server.xml. And delegates user & role declaration to the conf/tomcat-users.xml file. See: http://tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#UserDatabaseRealmfor more information.
默认情况下,tomcat 使用内存数据库来存储用户和角色。这是在 conf/server.xml 中配置的。并将用户和角色声明委托给 conf/tomcat-users.xml 文件。请参阅:http: //tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#UserDatabaseRealm了解更多信息。
- Then verify that config/user-tomcat.xmlexists and looks like this:
- 然后验证config/user-tomcat.xml 是否存在,如下所示:
<tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd">
<role rolename="admin"/>
<role rolename="manager-script"/>
<user username="admin" roles="admin,manager-script" password="admin" />
</tomcat-users>
Now, you're ready to deploy a war!
现在,您已准备好部署War!
Here are two ways to do that...
这里有两种方法可以做到这一点......
Using wget:
使用 wget:
wgetis a nifty tool that lets you do http requests via the command line. I recommend installing it using a package manager like homebrew, otherwise, you can install using this wget install guide.
wget是一个漂亮的工具,可以让您通过命令行执行 http 请求。我建议使用像homebrew这样的包管理器安装它,否则,你可以使用这个wget install guide进行安装。
- First restart your server to pick up any configuration changes that you may have done.
catalina stop
, thencatalina start
, will do the trick from bash.
*Note: When using wget to start deploy a war to tomcat, tomcat needs to be started first. - Now execute the following command:
- 首先重新启动您的服务器以获取您可能已完成的任何配置更改。
catalina stop
, 然后catalina start
, 将通过 bash 来解决问题。
*注意:使用wget开始部署war到tomcat时,需要先启动tomcat。 - 现在执行以下命令:
wget --http-user=admin --http-password=admin "http://localhost:8080/manager/text/deploy?war=file:/Users/yourusername/app/target/APP-1.0.0.war&path=/app"
- Now go to http://localhost:8080/app/to see your webapp.
- 现在去http://localhost:8080/app/查看你的 web 应用程序。
Notes:
笔记:
Wait a few seconds to let the http request to send and fully deploy the war. (sometimes this takes a while).
You may be tempted to reference the war file using the home directory shortcut like this
file:~/app/target/APP-1.0.0.war
, but that won't work.To undeploy the war simply replace
deploy
withundeploy
in the wget command.
等待几秒钟,让 http 请求发送并完全部署War。(有时这需要一段时间)。
您可能会想像这样使用主目录快捷方式来引用 war 文件
file:~/app/target/APP-1.0.0.war
,但那是行不通的。要取消部署War,只需在 wget 命令中替换
deploy
为undeploy
。
Using the Tomcat Maven Plugin:
使用 Tomcat Maven 插件:
If you have the source code, you can build and deploy the war yourself very easily with the tomcat7-maven-plugin. Note: At the time I wrote this there was no tomcat8-maven-plugin; the tomcat-7-maven-plugin works just fine for tomcat 8.
如果你有源代码,你可以很容易地使用tomcat7-maven-plugin自己构建和部署War。注意:在我写这篇文章的时候还没有 tomcat8-maven-plugin;tomcat-7-maven-plugin 适用于 tomcat 8。
- Add the plugin to the pom.xml:
- 将插件添加到 pom.xml 中:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${version.tomcat.maven.plugin}</version>
<configuration>
<path>/${project.build.finalName}</path>
<configurationDir>${env.CATALINA_HOME}</configurationDir>
<additionalConfigFilesDir>${env.CATALINA_HOME}/conf</additionalConfigFilesDir>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
</configuration>
</plugin>
- Navigate to your project root, where the pom.xmlis, and run the following command:
mvn tomcat7:run
- 导航到pom.xml所在的项目根目录,然后运行以下命令:
mvn tomcat7:run
回答by anubhava
To deploy a WAR from the command line you can use wget (or curl)
要从命令行部署 WAR,您可以使用 wget(或 curl)
wget "http://localhost:8080/manager/text/deploy?war=file:/path/to/MyWar.war&path=/MyApp" -O -
回答by Christopher Schultz
Take a look at Tomcat's manager webapp. You can use the "text" interface to do things from the command-line. Tomcat even comes with some Apache Ant tasks that can deploy, undeploy, etc. for you.
看看 Tomcat 的manager webapp。您可以使用“文本”界面从命令行执行操作。Tomcat 甚至带有一些 Apache Ant 任务,可以为您部署、取消部署等。