Question

When I put xml resources such as spring configuration files, logging ... etc in the src/main/resources these files are not readable by my application when I run it in the embedded tomcat plugin. However moving these files into src/main/java fixes the problem.

Here is the plugin definition that I am using:

<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>

Why can the resources be found when I put them in src/main/java but not when I put them in src/main/resources when running mvn tomcat7:run

UPDATE: I am using eclipse with m2e I can run the application just fine using Run As > Run on Server which is pointed to a local tomcat server. The folder structure is correct, eclipse settings are coming from the pom.xml via 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)

in this case log4j-config.xml is in src/main/resources, there are few more exceptions like that saying that files on src/main/resources folder don't exist.

Update 2: The problem turned out to be a <resources> element in the parent POM that was causing the .xml files to not be included in the generated output.

Was it helpful?

Solution

Normally when eclipse try to find the files which are defined by your application..it links the src to src/main/java and src/main/resources..could you see the following entries in .class file of maven generated for 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>

then for the resources first the tomcat checks whether they are in the resources (and also checks them in the java folder)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top