tomcat7-maven-plugin extraDependency seems not being loaded
Asked Answered
D

1

5

I've been using tomcat7-maven-plugin. I want to run my webapp which connects to the PostgreSQL database by using the embedded tomcat. This is the related part of my POM file:

<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>

Executing tomcat7:run fails with

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

The dependency itself is correct (http://repo1.maven.org/maven2/postgresql/postgresql/8.4-701.jdbc4/).

I use Maven 3.

Doxy answered 29/3, 2012 at 15:43 Comment(0)
L
13

The parameter extraDependencies is not for the run mojo :-). See parameters here: http://tomcat.apache.org/maven-plugin-2.0-SNAPSHOT/tomcat7-maven-plugin/run-mojo.html. This parameter is for exec-war see purpose http://tomcat.apache.org/maven-plugin-2.0-SNAPSHOT/executable-war-jar.html. To add your jdbc driver simply do:

<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 :-)

Luis answered 29/3, 2012 at 15:52 Comment(3)
Obviously the simplest solution is the hardest one to be seen, thanks a lot! :)Doxy
Btw this seems to be working until 2.0 release (inclusive). In 2.1 and 2.2 there is no way of adding dependency like that, it just doesn't work.Rhines
@Sloin May I ask you if you can elaborate on your it-just-doesn't-work ? I could start my mvn clean install tomcat7:run fine on a 2.2 version.Parthena

© 2022 - 2024 — McMap. All rights reserved.