spring - tomcat7 maven 插件无法访问 src/main/resources 中的资源?

标签 spring tomcat maven maven-tomcat-plugin

当我在 src/main/resources 中放置诸如 spring 配置文件、日志记录等 xml 资源时,当我在嵌入式 tomcat 插件中运行这些文件时,我的应用程序无法读取这些文件。然而,将这些文件移动到 src/main/java 中可以解决问题。

这是我正在使用的插件定义:

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <configuration>
        <port>9090</port>
        <path>/</path>
            <contextFile>${basedir}/src/main/webapp/META-INF/context.xml</contextFile>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgres.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument-tomcat</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>${javax.mail.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>activation</artifactId>
                    <groupId>javax.activation</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>default-cli</id>
            <goals>
                <goal>deploy</goal>
                <goal>run-war</goal>
            </goals>
            <phase>pre-integration-test</phase>
        </execution>
    </executions>
</plugin>

为什么运行mvn tomcat7:run时,资源放在src/main/java里能找到,放在src/main/resources里找不到

更新: 我将 eclipse 与 m2e 结合使用,我可以使用指向本地 tomcat 服务器的 Run As > Run on Server 运行应用程序。文件夹结构正确,eclipse 设置来自 pom.xml 通过 m2e。

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: class path resource [log4j-config.xml] cannot be resolved to URL because it does not exist
    at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:155)
    at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

在这种情况下,log4j-config.xml 位于 src/main/resources 中,还有一些异常(exception),比如 src/main/resources 文件夹中的文件不存在。

更新 2: 问题原来是 <resources>导致 .xml 文件未包含在生成的输出中的父 POM 中的元素。

最佳答案

通常当 eclipse 尝试查找由您的应用程序定义的文件时..它将 src 链接到 src/main/java 和 src/main/resources..您能在生成的 maven .class 文件中看到以下条目吗对于 eclipse ..

<classpath>
    <classpathentry including="**/*.java" kind="src" path="src/main/java"/>
    <classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>

/**all repository entries are also here**/
</classpath>

然后对于资源,tomcat首先检查它们是否在资源中(并在java文件夹中检查它们)

关于spring - tomcat7 maven 插件无法访问 src/main/resources 中的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13412608/

相关文章:

java - 如何使用 Spring Boot、JPA 和 Thymeleaf 设置搜索栏

在 Spring 应用程序中打开模式对话框的 JavaScript 函数

java - 来自 JAR 的 Spring 依赖注入(inject)

java - 为什么 RestTemplate 并发调用时会挂起?

java - CORS 过滤器未按预期工作

java - 通过JMX访问docker容器中运行的tomcat

tomcat 网址和 robots.txt

maven - gradle:导入本地 bom

java - 应用服务器集成到开发流程中

java - Maven 的配置文件问题