Start Tomcat as a Gradle plugin for Java web projects

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, Ultimatebut 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 Communityversion. The problem is that this version does not have Tomcat support. In most cases, you can only install Smart Tomcatplug-ins. Of course, there is Tomcat runneranother 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 groovygradle, which is also the default language of gradle for a long time. However, if it is kotlinnot too different, just follow the gourd build.gradle.

plugins {
	id 'com.bmuschko.tomcat'
}

Of course this is apply pluginalso possible:

apply plugin: "com.bmuschko.tomcat"

Then introduce the tomcat body into dependenciesit, 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, tomcatVersionfor 9.0.75, tomcat-embed-logging-julifor 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了:

image.png

执行tomcatRun即可运行,这种方式总体看上去也是比较便捷的,不过说便捷当然还是比不了spring-boot,但也有很多人喜欢这样传统的方式。国内也有很多文章介绍这个com.bmuschko.tomcat,连b站专栏都有了,不过感觉都是多多少少有些遗漏,根据我实践来看最后buildscript的这一段是不能跳过的。

Guess you like

Origin juejin.im/post/7234057845620080695