java - 如何使用 maven 插件向嵌入式 tomcat 添加依赖项?

标签 java maven tomcat embedded-tomcat-7

更新

事实证明,我的依赖没有任何问题。问题是我的 war 中打包的 META-INF/context.xml 文件没有被 tomcat 插件读取。我仍在寻找解决方案,以解决为什么这不起作用。

原始问题

我有一个看起来像这样的多模块项目:

myproject
+ - business
+ - app

其中“business”和“app”是在其 pom.xml 中将“myproject”定义为父级的模块。应用程序模块将一个 war 文件与一个包含 JDBC 连接池定义的 context.xml 文件打包在一起。当 war 文件被打包并部署到一个独立的 tomcat 实例时,一切正常。独立的 tomcat 实例在其 lib 类路径目录中具有所需的 MySql 连接器 jar。

当我使用带有 tomcat7:run 目标的 tomcat7 maven 插件运行应用程序模块,然后访问我的应用程序时,我收到“SQLException:没有合适的驱动程序”错误。如何让嵌入式 tomcat 在正确的时间看到 MySql 连接器 jar?

我的项目 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>

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

最佳答案

在回答我最初的问题时,添加对 tomcat 插件的依赖项并在加载 Web 应用程序之前由类加载器加载类的方法是在插件中添加依赖项标记。

为了解决真正的问题,我必须添加一个特定的配置来告诉 tomcat 插件从存储在 app 的 Web 应用程序模块中读取 /META-INF/context.xml 文件/src/main/webapp/META-INF/context.xml.

我的根 myproject 父 pom 的更正 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>
          <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>

关于java - 如何使用 maven 插件向嵌入式 tomcat 添加依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42566184/

相关文章:

java - 如何更改枚举值?

java - 使用 Maven 切换 SLF4J SimpleLogger 属性文件

java - 每当我重新单击或导航到下一个 fragment 时,我的 fragment 都会不断重新创建

Java - 关闭 ServerSocketChannel

maven - 使用Jenkins部署到Nexus

java - 如何在POM.xml文件中指定代理,找不到settings.xml文件

maven - 使用 Jenkins 在远程 Tomcat8 上部署时,您提供的用户名不允许使用基于文本的 Tomcat 管理器(错误 403)

java - 优化tomcat启动时间

java - eclipse 上 tomcat 8 的问题

java - 如何只声明一个