Note: This article is for creating a Tomcat (JavaEE) project in the IntelliJ Community (free version). If you are using Ultimate (paid version), please use the following sites. In addition, the environment is assumed to be Windows 10.
I'm sorry, but I think that the installation of IntelliJ is infinitely explained, so I will omit it. If you find it difficult to find a commentary site yourself, please refer to the link below.
Tomcat is not necessary for development, but if you want it to function as a server, it will not hurt to install it. In addition, we will leave the explanation to the following site.
After launching IntelliJ, click Create New Project. Select Gradle from the project type selection on the left. JDK version as introduced in Site Is specified. Select Next and enter the project name for the Artifact ID. You can leave the version as it is for the time being. Select Next and check Auto Import to proceed. Press Done to complete.
IntelliJ settings can be opened from File → Settings.
Build, Run, Deploy → Build Tools → Gradle automatically creates an empty content root directory By checking the box, the source directory will be created automatically.
Check Show quick documents by moving the mouse under Others in Editor → General. This makes it possible to display the information (javadoc) of the code just by hovering the cursor like Eclipse.
You can install the plying in from the setting plying in. Code shaping plugin google-java-format and code format check plugin CheckStyle-IDEA We recommend that you install (: //plugins.jetbrains.com/plugin/1065-checkstyle-idea).
There is a file called build.gradle directly under the project folder, so open it.
You can configure the Gradle wrapper to use a distribution with a source. It provides the IDE with Gradle API / DSL documentation.
Is displayed, OK, apply the suggestion! I think you should select. (I don't understand)
Now, let's set up to create a Tomcat application in this project.
See below for .gradle files.
Also, for the grammar of Groovy (script format described in the .gradle file), refer to the following.
First, you should have generated code similar to the following.
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
Add the id to plugins here.
plugins {
id 'java'
//A pig-in that allows you to war your project
id 'war'
//A pig-in that can launch Tomcat with one click
id "org.akhikhl.gretty" version "2.0.0"
}
By the way, please refer to the following for the war file and each plugin.
Although it is provided in the execution environment, it is not possible to compile a server program without the JavaEE API. Therefore, specify the library to be referenced only at compile time in dependencies.
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
//Specify the library to be used only at compile time.
compileOnly 'javax:javaee-api:8.0'
}
A plugin called gretty can start two types of servers: Jetty and Tomcat. .. To actually use it, add the following code.
gretty {
//Here, Jetty/Specify Tomcat and version.
servletContainer = 'tomcat8'
}
For the version that can be specified in ServletContainer, refer to the here site.
Next, specify the file name of the war file generated by the war plug-in. It is not necessary to specify it separately, so skip it if you do not need it.
war {
archiveName = 'Hoge.war'
}
To change the character code at compile time, add the following two sentences.
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
UTF-8 is specified here. However, if it is left as it is, characters may be garbled when communicating or reading an external file, so Also change the character code at runtime. Add the following inside getty.
gretty {
servletContainer = 'tomcat8'
jvmArgs = ['-Dfile.encoding=UTF-8']
}
The string specified in jvmArgs is added as an option when executing java.
This completes editing the configuration file. The summary is shown below.
plugins {
id 'java'
id 'war'
id "org.akhikhl.gretty" version "2.0.0"
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly 'javax:javaee-api:8.0'
}
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'
gretty {
servletContainer = 'tomcat8'
jvmArgs = ['-Dfile.encoding=UTF-8']
}
war {
archiveName = 'Hoge.war'
}
Select View → Tools window → Gradle to see the Gradle task list? Something like that will come out. You can start Tomcat by double-clicking Tasks-> gretty-> tomcat Run in it. When you start it with this, you can also operate it with the execute button on the upper right. Save the project name [tomcatRun] from the dropdown to the left of the ▷ mark so that you can recall it later. If the item in [] is something else, run tomcatRun once and try again.
There is a task called war in build in Tasks, so when you execute it, the war project will be output as a war file. Specifically, the class file etc. are stored in the generated folder called build, and the war file is placed in libs in build.
By placing the war file in webapps in the Tomcat installation directory, you can refer to it with the http: // IP: Port / war file name when running Tomcat.
The getty obtained by the above procedure was available only for versions up to Tomcat8, so I searched for one that can use Tomcat9.
・ Gretty-gradle-plugin (GitHub) ・ Gretty documentation (Official site)
Remove org.akhikhl.gretty from plagins and add the following statement:
apply from: 'https://raw.github.com/gretty-gradle-plugin/gretty/master/pluginScripts/gretty.plugin'
Also, if you get an error saying that gretty.plugin cannot be found at runtime, such as on a network with restricted ssh, you can also execute it by the following method.
A problem occurred evaluating root project 'Hoge'.
\> Could not get resource 'https://raw.github.com/gretty-gradle-plugin/gretty/master/pluginScripts/gretty.plugin'.
\> Could not HEAD 'https://raw.github.com/gretty-gradle-plugin/gretty/master/pluginScripts/gretty.plugin'.
> Connect to raw.github.com:443 [raw.github.com/151.101.72.133] failed: Connection timed out: connect
(Error statement)
First, download gretty.plugin that appears in the error statement. After putting it in the same directory as build.gradle, set as follows instead of apply from added earlier.
apply from: 'gretty.plugin'
It can be executed just by specifying the id in plugins as shown below.
build.gradle
plugins {
id 'java'
id "org.gretty" version "2.3.0"
}
Recommended Posts