7.3.3. WAR deployment to WildFly

The WAR files with CUBA application can be deployed to the WildFly application server. An example below demonstrates how to deploy a CUBA application using PostgreSQL 9.6 to the WildFly 8.2 server on Windows.

  1. Assemble and deploy the project to the default Tomcat server in order to get all necessary dependencies locally.

  2. Configure the application home directory for the application:

    • Create a folder that will be fully available for WildFly server’s process. For example: C:\Users\UserName\app_home.

    • Copy the logback.xml file from tomcat/conf to this folder and edit the logDir property:

    <property name="logDir" value="${app.home}/logs"/>
  3. Configure the WildFly server

    • Install WildFly to a local folder, for example, to C:\wildfly.

    • Edit the C:\wildfly\bin\standalone.conf.bat file and add the following line to the end of the file:

    set "JAVA_OPTS=%JAVA_OPTS% -Dapp.home=%USERPROFILE%/app_home -Dlogback.configurationFile=%USERPROFILE%/app_home/logback.xml"

    Here we define the app.home system property with the application home directory and configure the logging by setting the path to the logback.xml file. You can also use an absolute path instead of %USERPROFILE% variable.

    • Compare the Hibernate Validator versions in WildFly and CUBA application (normally, the platform uses a newer version). Replace the C:/wildfly/modules/system/layers/base/org/hibernate/validator/main/hibernate-validator-x.y.z-sometext.jar with the newer file from tomcat/shared/lib, for example, hibernate-validator-5.4.1.Final.jar.

    • Update the JAR file version number in the /wildfly/modules/system/layers/base/org/hibernate/validator/main/module.xml file.

    • To register PostgreSQL driver in WildFly, copy the postgresql-9.4-1201-jdbc41.jar from tomcat/lib to C:\wildfly\standalone\deployments.

      Tip

      If you use WildFly 11, in order to install PostgreSQL driver, you should also modify your module.xml file in the following way:

      <module xmlns="urn:jboss:module:1.1" name="org.postgresql">
      
          <resources>
              <resource-root path="postgresql-9.4.1212.jar"/>
          </resources>
      
          <dependencies>
              <module name="javax.api"/>
              <module name="javax.transaction.api"/>
          </dependencies>
      </module>

      Then you should run jboss-cli from the bin folder and run the following command:

      /subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql, driver-module-name=org.postgresql, driver-class-name=org.postgresql.Driver)
  4. Create JDBC Datasource

    • Start WildFly by running standalone.bat

    • Open the administration console on http://localhost:9990. The first time you log in, you will be asked to create a user and a password.

    • Open the Configuration - Subsystems - Datasources tab and create a new datasource for your application:

    Name: Cuba
    JNDI Name: java:/jdbc/CubaDS
    JDBC Driver: postgresql
    Connection URL: your database URL
    Username: your database username
    Password: your database password

    The JDBC driver will be available on the list of detected drivers if you have copied postgresql-x.y.z.jar as described above.

    Check the connection by clicking the Test connection button.

    • Activate the datasource.

  5. Build the application

    • Open Deployment settings > WAR in Studio.

    • Check Build WAR checkbox.

    • Set ${app.home} in the Application home directory field.

    • Check Include JDBC driver checkbox.

    • Save the settings.

    • Open build.gradle in IDE and add the doAfter property to the buildWar task. This property will copy the WildFly deployment descriptor:

      task buildWar(type: CubaWarBuilding) {
          includeJdbcDriver = true
          appProperties = ['cuba.automaticDatabaseUpdate' : true]
          singleWar = false
          appHome = '${app.home}'
          doAfter = {
              copy {
                  from 'jboss-deployment-structure.xml'
                  into "${project.buildDir}/tmp/core/war/META-INF/"
              }
              copy {
                  from 'jboss-deployment-structure.xml'
                  into "${project.buildDir}/tmp/web/war/META-INF/"
              }
          }
      }
      Tip

      For a singleWAR configuration the task will be different:

      task buildWar(type: CubaWarBuilding) {
          webXmlPath = 'modules/web/web/WEB-INF/single-war-web.xml'
          appProperties = ['cuba.automaticDatabaseUpdate' : true]
          includeJdbcDriver = true
          appHome = '${app.home}'
          doAfter = {
              copy {
                  from 'jboss-deployment-structure.xml'
                  into "${project.buildDir}/tmp/war/META-INF/"
              }
          }
      }

      If your project also contains a Polymer module, add the following configuration to your single-war-web.xml file:

      <servlet>
          <servlet-name>default</servlet-name>
          <init-param>
              <param-name>resolve-against-context-root</param-name>
              <param-value>true</param-value>
          </init-param>
      </servlet>
    • In the project root folder, create the jboss-deployment-structure.xml file and add the WildFly deployment descriptor to it:

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
        <deployment>
            <exclusions>
                <module name="org.apache.commons.logging" />
                <module name="org.apache.log4j" />
                <module name="org.jboss.logging" />
                <module name="org.jboss.logging.jul-to-slf4j-stub" />
                <module name="org.jboss.logmanager" />
                <module name="org.jboss.logmanager.log4j" />
                <module name="org.slf4j" />
                <module name="org.slf4j.impl" />
                <module name="org.slf4j.jcl-over-slf4j" />
            </exclusions>
        </deployment>
    </jboss-deployment-structure>
    • Run the buildWar task to create WAR files.

  6. Copy the files app-core.war and app.war from build\distributions\war to WildFly directory C:\wildfly\standalone\deployments.

  7. Restart the WildFLy server.

  8. Your application will become available on http://localhost:8080/app. The log files will be saved in the application home: C:\Users\UserName\app_home\logs.