Build Java x Spring x VSCode x Gradle on Docker (1)

It is easy to prepare the Java and Spring development environment with Docker. In this article, I also included VS Code in Docker. Since the entire development environment can be managed with Docker, it is recommended because it is easy to manage.

Character

What is Docker

It is an environment to run an application in a container on Linux. Applications and libraries can be consolidated in the same container and reused. https://ja.wikipedia.org/wiki/Docker

VSCode https://ja.wikipedia.org/wiki/Visual_Studio_Code It is an Editor made by Microsoft. If you install Dart Plugin, you can use the interpolation function etc. and it is convenient.

Code-Server It's a great guy who can run VS Code as a web service. https://github.com/cdr/code-server

Try to create an environment

https://github.com/kyorohiro/my-code-server/tree/master/java_spring

(1) Write a dockerfile

FROM openjdk:11

#
# GRADLE
RUN mkdir /works
WORKDIR /works

RUN apt-get update
RUN apt-get install -y curl wget gnupg less lsof net-tools git apt-utils -y
RUN apt-get install -y build-essential libssl-dev curl git-core
RUN apt-get install -y emacs

RUN wget https://downloads.gradle.org/distributions/gradle-5.4.1-bin.zip
RUN unzip gradle-5.4.1-bin.zip
ENV PATH="/works/gradle-5.4.1/bin:${PATH}"

#
#May not be needed
# https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-For dependency
# nodejs for vscode plugin
RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh
RUN sh install_nvm.sh
ENV NVM_DIR="/root/.nvm"

RUN chmod o+x /root/.nvm/nvm.sh
RUN /bin/bash -c "source /root/.nvm/nvm.sh && nvm install v12.0.0"
ENV PATH="/root/.nvm/versions/node/v12.0.0/bin/:${PATH}"

#
# code-server
RUN wget https://github.com/cdr/code-server/releases/download/1.939-vsc1.33.1/code-server1.939-vsc1.33.1-linux-x64.tar.gz
RUN tar xzf code-server1.939-vsc1.33.1-linux-x64.tar.gz -C ./ --strip-components 1

(1) Run docker image

docker build -t java_spring_vscode .
docker run -p 8443:8443 -p 8080:8080 -it java_spring_vscode bash

(2) Start vscode using code-server

mkdir /works/w
/works/code-server /works/w --allow-http --no-auth

(3) and open'http://127.0.0.1:8443/' in your browser

root_page.jpg

Install Java Plugin

(1) https://marketplace.visualstudio.com/items?itemName=redhat.java

(2) May be unnecessary

.vscode/settings.json

{
    "java.home": "/usr/lib/jvm/java-11-openjdk-amd64",
    "java.maven.downloadSources": true,
    "java.import.gradle.enabled": true,
    "java.errors.incompleteClasspath.severity": "warning",
    "java.configuration.updateBuildConfiguration": "automatic"
}

Let's write HelloWorld.

(1) Terminal -> New Terminal on VSCODE

(2) On Terminal

$ gradle init --type java-application
$ gradle build

(3) Add eclipse to build.gradle for redhat java plugin

plugins {
    id 'java'
    id 'application'
    id 'eclipse'
}

repositories {
    jcenter()
}

dependencies {
    implementation 'com.google.guava:guava:27.0.1-jre'
   testImplementation 'junit:junit:4.12'
}

mainClassName = 'hello.App'

(4) Try running Hello World !!.

$ gradle eclipse
$ gradle build
$ gradle run

Let's run Spring Boot.

(1) Modify build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 11
targetCompatibility = 11

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

(2) Downloading a package

$ gradle build
$ gradle eclipse

(3) src/main/java/hello/App.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

(4) src/main/java/hello/Greeting.java

package hello;

public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId(){
        return this.id;
    }

    public String getContent() {
        return this.content;
    }
}

(5) src/main/java/hello/GreetingController.java

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

(6) Try to start

 $ SERVER_PORT=8080 SERVER_HOST=0.0.0.0 gradle tasks bootRun

(7) and, open 'http://127.0.0.1:8080/greeting' at your browser

spring_page.jpg

that's all.

Code-Server was very convenient. https://github.com/cdr/code-server

the end.

The code for this time is summarized below. https://github.com/kyorohiro/my-code-server/tree/master/java_spring

PS

[a] If you want to resume

$ docker ps -a
check id and
$ docker start < id > 
$ docker exec -it < id > bash

[b] If you want to change the settings

$ docker commit < id > java_spring_vscode_xxx
$ docker run -p 8443:8443 -p 8080:8080 -it java_spring_vscode_xxx bash

[c] Mount

$ docker run -p 8443:8443 -p 8080:8080 -v /Users/kyorohiro/w/xxx:/app/w -it java_spring_vscode bash

Recommended Posts

Build Java x Spring x VSCode x Gradle on Docker (1)
Build Clang x VSCode on Docker (1)
Spring Boot gradle build with Docker
Build Spring Boot + Docker image in Gradle
First gradle build (Java)
Build a development environment for Docker, java, vscode
Spring + Gradle + Java Quick Start
Oracle Java 8 on Docker Ubuntu
[Vscode xdebug3.0 x PHP x Docker]
Using Docker from Java Gradle
Build a Java project with Gradle
Build Unity development environment on docker
Build an ASP.net Core Web API environment on Docker (VSCode) Part 1
Build an ASP.NET Core Web API environment on Docker (VSCode) Part 2
When Gradle build stops when importing on Mac
spring × docker
CICS-Run Java applications-(3) Build management with Gradle
Build a Java development environment on Mac
Build Java 8 development environment on AWS Cloud9
Spring Boot + Docker Java development environment construction
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Spring Java
Build Redmine code reading environment on Docker
Build OpenCV with Java Wrapper on Ubuntu 18.04
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Build an environment with Docker on AWS
Build an Ultra96v2 development environment on Docker 1
Build Web Application Server (Java) on VPS
Try to build Java8 environment on Amazon Linux2
Put Java 11 and spring tool suite on mac
[Gradle] Build operations often performed in Java projects
Introducing Rspec with Ruby on Rails x Docker
Build a Laravel / Docker environment with VSCode devcontainer
Build and test Java + Gradle applications with Wercker
[Java] Build Java development environment on Ubuntu & check execution
Using Gradle with VS Code, build Java → run
Build Spring Boot project by environment with Gradle
Java tips-Create a Spring Boot project in Gradle
Build a container for Docker x Laravel phpMyAdmin
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Docker on Ubuntu18.04 on WSL2 and VSCode installation instructions
Build a Java runtime environment on Sakura VPS
LINE Bot x Java (Spring Boot) construction procedure
Liberty on Docker
Create a Java (Gradle) project with VS Code and develop it on a Docker container
vagrant java build
Selenium x Java
/ n \ n docker java
Build ELK stack on Mac OS X and output JAVA log to Elasticsearch (log4j2)
[Java] Spring DI ③
Redmine on Docker
Cache Gradle dependent files to speed up docker build
Build Java development environment with VS Code on Mac
Build VS Code + WSL + Java + Gradle environment from scratch
How to build docker environment with Gradle for intelliJ
Easily build Redmine on Windows using WSL2 and Docker
VSCode Java Debugger for Java Build failed Causes and countermeasures
Build Java development environment with WSL2 Docker VS Code
[Docker] Build an Apache container on EC2 using dockerfile
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Try to build a Java development environment using Docker