Using Gradle with VS Code, build Java → run

Overview

As the title suggests, this article briefly summarizes the procedure of "Building and running Java using Gradle with VS Code".

What is Gradle? https://ja.wikipedia.org/wiki/Gradle Gradle is a ** open source build automation system ** based on the Apache Ant and Apache Maven concepts, and is a Groovy-based ** domain-specific language instead of the ** XML format used by Apache Maven to declare project settings ( DSL) is adopted. Gradle uses a Directed Acyclic Graph (DAG) to determine the task launch order. Gradle is designed for multi-project builds, which can be very large, and supports incremental builds that intelligently determine which part of the build tree is up to date. Therefore, there is no need to rerun tasks that rely on the latest incremental build part. Early plugins focused primarily on Java, Groovy, and Scala development and deployment, but the roadmap shows workflows for other languages and projects.

procedure

1. Install VS Code

Install the main body of VS Code from the following https://code.visualstudio.com/

2. Install Java plugin

Search for "Java Extension Pack" in Preference-> Extensions and install it If you install this, all the following plugins will be included ・ Language Support for Java (TM) by Red Hat ・ Debugger for Java ・ Java Test Runner ・ Maven for Java -Java Dependency Viewer ・ Visual Studio IntelliCode

3. Java preparation for the terminal

Install Java and set $ JAVA_HOME as an environment variable https://www.java.com/ja/download/help/download_options.xml Add one of the following depending on the Java version used for the ~ / .bash_profile file

export JAVA_HOME=`/usr/libexec/java_home -v 10` #When you want to use java10
export JAVA_HOME=`/usr/libexec/java_home -v 9` #When you want to use java9
export JAVA_HOME=`/usr/libexec/java_home -v 1.8` #When you want to use java8

4. Install Gradle

Install with the following command

brew install gradle

5. Create a project

Create a folder for your Gradle project

mkdir project-name-dir
cd project-name-dir
gradle init --type java-application

6. Gradle operation check

Since the skeleton is prepared in the initial creation, execute the following to check the operation

gradle build #Build
gradle run #Run
gradle check #Verification
gradle clean #clean

7. VS Code settings

Add JAVA_HOME to VS Code settings as well Preference->Settings Search for "javahome" in, open the configuration file and add as below

"java.home": "<2.One of the paths added to the environment variable in>"

8. Task settings

Set tasks when running in VS Code For the settings, add the following to the file .vscode/task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "./gradlew build"
        },
        {
            "label": "run",
            "type": "shell",
            "command": "./gradlew run",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "./gradlew clean"
        },
        {
            "label": "check",
            "type": "shell",
            "command": "./gradlew check"
        }
    ]
}

9. Perform the task

Execute the task You can execute the task defined in Terminal-> Run Task

Summary

Actually, I was planning to make a Java application and write an article, but since it is still being implemented, I tried using Gradle with VS Code for the first time, so I tried to organize the process, but it was relatively easy to introduce. It was. It's also a feature of VS Code, but it's lightweight and very comfortable.

Recommended Posts

Using Gradle with VS Code, build Java → run
Java build with mac vs code
Build a Java development environment with VS Code
Build Java development environment with VS Code on Mac
Build VS Code + WSL + Java + Gradle environment from scratch
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Build a Java project with Gradle
Prepare Java development environment with VS Code
CICS-Run Java applications-(3) Build management with Gradle
Try using Spring Boot with VS Code
[Note] A story about changing Java build tools with VS Code
Until you run Hello World of JavaFX with VS Code + Gradle
Lombok with VS Code
Static code analysis with Checkstyle in Java + Gradle
Build and test Java + Gradle applications with Wercker
First gradle build (Java)
Build Java with Wercker
Try debugging a Java program with VS Code
Getting started with Java programs using Visual Studio Code
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Build ruby debug environment with VS Code of Windows 10
Build Java program development environment with Visual Studio Code
Run batch with docker-compose with Java batch
Run VS Code on Docker
Docker management with VS Code
Format Ruby with VS Code
Hello World with VS Code!
Using Mapper with Java (Spring)
Using Docker from Java Gradle
Run Java VM with WebAssembly
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Java web application development environment construction with VS Code (struts2)
A memo to start Java programming with VS Code (2020-04 version)
Talk about using Java input wait (Scanner) in VS Code
Build WebAPP development environment with Java + Spring with Visual Studio Code
Code Java from Emacs with Eclim
Java Spring environment in vs Code
I tried using JOOQ with Gradle
Spring Boot programming with VS Code
Try using Redis with Java (jar)
[Java] Boilerplate code elimination using Lombok
Execute packaged Java code with commands
Using Java with AWS Lambda-Eclipse Preparation
[Java] Boilerplate code elimination using Lombok 2
Html5 development with Java using TeaVM
Getting Started with Docker with VS Code
Java development environment (Mac, VS Code)
Run an application made with Java8 with Java6
Sample code using Minio from Java
Using proxy service with Java crawling
Spring Boot gradle build with Docker
[Gradle] Build a Java project with a configuration different from the convention
Using Java with AWS Lambda-Implementation-Check CloudWatch Arguments
[Java] Create an executable module with Gradle
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
Link Java and C ++ code with SWIG
Using JupyterLab + Java with WSL on Windows 10
Java development with Codenvy: Hello World! Run
Game development with two people using java 2