且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过终端命令将Web应用程序部署到tomcat应用程序服务器

更新时间:2023-08-28 11:11:16

首先,您需要确保

First you need to make sure that tomcat-user.xml is configured with the correct users and roles. The minimum role configuration is "admin,manager-script":

  1. 找出tomcat的安装位置.您可以在bash中执行此操作: catalina版本
  2. 导航到CATALINA_HOME配置目录,该目录显示在上一条命令的输出中.

cd /usr/local/Cellar/tomcat/8.0.22/libexec/conf

  • 注意:在我的示例中,我使用自制软件安装了tomcat 8,用您的命令行输出中显示的内容替换了此路径.

  1. 验证 server.xml 是否包含 UserDatabase 资源(如果未添加):
  1. Verify that the server.xml contains a UserDatabase resource (if it doesn't add it):

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

默认情况下,tomcat使用内存数据库存储用户和角色.这是在conf/server.xml中配置的.并委派用户&conf/tomcat-users.xml文件中的角色声明.请参阅: http://tomcat.apache.org/tomcat-8.0-doc/realm-howto.html#UserDatabaseRealm 了解更多信息.

  1. 然后确认 config/user-tomcat.xml 存在并且看起来像这样:
  1. Then verify that config/user-tomcat.xml exists and looks like this:

  <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!

有两种方法可以做到这一点...

Here are two ways to do that...

使用wget:

wget 是一个漂亮的工具,可让您通过命令行执行http请求.我建议使用类似 homebrew 的包管理器进行安装,否则,您可以使用此

wget is 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.

  1. 首先重新启动服务器,以获取可能已进行的所有配置更改. catalina stop ,然后 catalina start ,将通过bash解决问题.
    *注意:使用wget开始向tomcat部署战争时,需要先启动tomcat.
  2. 现在执行以下命令:
  1. First restart your server to pick up any configuration changes that you may have done. catalina stop, then catalina start, will do the trick from bash.
    *Note: When using wget to start deploy a war to tomcat, tomcat needs to be started first.
  2. Now execute the following command:

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"

  • 现在转到 http://localhost:8080/app/来查看您的Web应用.
    • Now go to http://localhost:8080/app/ to see your webapp.
    • 注释:

      • 请等待几秒钟,以使http请求发送并完全部署该战争.(有时需要一段时间).

      • Wait a few seconds to let the http request to send and fully deploy the war. (sometimes this takes a while).

      您可能会喜欢使用主目录快捷方式(例如 file:〜/app/target/APP-1.0.0.war )引用war文件,但这不会工作.

      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.

      要取消部署战争,只需在wget命令中将 deploy 替换为 undeploy .

      To undeploy the war simply replace deploy with undeploy in the wget command.

      使用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.

  1. 将插件添加到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>

  1. 导航到项目根目录( pom.xml 所在的位置),然后运行以下命令: mvn tomcat7:run