【问题标题】:Resources in src/main/resources are not accessible to tomcat7 maven plugin?tomcat7 maven插件无法访问src/main/resources中的资源?
【发布时间】:2012-11-16 08:02:48
【问题描述】:

当我在 src/main/resources 中放置 xml 资源(例如 spring 配置文件、日志记录...等)时,当我在嵌入式 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中却找不到资源

更新:我正在使用带有 m2e 的 eclipse 我可以使用指向本地 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 中,还有一些例外情况,例如 src/main/resources 文件夹中的文件不存在。

更新 2:问题原来是父 POM 中的 &lt;resources&gt; 元素导致 .xml 文件不包含在生成的输出中。

【问题讨论】:

  • 这有点奇怪,因为我个人使用它没有任何问题你有什么样的错误?有什么样本可以重现吗?

标签: spring tomcat maven maven-tomcat-plugin


【解决方案1】:

通常当 Eclipse 尝试查找由您的应用程序定义的文件时..它将 src 链接到 src/main/java 和 src/main/resources..您是否会在 .class 生成的 maven 文件中看到以下条目日食..

<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文件夹中检查它们)

【讨论】:

  • 我猜你创建的 maven spring 文件夹结构没有将资源文件夹标识为 SRC 文件夹路径起点
  • 我在问题中没有看到 eclipse 这个词的痕迹。而且也没有标签说日食。你为什么假设他正在使用eclipse?您还知道其他 IDE。而且这个问题与任何 IDE 都没有任何关系。这是资源是否可用的问题。
  • @maba 抱歉假设错误。我在这里对他的想法是向他暗示,在 maven 中创建文件夹结构可能存在一些错误。作为一个例子,我为他放置了 eclipse,以便它可能对他找出错误的地方有用.
猜你喜欢
  • 2017-10-03
  • 2017-04-11
  • 2014-11-21
  • 2013-05-11
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
相关资源
最近更新 更多