5.3.5. 部署 WAR 至 Tomcat Linux 服务

以下示例在 Ubuntu 16.04 测试通过

  • build.gradle 末尾添加 buildWar 任务。可以指定不同的 context.xml 文件来设置生产环境数据库连接:
  1. task buildWar(type: CubaWarBuilding) {
  2. appHome = '${catalina.base}/work'
  3. singleWar = true
  4. includeContextXml = true
  5. includeJdbcDriver = true
  6. appProperties = ['cuba.automaticDatabaseUpdate': true]
  7. webXmlPath = 'modules/web/web/WEB-INF/single-war-web.xml'
  8. coreContextXmlPath = 'modules/core/web/META-INF/war-context.xml'
  9. }

如果目标 Tomcat 服务的参数跟快速部署里用到的本地 Tomcat 的参数不同,需要提供相应的应用程序属性。比如,如果目标 Tomcat 运行在 9999 端口,任务定义会是这样:

  1. task buildWar(type: CubaWarBuilding) {
  2. appHome = './app_home'
  3. singleWar = false
  4. includeContextXml = true
  5. includeJdbcDriver = true
  6. appProperties = [
  7. 'cuba.automaticDatabaseUpdate': true,
  8. 'cuba.webPort': 9999,
  9. 'cuba.connectionUrlList': 'http://localhost:9999/app-core'
  10. ]
  11. }
  • 执行 buildWar Gradle 任务。会在项目 build/distributions 目录生成 app.war 文件。
  1. gradlew buildWar
  • 安装 Tomcat 8 Linux Service:
  1. sudo apt-get install tomcat8
  • 拷贝项目生成的 app.war 文件到 Tomcat 服务的 /var/lib/tomcat8/webapps 目录。

Tomcat 服务默认是使用 tomcat8 用户来启动的。所以 webapps 目录的所有者也是 tomcat8

  • 创建配置文件 /usr/share/tomcat8/bin/setenv.sh 包含以下设置:
  1. export CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m"
  • 重启 Tomcat 服务:
  1. sudo service tomcat8 restart