4

I'm using Spring Boot and Gradle to create a war.

war {
    baseName = 'foo'
    version =  '0.1.0'
}

As the war task has a version number the generated war name is foo-0.1.0.war and so when deployed to Tomcat the context path is http://localhost/foo-0.1.0/

I'd like to use the context path 'bar' omitting the version number and using a different path.

http://localhost/bar

Is this possible using a Spring annotation? I've tried changing the @RequestMapping but this only changes the context to http://localhost/foo-0.1.0/bar

Following the answers here and here tried adding a context xml snippet to <catalina_home>/conf/Catalina/localhost/

<Context docBase="/foo-0.1.0" path="/bar" reloadable="true"> </Context>

but although Tomcat is be undeploying /foo-0.1.0 I get the error:

WARNING: A docBase C:\tools\apache-tomcat-7.0.53\webapps\foo-0.1.0 inside the host appBase has been specified, and will be ignored.

Have I configured the Context incorrectly?

If setting the context is the correct solution how do I add it to my project as spring boot appears to generate the web-inf folder when the application is compiled?

I've tried adding context.xml to /src/main/webapp/META-INF and although its included in the war it doesn't change the context path

enter image description here

Community
  • 1
  • 1
GrahamA
  • 5,875
  • 29
  • 39
  • Add a context.xml file to the wars meta-inf directory containing the path. Or don't deploy to tomcat, use the embedded server and simply set `server.context-path=/bar` in your `application.properties`. – M. Deinum Jun 23 '15 at 10:30
  • @M.Deinum I can't use the embedded server as I need to deploy the war into a production environment that contains other applications. I've tried the meta-inf approach and it doesn't seem to work - maybe something wrong with the file – GrahamA Jun 23 '15 at 10:32
  • When putting it in the META-INF make sure you don't have to other context.xml around. If you do it will be ignored as the one on the server takes precedence! – M. Deinum Jun 23 '15 at 10:37
  • @M.Deinum good advice, I've checked and there are no other context xml snippets in `\conf\Catalina\localhost` – GrahamA Jun 23 '15 at 10:40
  • Also this isn't really spring boot related but simply how the gradle war stuff works and how deployment on tomcat works. It is the same for each war you deploy on tomcat. Also check the `server.xml` for any related entries. – M. Deinum Jun 23 '15 at 10:46
  • 1
    That approach won't work, *`This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.`* (from the tomcat guide). So the only solutions appears to be to rename your war or add it inside the Host element in your server.xml. – M. Deinum Jun 23 '15 at 10:59
  • @M.Deinum I'm starting to think that's the case but then the Apache user guide warns against modifying server.xml – GrahamA Jun 23 '15 at 11:01

1 Answers1

2

The root cause of the problem was using the Manger GUI to deploy the application, it seems that using the "WAR file to deploy" 'tool' uses the war name even if a context is provided

Manager - war file to deploy

Using the Gradle Cargo plugin I've been able to deploy the application as required using a context.xml in \src\main\resources and using a context in build.gradle

cargo {
    containerId = 'tomcat7x'
    port = 80

    deployable {
        context = 'bar'
    }
...
}
GrahamA
  • 5,875
  • 29
  • 39