繁体   English   中英

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

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

UPDATE

事实证明,我的依赖没有错。 问题是tomcat插件无法读取我打包在战争中的META-INF / context.xml文件。 我仍在寻找一种解决方案,为什么它不起作用。

原始问题

我有一个如下所示的多模块项目:

myproject
+ - business
+ - app

其中“ business”和“ app”是在其pom.xml中定义为“ myproject”的模块。 应用模块将war文件与context.xml文件打包在一起,该文件包含JDBC连接池的定义。 将war文件打包并部署到独立的tomcat实例后,一切正常。 独立的tomcat实例在其lib classpath目录中具有必需的MySql连接器jar。

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

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>

应用程序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 / src / main / webapp / META-INF / context中的 Web应用程序模块中读取/META-INF/context.xml文件。 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM