In the era of NetBeans and Eclipse, it was relatively easy to start web projects with Tomcat. Myeclipse even had a built-in tomcat server, so you didn't even need to download tomcat yourself. But when Java's IDE became popular in Intellij IDEA, there were some small problems...
I have to admit that the iteration speed of Intellij IDEA is a bit fast in recent years, and there are several major updates a year. At the beginning, many people should use the version, Ultimate
but even if it can be cracked like myeclipse, but every time it is released There are still some troubles in the new version. It is much more troublesome than before. Many people have to switch to the new Community
version. The problem is that this version does not have Tomcat support. In most cases, you can only install Smart Tomcat
plug-ins. Of course, there is Tomcat runner
another plugin. These IDE plug-ins are certainly possible, but only one set of solutions is obviously weak, and it is easy to fail to install IDEA plug-ins in China. Here is a new way to introduce a Gradle plug-in to start Tomcat, and you don’t need to install it yourself. In addition, if you install Tomcat itself, you only need to install the plug-in.
The default language introduced here is groovy
gradle, which is also the default language of gradle for a long time. However, if it is kotlin
not too different, just follow the gourd build.gradle
.
plugins {
id 'com.bmuschko.tomcat'
}
Of course this is apply plugin
also possible:
apply plugin: "com.bmuschko.tomcat"
Then introduce the tomcat body into dependencies
it, after all, it is to be started together with the project:
dependencies {
def tomcatVersion = '8.5.16'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:8.5.2",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-websocket:${tomcatVersion}"
}
If you want to use version 9, tomcatVersion
for 9.0.75
, tomcat-embed-logging-juli
for 9.0.0.M6
.
At the end, you need to make some settings for tomcat:
tomcat {
httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
ajpProtocol = 'org.apache.coyote.ajp.AjpNio2Protocol'
}
These two properties can be said to be necessary. Of course, there are many parts that can be set. The following common properties can be seen at a glance:
tomcat {
httpPort = 8090
httpsPort = 8091
enableSSL = true
contextPath = 'url-path'
}
当然还有很多,详细可以看它的github文档介绍:github.com/bmuschko/gr…
最后还有一点,需要在settings.gradle
中写一段buildscript
:
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.7.0'
}
}
然后刷新IDEA的Gradle面板就能看见Tasks里有一个web application
了:
执行tomcatRun
即可运行,这种方式总体看上去也是比较便捷的,不过说便捷当然还是比不了spring-boot
,但也有很多人喜欢这样传统的方式。国内也有很多文章介绍这个com.bmuschko.tomcat
,连b站专栏都有了,不过感觉都是多多少少有些遗漏,根据我实践来看最后buildscript
的这一段是不能跳过的。