gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:03:52 25 4
gpt4 key购买 nike

当我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com