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.
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
https://github.com/kyorohiro/my-code-server/tree/master/java_spring
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
(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"
}
(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
(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
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