javatomcatwarexecutable-jarweb-inf

Difference between tomcat exploding war in startup/catalina script and manually exploding war?


I've copied my .war into the webapps folder of my local tomcat server. When I start tomcat, the .war gets exploded to a different structure than when I manually explode it with jar -xf. When I manually explode the .war, I get the WEB-INF folder as a child of the app folder (below). This is what I want.

Both startup fine, but when tomcat explodes my war, sending request for resources always returns 404. I think WEB-INF needs to be the child of the exploded war directory for requests to return 200. Manually exploding the .war inside webapps and then running tomcat works and also stopping tomcat after it has auto-exploded the war, moving the WEB-INF one level up, and then restarting tomcat also works.

Is there a way to package my war so that tomcat will explode it with WEB-INF as the immediate child of the exploded war directory? And why is tomcat exploding the war any differently than jar -xf?

TOMCAT

exploded_war
├── app_name
│   └── WEB-INF
│       ├── classes
│       │   └── ...
│       ├── servlet.xml
│       ├── lib
│       │   ├── ...
│       └── web.xml
└── META-INF
    └── ...

MANUAL

exploded_war
└── WEB-INF
    ├── classes
    │   └── ...
    ├── servlet.xml
    ├── lib
    │   ├── ...
    └── web.xml

EDIT - Here is my build script executed in the project folder -

mvn package
cp src/WEB-INF/*.xml /target/app_name/WEB-INF 
cd target
jar -cvf app_name.war app_name/WEB-INF 
cp app_name.war {path_to}/apache-tomcat-8.5.13/webapps

Solution

  • Your WAR file structure is incorrect. WAR file must not contain application name folder at its root. You need to drop app_name folder from the jar command.

        mvn package
        cp src/WEB-INF/*.xml /target/app_name/WEB-INF 
        cd target/app_name
        jar -cvf ../app_name.war WEB-INF 
        cd ..
        cp app_name.war {path_to}/apache-tomcat-8.5.13/webapps
    

    See the following screenshot for example:

    WAR Structure