Search code examples
maventomcatmaven-war-plugin

Tomcat not serving files from maven-war-plugin


I am trying to make my Tomcat server serve a file. I made a very simple example to show you what's wrong, and even if it's simple, it's not working.

My project is made like this:

test
|->assets
|   |->testB.txt
|->src
|  |->main
|  |  |->webapp
|  |  |  |->WEB-INF
|  |  |  |  |->web.xml
|  |  |  |->testA.txt
|-> pom.xml

pom.xml

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

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>assets/</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat6-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
</web-app>

If I execute mvn package tomcat6:run, I can access testA.txt but I can't access testB.txt, even if, when I have a look a the ".war" generated, I see :

|->testA.txt
|->testB.txt
|->META-INF
|->WEB-INF
|  |->web.xml
|  |->classes

I can't figure out why I have access to one but I can't see the other (404 error) ...


Solution

  • You cannot access testB.txt because when running tomcat6:run, the Tomcat Maven Plugin will look at the webapp folder by default, not at the generated war file (via the package phase) nor at the generated unpacked war in the target folder.

    This is made on purpose so that you can create on the fly new resources or change their content and changes will be available on the running instance (hot deploy).

    You can verify it by:

    • Add an additional testC.txt to the war file, it will be ignored by the running instance
    • Add an additional testC.txt to the built unpacked war, it will be ignored
    • Add an additional testC.txt to the webapp folder, it will be available!

    From its official documentation:

    The default location is ${basedir}/src/main/webapp

    You can configure it via the warSourceDirectory element. In your case, you want to point it at the built unpacked war of the target folder. So you can change your configuration as following:

    <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat6-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <path>/</path>
            <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
        </configuration>
    </plugin>
    

    Note: it is now pointing at Maven built via the package phase. And it will work.