[JAVA] Prepare the execution environment of Tomcat in IntelliJ Community

Prepare the execution environment of Tomcat in IntelliJ Community

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.

Install IntelliJ

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.

Install tomcat

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.

Creating a project

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.

Various settings

IntelliJ settings can be opened from File → Settings.

Automatic source directory generation

Build, Run, Deploy → Build Tools → Gradle automatically creates an empty content root directory By checking the box, the source directory will be created automatically.

Enable hover display for javadoc

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.

Installing the pig-in

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).

build.gradle settings

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

Addition of plying in

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.

API addition used only at compile time

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

Server version specification

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.

Specify the name of the war file to generate

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

Character code specification

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

Execution method

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.

Bonus (war generation)

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.

Postscript (Introduced gretty that can use Tomcat 9)

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'

Postscript (It became possible to specify with plugins)

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"
}

Other articles that may be useful in Java EE development

Recommended Posts

Prepare the execution environment of Tomcat in IntelliJ Community
Tomcat, Context, debug settings in IntelliJ Community
Prepare the environment of CentOS 8 with Sakura VPS
Format of the log output by Tomcat itself in Tomcat 8
Access the war file in the root directory of Tomcat
Return the execution result of Service class in ServiceResponse class
[Java] Get the file in the jar regardless of the environment
Set the root URL when starting tomcat in intelliJ
SSL in the local environment of Docker / Rails / puma
Install by specifying the version of Django in the Docker environment
Setting the baseURL in the axios module of Docker environment Nuxt
Examine the system information of AWS Lambda operating environment in Java
Implementation of asynchronous processing in Tomcat
Order of processing in the program
Setting project environment variables in intelliJ
Run Redmine in the local environment of Windows10 Pro-Use Docker Desktop for Windows
How to change the value of a variable at a breakpoint in intelliJ
The secret to the success of IntelliJ IDEA
The identity of params [: id] in rails
Implementation of multi-tenant asynchronous processing in Tomcat
Prepare the format environment with "Rails" (VScode)
Prepare the security check environment for Rails 6
The story of AppClip support in Anyca
Creating a Servlet in the Liberty environment
I tried the AutoValue library in Intellij
The story of writing Java in Emacs
[Rails] Reset the database in the production environment
Display the background image in the production environment
Write the movement of Rakefile in the runbook
Prepare Nuxt (web) + Laravel (API) development environment in the same repository using docker-compose
Be careful about upgrade if you use | etc. in the URL of Tomcat
Set the source of the library set as a dependency in IntelliJ as a separate module of the project
How to set environment variables in the properties file of Spring boot application
[Order method] Set the order of data in Rails
Has the content of useBodyEncodingForURI changed from Tomcat8?
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
Self-hosting with Docker of AuteMuteUs in Windows environment
About the idea of anonymous classes in Java
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Prepare the JVM language development environment with WSL
The story of learning Java in the first programming
Measure the size of a folder in Java
Improve the performance of your Docker development environment
Feel the passage of time even in Java
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
Support out of support in docker environment using centos6
Import files of the same hierarchy in Java
I tried using the profiler of IntelliJ IDEA
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
How to use git with the power of jgit in an environment without git commands