2

我正在尝试将基于 JaveEE Tomcat7 的应用程序部署到 OpenShift。整个配置基于 Tomcat 7 (JBoss EWS 2.0) catridge。到目前为止,我已经在本地开发了我的应用程序,并且一切正常,包括基于 JDBCRealm 的用户身份验证。

项目规范,pom.xml 附在帖子末尾:

  • 完全基于 Maven
  • 通过工件org.apache.tomcat.maven使用的 tomcat

但是现在我需要让我的应用程序在网络上可用,所以我决定去 OpenShift,在那里我创建了 Tomcat7 应用程序(它也是基于 maven 的)。部署成功,但我现在遇到了本地工作的 JDBCRealm 问题。问题是 tomcat 无法找到 MySQL 驱动程序来打开领域连接。通过指定我的 context.xml 并添加 mysql 依赖项,我用org.apache.tomcat.maven解决了这个问题。但是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>

当然,我的 context.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&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"/>
</Context>
4

2 回答 2

1

感谢您在我们的论坛上发帖: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 ...

你能看看它,看看它是否有帮助?然后,如果您仍然无法连接,请告诉我们。

于 2013-04-01T06:21:35.673 回答
0

鉴于在 openshift 中您只能部署一个 webapp,您必须编辑全局context.xmlserver.xml以配置数据源。

此外,您还需要在您具有写入权限的地方添​​加数据库 jar 驱动程序,并且 tomcat 可以找到它。

在这里你可以展示如何。

于 2013-05-30T16:41:45.167 回答