我一直在使用tomcat7-maven- plugin。我想运行通过使用嵌入式tomcat连接到PostgreSQL数据库的webapp。这是我的POM文件的相关部分:
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <executions> <execution> <id>tomcat-run</id> <goals> <goal>exec-war-only</goal> </goals> <phase>package</phase> <configuration> <path>/</path> <attachArtifactClassifierType>war</attachArtifactClassifierType> <enableNaming>true</enableNaming> <extraDependencies> <extraDependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>8.4-701.jdbc4</version> </extraDependency> </extraDependencies> </configuration> </execution> </executions>
执行tomcat7:run失败并显示
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50) at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244) at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:247) at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:236) ... 29 more
依赖项本身是正确的(http://repo1.maven.org/maven2/postgresql/postgresql/8.4-701.jdbc4/)。
我使用Maven 3。
参数extraDependencies不适用于运行mojo :-)。在此处查看参数:http : //tomcat.apache.org/maven-plugin-2.0-SNAPSHOT/tomcat7-maven-plugin/run- mojo.html。此参数仅供exec-war使用,请参见http://tomcat.apache.org/maven- plugin-2.0-SNAPSHOT/executable-war-jar.html。要添加您的jdbc驱动程序,只需执行以下操作:
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>8.4-701.jdbc4</version> </dependency> </dependencies> </plugin>
HTH :-)