5.3.2. 部署 WAR 至 Jetty

以下是一个部署 WAR 包到 Jetty web 服务器的示例,假设应用程序使用的是 PostgreSQL 数据库。

  1. 使用 Studio 中的 Deployment > WAR settings 界面或者手动在 build.gradle 末尾添加 buildWar 任务。

    1. task buildWar(type: CubaWarBuilding) {
    2. appHome = '${app.home}'
    3. appProperties = ['cuba.automaticDatabaseUpdate': 'true']
    4. singleWar = false
    5. }

    需要注意的是,这里给 Middleware 和 web 客户端构建了单独的两个 WAR 文件。

  2. 从命令行启动 buildWar 任务(假设已经预先创建了 Gradle wrapper):

    1. gradlew buildWar

    如果成功的话,会在项目的 build\distributions\war 目录创建 app-core.warapp.war

  3. 创建一个应用程序主目录目录,比如,c:\work\app_home

  4. 下载并安装 Jetty 到本地目录,比如 c:\work\jetty-home。本示例使用 jetty-distribution-9.3.6.v20151106.zip 测试通过。

  5. 创建 c:\work\jetty-base 目录,并且在这个目录打开命令行窗口执行以下命令:

    1. java -jar c:\work\jetty-home\start.jar --add-to-start=http,jndi,deploy,plus,ext,resources
  6. 创建 c:\work\jetty-base\app-jetty.xml 文件,添加以下内容(假设使用名为 test PostgreSQL 数据库):

    1. <?xml version="1.0"?>
    2. <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
    3. <Configure id="Server" class="org.eclipse.jetty.server.Server">
    4. <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
    5. <Arg></Arg>
    6. <Arg>jdbc/CubaDS</Arg>
    7. <Arg>
    8. <New class="org.postgresql.ds.PGSimpleDataSource">
    9. <Set name="ServerName">localhost</Set>
    10. <Set name="PortNumber">5432</Set>
    11. <Set name="DatabaseName">test</Set>
    12. <Set name="User">cuba</Set>
    13. <Set name="Password">cuba</Set>
    14. </New>
    15. </Arg>
    16. </New>
    17. </Configure>

    MS SQL 数据库的 app-jetty.xml 文件需要使用下面这个模板:

    1. <?xml version="1.0"?>
    2. <!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure_9_0.dtd">
    3. <Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
    4. <New id="CubaDS" class="org.eclipse.jetty.plus.jndi.Resource">
    5. <Arg/>
    6. <Arg>jdbc/CubaDS</Arg>
    7. <Arg>
    8. <New class="org.apache.commons.dbcp2.BasicDataSource">
    9. <Set name="driverClassName">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
    10. <Set name="url">jdbc:sqlserver://server_name;databaseName=db_name</Set>
    11. <Set name="username">username</Set>
    12. <Set name="password">password</Set>
    13. <Set name="maxIdle">2</Set>
    14. <Set name="maxTotal">20</Set>
    15. <Set name="maxWaitMillis">5000</Set>
    16. </New>
    17. </Arg>
    18. </New>
    19. </Configure>
  7. 或许(比如对于 MS SQL 数据库),需要下载以下这些 JAR 并且添加到 c:\work\jetty-base\lib\ext 目录。

    1. commons-pool2-2.4.2.jar
    2. commons-dbcp2-2.1.1.jar
    3. commons-logging-1.2.jar
  8. 将下面这些添加到 c:\work\jetty-base\start.ini 文件的开头:

    1. --exec
    2. -Xdebug
    3. -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n
    4. -Dapp.home=c:\work\app_home
    5. -Dlogback.configurationFile=c:\work\app_home\logback.xml
    6. # ---------------------------------------
    7. app-jetty.xml
  9. 拷贝数据库的 JDBC 驱动到 c:\work\jetty-base\lib\ext 目录。可以从 CUBA Studio 的 lib 目录或者项目的 build\tomcat\lib 目录拷贝这些驱动。比如对于 PostgreSQL,驱动文件是 postgresql-9.1-901.jdbc4.jar

  10. 拷贝 WAR 文件到 c:\work\jetty-base\webapps 目录。

  11. c:\work\jetty-base 目录打开命令行窗口并且执行:

    1. java -jar c:\work\jetty-home\start.jar
  12. 在浏览器打开 http://localhost:8080/app