Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Use different Tomcat Version

In Spring Boot Documentation there is a section Use Tomcat 7.x or 8.0 with Gradle This works great with Maven as shown in some spring-boot-samples, but unfortunately not for Gradle.

Is there a easier solution than excluding all tomcat dependencies form tomcat-starter and add them separately with another version?

To Reproduce: If you convert the sample project spring-boot-sample-tomcat80-ssl into a Gradle project with following dependencies:

dependencies {
    compile("org.springframework.boot:spring-boot-starter:1.4.2.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RELEASE")
    compile("org.springframework:spring-webmvc:4.3.4.RELEASE")
    compile("org.apache.httpcomponents:httpclient:4.5.2")
    compile("org.apache.tomcat:tomcat-juli:8.0.36")
    compile("org.yaml:snakeyaml:1.17")
    testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.2.RELEASE'
}

The output from gradlew dependencies looks like this:

compile - Dependencies for source set 'main'.
+--- org.springframework.boot:spring-boot-starter:1.4.2.RELEASE
|    +--- org.springframework.boot:spring-boot:1.4.2.RELEASE
|    |    +--- org.springframework:spring-core:4.3.4.RELEASE
|    |    |    \--- commons-logging:commons-logging:1.2
|    |    \--- org.springframework:spring-context:4.3.4.RELEASE
|    |         +--- org.springframework:spring-aop:4.3.4.RELEASE
|    |         |    +--- org.springframework:spring-beans:4.3.4.RELEASE
|    |         |    |    \--- org.springframework:spring-core:4.3.4.RELEASE (*)
|    |         |    \--- org.springframework:spring-core:4.3.4.RELEASE (*)
|    |         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    |         +--- org.springframework:spring-core:4.3.4.RELEASE (*)
|    |         \--- org.springframework:spring-expression:4.3.4.RELEASE
|    |              \--- org.springframework:spring-core:4.3.4.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-autoconfigure:1.4.2.RELEASE
|    |    \--- org.springframework.boot:spring-boot:1.4.2.RELEASE (*)
|    +--- org.springframework.boot:spring-boot-starter-logging:1.4.2.RELEASE
|    |    +--- ch.qos.logback:logback-classic:1.1.7
|    |    |    +--- ch.qos.logback:logback-core:1.1.7
|    |    |    \--- org.slf4j:slf4j-api:1.7.20 -> 1.7.21
|    |    +--- org.slf4j:jcl-over-slf4j:1.7.21
|    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    +--- org.slf4j:jul-to-slf4j:1.7.21
|    |    |    \--- org.slf4j:slf4j-api:1.7.21
|    |    \--- org.slf4j:log4j-over-slf4j:1.7.21
|    |         \--- org.slf4j:slf4j-api:1.7.21
|    +--- org.springframework:spring-core:4.3.4.RELEASE (*)
|    \--- org.yaml:snakeyaml:1.17
+--- org.springframework.boot:spring-boot-starter-tomcat:1.4.2.RELEASE
|    +--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6
|    +--- org.apache.tomcat.embed:tomcat-embed-el:8.5.6
|    \--- org.apache.tomcat.embed:tomcat-embed-websocket:8.5.6
|         \--- org.apache.tomcat.embed:tomcat-embed-core:8.5.6
+--- org.springframework:spring-webmvc:4.3.4.RELEASE
|    +--- org.springframework:spring-aop:4.3.4.RELEASE (*)
|    +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|    +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|    +--- org.springframework:spring-core:4.3.4.RELEASE (*)
|    +--- org.springframework:spring-expression:4.3.4.RELEASE (*)
|    \--- org.springframework:spring-web:4.3.4.RELEASE
|         +--- org.springframework:spring-aop:4.3.4.RELEASE (*)
|         +--- org.springframework:spring-beans:4.3.4.RELEASE (*)
|         +--- org.springframework:spring-context:4.3.4.RELEASE (*)
|         \--- org.springframework:spring-core:4.3.4.RELEASE (*)
+--- org.apache.httpcomponents:httpclient:4.5.2
|    +--- org.apache.httpcomponents:httpcore:4.4.4
|    +--- commons-logging:commons-logging:1.2
|    \--- commons-codec:commons-codec:1.9
+--- org.apache.tomcat:tomcat-juli:8.0.36
\--- org.yaml:snakeyaml:1.17

Executing the SampleTomcatSslApplicationTests you can see the wrong Tomcat Version in Logs:

2016-12-21 10:20:36.713  INFO 11368 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-12-21 10:20:36.714  INFO 11368 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6

SOLUTION

  • use spring boot gradle plugin -> here comes the magic
  • set ext['tomcat.version'] = '8.0.36' in build script
like image 224
samized Avatar asked Dec 21 '16 10:12

samized


People also ask

Which Tomcat version does spring boot use?

Tomcat 7 & 8.0 work with Spring Boot, but the default is to use Tomcat 8.5. If you cannot use Tomcat 8.5 (for example, because you are using Java 1.6) you will need to change your classpath to reference a different version.

Can we change Tomcat server in spring boot?

How to change Embedded tomcat server in Spring Boot? Default Embedded tomcat server can be changed to any of Jetty, Undertow and Reactor servers by changing the default dependency to specific dependency in pom. xml and also by mentioning exclude option to particular dependency.

How do I change the default embedded Tomcat server in spring boot?

Another way to change the port of embedded tomcat in the Spring Boot application is by specifying the server. port property in the resource file. For example, if you want your Spring boot application to listen on port 8080, then you can specify server. port=8080 on the application.


1 Answers

I already replied you on the issue you've created. After you've told us our documentation was wrong, I took the time to build a sample that does exactly what is referenced in the doc.

If that does not work for you, you'll have to be more specific and explain what is wrong in the documentation.

like image 194
Stephane Nicoll Avatar answered Oct 03 '22 02:10

Stephane Nicoll