我正在尝试部署基于JaveEE Tomcat7的应用程序到OpenShift。整个配置基于Tomcat 7 (JBoss EWS 2.0)架构。到目前为止,我已经在本地开发了我的应用程序,包括基于JDBCRealm的用户身份验证,所有工作都很好。
项目规范,pom.xml附在文章末尾:
- 完全基于maven的
但是现在我需要让我的应用程序在web上可用,所以我决定去OpenShift,在那里我创建了Tomcat7应用程序(这也是基于maven的)。部署是成功的,但是我现在遇到了JDBCRealm的问题,它在本地工作。问题是tomcat找不到MySQL驱动程序打开领域连接。我用org.apache.tomcat解决了这个问题。通过指定my context.xml并添加mysql依赖项。但是maven-war-plugin似乎没有一个选项来注入依赖。所以我遇到了这样的错误:
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
和pom.xml,像这样,这不起作用:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>majekwms</groupId>
<artifactId>majekwms</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>majekwms</name>
<developers>
<developer>
<id>kornicameister</id>
<email>kornicameister@gmail.com</email>
<name>Tomasz Trębski</name>
</developer>
</developers>
<repositories>
<repository>
<id>eap</id>
<url>http://maven.repository.redhat.com/techpreview/all</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>eap</id>
<url>http://maven.repository.redhat.com/techpreview/all</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<json-simple.version>1.1.1</json-simple.version>
<jdk.version>1.7</jdk.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.1.9</mysql.version>
<tomcat.maven.version>2.0</tomcat.maven.version>
<maven.compiler.version>3.0</maven.compiler.version>
<junit.version>4.11</junit.version>
<hibernate.version>4.2.0.CR1</hibernate.version>
<hibernate-annotations.version>3.5.6-Final</hibernate-annotations.version>
<hibernate-common-annotations.version>3.3.0.ga</hibernate-common-annotations.version>
<gson.version>2.2.2</gson.version>
<slf4j-log4j12.version>1.7.2</slf4j-log4j12.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>3.0.0.Final-redhat-1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--json-->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>${json-simple.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<!--json-->
<!--hibernate-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>${hibernate-annotations.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<!--hibernate-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.21</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<!-- When built in OpenShift the 'openshift' profile will be used when
invoking mvn. -->
<!-- Use this profile for any OpenShift specific customization your app
will need. -->
<!-- By default that is to put the resulting archive into the 'webapps'
folder. -->
<!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
<id>openshift</id>
<build>
<finalName>majekwms</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.maven.version}</version>
<configuration>
<url>http://127.6.35.1:8080/MajekWMS/</url>
<mode>both</mode>
<path>/MajekWMS</path>
<uriEncoding>UTF-8</uriEncoding>
<contextReloadable>true</contextReloadable>
<contextFile>src/main/tomcat/context.xml</contextFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
<containerConfigXML>src/main/tomcat/context.xml</containerConfigXML>
<outputDirectory>webapps</outputDirectory>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
当然我的上下文。xml,这似乎被检测到,tomcat试图从它读取配置,但由于mysql驱动程序丢失它失败与SQLException:
<?xml version="1.0" encoding="UTF-8"?>
<Context
className="org.apache.catalina.core.StandardContext"
reloadable="true"
antiJARLocking="true"
swallowOutput="true">
<Realm connectionName="user_name_here"
connectionPassword="pass_here"
connectionURL="jdbc:mysql://localhost:3306/majekwms?useUnicode=true&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"/>
</Context>
感谢您在我们的论坛发帖:https://www.openshift.com/forums/openshift/openshift-tomcat7-and-jdbc-realm#comment-28951
这里有一个论坛讨论这个话题:https://www.openshift.com/kb/kb-e1040-using-your-own-jar-files-in-a-java-applica…
你能帮我看一下吗?然后,如果您仍然无法连接,请告诉我们。
考虑到在openshift中你只能部署一个web应用程序,你必须编辑全局context.xml和server.xml来配置数据源。
你还需要添加数据库jar驱动程序在你有写权限的地方,tomcat可以找到它。