javamaventomcatembedded-tomcat-7

How can I add a dependency to embedded tomcat using the maven plugin?


UPDATE

As it turns out there is nothing wrong with my dependencies. The problem is that my META-INF/context.xml file packaged inside my war is not being read by the tomcat plugin. I am still looking for a solution as to why this isn't working.

ORIGINAL QUESTION

I have a multi-module project that looks like this:

myproject
+ - business
+ - app

where "business" and "app" are modules with "myproject" defined as parent in their pom.xml. The app module packages a war file with a context.xml file containing a definition for a JDBC connection pool. When the war file is packaged and deployed to a standalone tomcat instance everything works. The standalone tomcat instance has the required MySql connector jar in its lib classpath directory.

When I run the app module using the tomcat7 maven plugin with the tomcat7:run goal and then access my application I get the "SQLException: No suitable driver" error. How can I get the embedded tomcat to see the MySql connector jar at the right time?

myproject pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.robsite</groupId>
  <artifactId>myproject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>app</module>
    <module>business</module>
  </modules>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>8080</port>
          <path>/app</path>
          <contextReloadable>true</contextReloadable>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

app pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.robsite</groupId>
    <artifactId>myproject</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>app</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <!-- Jersey modules -->
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.26-b01</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.25.1</version>
    </dependency>
    <!-- Spring modules -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.6.RELEASE</version>
    </dependency>
    <!-- Modules provided by web container -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- Dependency on business module and its provided dependencies -->
    <dependency>
      <groupId>com.robsite</groupId>
      <artifactId>business</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

Solution

  • In answer to my original question the way to add dependencies on the tomcat plugin, and have classes loaded by the classloader before loading a web app, is by adding a dependency tag inside the plugin.

    To solve the real problem I had to add a specific configuration to tell the tomcat plugin to read the /META-INF/context.xml file from my web application module stored at app/src/main/webapp/META-INF/context.xml.

    The corrected pom.xml for my root myproject parent pom is below.

    <?xml version="1.0" encoding="UTF-8"?>
    <project
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://maven.apache.org/POM/4.0.0"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    >
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.robsite</groupId>
      <artifactId>myproject</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <modules>
        <module>app</module>
        <module>business</module>
      </modules>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <port>8080</port>
              <path>/app</path>
              <contextReloadable>true</contextReloadable>
              <contextFile>${project.basedir}/src/main/webapp/META-INF/context.xml</contextFile>
            </configuration>
            <dependencies>
              <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.40</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
      </build>
    
    </project>