【问题标题】:ClassNotFound Maven WarClassNotFound Maven 战争
【发布时间】:2017-04-19 20:50:44
【问题描述】:

我正在构建一个多模块 Maven Web 应用程序项目,但在我的 Tomcat 服务器上部署 WAR 时遇到问题。

我的项目结构是

  • 我的应用程序
    • MyAppSchemas
    • MyAppUtils
    • MyAppWs

这就是问题所在。当我尝试在本地 Tomcat 服务器上部署生成的战争时,我收到以下错误:

Apr 19, 2017 1:42:43 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter ApiOriginFilter
java.lang.ClassNotFoundException: io.swagger.api.ApiOriginFilter

APIOriginFilter 是在 MyAppSchemas 模块中生成的一个类。我已将 MyAppSchemas jar 作为依赖项包含在 MyAppWs pom 中:

<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>io.swagger</groupId>
        <artifactId>MyApp</artifactId>
        <version>1.0</version>
    </parent>
    <artifactId>MyAppWs</artifactId>
    <name>MyAppWs</name>
    <packaging>war</packaging>
    <build> 
        <plugins>
                    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>               
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>APP-INF/lib</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>MyAppSchemas</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>MyAppUtils</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</project>

底线:如何在我的战争运行时类路径中包含其他子模块?

【问题讨论】:

  • 我不相信它会有所作为,但删除 maven-war-plugin 配置。在 Tomcat 上下文中,这完全是一派胡言。然后在执行mvn clean install 后,验证您的依赖jar 是否已放置在构建WAR 文件过程中生成的MyAppWs/target/MyAppWs/WEB-INF/lib 目录中。

标签: java maven tomcat swagger war


【解决方案1】:

这可以通过将您的类添加到外部 jar 并将其标记为依赖项来实现。

通过系统范围添加依赖项

<dependency>
   ..
   <scope>system<scope>
   <systemPath>your jar path</systemPath>
</dependency>

然后使用插件定义如下

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>directory path/*.jar</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

【讨论】:

  • 我明白你的意思,但不应该有办法处理这个只是 maven 子模块依赖项吗?
  • 通常会遵循这种风格,因为当我们需要引用自定义类时它很灵活。
猜你喜欢
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多