title: OpenShift, Tomcat7 and JDBC Realm
body: <p>I am trying to deploy JaveEE Tomcat7 based application to OpenShift. Whole configuration is based on Tomcat 7 (JBoss EWS 2.0) catridge. Up to now I'd developed my application locally and all worked fine including JDBCRealm based user's authentication.</p>
  
  <p>Project spec, pom.xml attached at the end of the post:</p>
  
  <ul>
  <li>entirely maven based</li>
  <li>tomcat used via artifact <strong>org.apache.tomcat.maven</strong></li>
  </ul>
  
  <p>But now I need to make my app available in web, so I decided to go to OpenShift, where I created Tomcat7 application (which is also maven based). Deployment was successful but I am having trouble with JDBCRealm now, which worked locally. The problem is that tomcat fails to find MySQL driver to open realm connection. I'd solved this problem with <strong>org.apache.tomcat.maven</strong> by specifying my context.xml and adding mysql dependency. Howeever <strong>maven-war-plugin</strong> which seems to not have an option to inject the dependency. So i am stucked with an error like this:</p>
  
  <pre><code>mar 30, 2013 8:59:08 AM org.apache.catalina.realm.JDBCRealm authenticate
  SEVERE: Exception performing authentication
  java.sql.SQLException: com.mysql.jdbc.Driver
      at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:706)
      at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:352)
      at org.apache.catalina.authenticator.BasicAuthenticator.authenticate(BasicAuthenticator.java:158)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:188)
      at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:702)
      ... 14 more
  </code></pre>
  
  <p>and pom.xml like this, which does not do the job:</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;majekwms&lt;/groupId&gt;
      &lt;artifactId&gt;majekwms&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;name&gt;majekwms&lt;/name&gt;
  
      &lt;developers&gt;
          &lt;developer&gt;
              &lt;id&gt;kornicameister&lt;/id&gt;
              &lt;email&gt;kornicameister@gmail.com&lt;/email&gt;
              &lt;name&gt;Tomasz Trębski&lt;/name&gt;
          &lt;/developer&gt;
      &lt;/developers&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;eap&lt;/id&gt;
              &lt;url&gt;http://maven.repository.redhat.com/techpreview/all&lt;/url&gt;
              &lt;releases&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/releases&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;eap&lt;/id&gt;
              &lt;url&gt;http://maven.repository.redhat.com/techpreview/all&lt;/url&gt;
              &lt;releases&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/releases&gt;
              &lt;snapshots&gt;
                  &lt;enabled&gt;true&lt;/enabled&gt;
              &lt;/snapshots&gt;
          &lt;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
      &lt;properties&gt;
          &lt;maven.compiler.source&gt;1.6&lt;/maven.compiler.source&gt;
          &lt;maven.compiler.target&gt;1.6&lt;/maven.compiler.target&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;json-simple.version&gt;1.1.1&lt;/json-simple.version&gt;
  
          &lt;jdk.version&gt;1.7&lt;/jdk.version&gt;
          &lt;log4j.version&gt;1.2.17&lt;/log4j.version&gt;
          &lt;mysql.version&gt;5.1.9&lt;/mysql.version&gt;
          &lt;tomcat.maven.version&gt;2.0&lt;/tomcat.maven.version&gt;
          &lt;maven.compiler.version&gt;3.0&lt;/maven.compiler.version&gt;
          &lt;junit.version&gt;4.11&lt;/junit.version&gt;
          &lt;hibernate.version&gt;4.2.0.CR1&lt;/hibernate.version&gt;
          &lt;hibernate-annotations.version&gt;3.5.6-Final&lt;/hibernate-annotations.version&gt;
          &lt;hibernate-common-annotations.version&gt;3.3.0.ga&lt;/hibernate-common-annotations.version&gt;
          &lt;gson.version&gt;2.2.2&lt;/gson.version&gt;
          &lt;slf4j-log4j12.version&gt;1.7.2&lt;/slf4j-log4j12.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.jboss.spec&lt;/groupId&gt;
              &lt;artifactId&gt;jboss-javaee-6.0&lt;/artifactId&gt;
              &lt;version&gt;3.0.0.Final-redhat-1&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;${junit.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;${log4j.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;${slf4j-log4j12.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysql.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!--json--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.googlecode.json-simple&lt;/groupId&gt;
              &lt;artifactId&gt;json-simple&lt;/artifactId&gt;
              &lt;version&gt;${json-simple.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
              &lt;artifactId&gt;gson&lt;/artifactId&gt;
              &lt;version&gt;${gson.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--json--&gt;
  
          &lt;!--hibernate--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-ehcache&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-annotations&lt;/artifactId&gt;
              &lt;version&gt;${hibernate-annotations.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-c3p0&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!--hibernate--&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.0.1&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.2.1&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
              &lt;artifactId&gt;tomcat-servlet-api&lt;/artifactId&gt;
              &lt;version&gt;7.0.21&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;profiles&gt;
          &lt;profile&gt;
              &lt;!-- When built in OpenShift the 'openshift' profile will be used when
                  invoking mvn. --&gt;
              &lt;!-- Use this profile for any OpenShift specific customization your app
                  will need. --&gt;
              &lt;!-- By default that is to put the resulting archive into the 'webapps'
                  folder. --&gt;
              &lt;!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html --&gt;
              &lt;id&gt;openshift&lt;/id&gt;
              &lt;build&gt;
                  &lt;finalName&gt;majekwms&lt;/finalName&gt;
                  &lt;plugins&gt;
                      &lt;plugin&gt;
                          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                          &lt;version&gt;${maven.compiler.version}&lt;/version&gt;
                          &lt;configuration&gt;
                              &lt;source&gt;${jdk.version}&lt;/source&gt;
                              &lt;target&gt;${jdk.version}&lt;/target&gt;
                              &lt;encoding&gt;${project.build.sourceEncoding}&lt;/encoding&gt;
                          &lt;/configuration&gt;
                      &lt;/plugin&gt;
                      &lt;plugin&gt;
                          &lt;groupId&gt;org.apache.tomcat.maven&lt;/groupId&gt;
                          &lt;artifactId&gt;tomcat7-maven-plugin&lt;/artifactId&gt;
                          &lt;version&gt;${tomcat.maven.version}&lt;/version&gt;
                          &lt;configuration&gt;
                              &lt;url&gt;http://127.6.35.1:8080/MajekWMS/&lt;/url&gt;
                              &lt;mode&gt;both&lt;/mode&gt;
                              &lt;path&gt;/MajekWMS&lt;/path&gt;
                              &lt;uriEncoding&gt;UTF-8&lt;/uriEncoding&gt;
                              &lt;contextReloadable&gt;true&lt;/contextReloadable&gt;
                              &lt;contextFile&gt;src/main/tomcat/context.xml&lt;/contextFile&gt;
                          &lt;/configuration&gt;
                          &lt;dependencies&gt;
                              &lt;dependency&gt;
                                  &lt;groupId&gt;mysql&lt;/groupId&gt;
                                  &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
                                  &lt;version&gt;${mysql.version}&lt;/version&gt;
                              &lt;/dependency&gt;
                          &lt;/dependencies&gt;
                      &lt;/plugin&gt;
                      &lt;plugin&gt;
                          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                          &lt;version&gt;2.1.1&lt;/version&gt;
                          &lt;configuration&gt;
                              &lt;attachClasses&gt;true&lt;/attachClasses&gt;
                              &lt;classesClassifier&gt;classes&lt;/classesClassifier&gt;
                              &lt;containerConfigXML&gt;src/main/tomcat/context.xml&lt;/containerConfigXML&gt;
                              &lt;outputDirectory&gt;webapps&lt;/outputDirectory&gt;
                              &lt;warName&gt;ROOT&lt;/warName&gt;
                          &lt;/configuration&gt;
                      &lt;/plugin&gt;
                  &lt;/plugins&gt;
              &lt;/build&gt;
          &lt;/profile&gt;
      &lt;/profiles&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>And of course my context.xml, which seems to be detected and tomcat tries to read configuration from it, but since mysql driver is missing it fails with SQLException:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Context
          className="org.apache.catalina.core.StandardContext"
          reloadable="true"
          antiJARLocking="true"
          swallowOutput="true"&gt;
      &lt;Realm connectionName="user_name_here"
             connectionPassword="pass_here"
             connectionURL="jdbc:mysql://localhost:3306/majekwms?useUnicode=true&amp;amp;characterEncoding=utf8"
             roleNameCol="role"
             userCredCol="secPassword"
             userNameCol="login"
             userRoleTable="userRole"
             userTable="user"
             driverName="com.mysql.jdbc.Driver"
             digest="md5"
             className="org.apache.catalina.realm.JDBCRealm"/&gt;
  &lt;/Context&gt;
  </code></pre>