Spring Boot/Gradle/Tomcat setting context path to

2019-04-01 00:14发布

问题:

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

回答1:

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

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'
    }
...
}