Create a Java (Maven) project with VS Code and develop it on a Docker container

Introduction

Looking back on 2020, the shock of encountering the Remote Development Extension Pack of VS Code cannot be described in words.

--Remote-SSH --Whether it's Linux on Hyper-V or Linux on the cloud, VS Code is the terminal, editor, and explorer instead of SSH login → editing with vi from the terminal. Impact --Remote-WSL --If you have a local WSL environment instead of Hyper-V, if VS Code is installed on the Windows side, you can start VS Code from the directory with code . (above). Similar) ... shock --Remote-Containers --Even if you don't prepare Hyper-V or WSL, or you don't prepare Dockerfile (you may prepare), start the container and make the local directory transparent on the started container Impact that can be developed while looking at

All three shocks were powerful, but for me, there is a development container sample with a development environment made by MS for these 8 languages in Remote Container. , I'll conquer everything! I was enthusiastic, but I couldn't do anything because of my busy schedule, so I started with Java (Maven).

Create a repository on GitHub

Create a new repository on GitHub. I've named it CCI-SMP-Java-Maven here (CCI stands for CircleCI, which I'm going to use next time, and SMP stands for SaMPle). You can use any name you like. In addition, Maven template is specified in Add .gitignore. GitHub - New Repository

Pull locally and launch VS Code

Pull the created repository to your local environment. For the environment in this article

git clone [email protected]:mfunaki/CCI-SMP-Java-Maven.git

It will be like that. After pulling, go to the created directory (eg cd CCI-SMP-Java-Maven) and

code .

Start VS Code from the directory where your local repository is located. Menu ** Terminal ** → You can also start the terminal with ** new terminal ** (in the photo below, the host name of my local environment, MAYOCT-P73 is displayed). Local Repository

Move the development environment from local to container

Click the green > < at the bottom left to display the command palette and display the options related to Remote Development. Select ** Remote-Containers: Open Folder in Container ** from these. I will. Remote Development

You will be asked from which folder to open VS Code, so leave the current directory specified and press the ** Open ** button. Then you will be asked for the container environment (automatically generated and added), so select ** Java **. Java

Next, you will be asked for the Java version, ** 15 **, and then Maven, Gradle, Node.js, whichever you want to install, so select ** Install Maven ** here. Then VS Code is restarted, but the description ** Dev Container: Java ** is added to the green > < at the bottom left, indicating that the bash prompt on the terminal is also in the container. It changed to the notation. VSCode on Java Container

Subsequent work is in the container. If you look at the automatically generated .devcontainer/Dockerfile, you can see that your world is Microsoft-made mcr.microsoft.com/vscode/devcontainers/java:0-15.

# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.154.0/containers/java/.devcontainer/base.Dockerfile

# [Choice] Java version: 11, 15
ARG VARIANT="15"
FROM mcr.microsoft.com/vscode/devcontainers/java:0-${VARIANT}

# [Option] Install Maven
ARG INSTALL_MAVEN="false"
ARG MAVEN_VERSION=""
# [Option] Install Gradle
ARG INSTALL_GRADLE="false"
ARG GRADLE_VERSION=""
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then su vscode -c "source /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven \"${MAVEN_VERSION}\""; fi \
    && if [ "${INSTALL_GRADLE}" = "true" ]; then su vscode -c "source /usr/local/sdkman/bin/sdkman-init.sh && sdk install gradle \"${GRADLE_VERSION}\""; fi

# [Option] Install Node.js
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "source /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

Check the Java and Maven (mvn) versions on the terminal. Java Version (I don't know how the behavior changes when the value of ARG INSTALL_MAVEN =" false " in the Dockerfile is true/false.)

Create a sample in the repository (on the container)

We will create a sample using Maven in 5 Minutes on the Maven official website as an example.

At the beginning of Maven in 5 Minutes, there is a section called Creating a Project, so enter the line starting with mvn on the terminal and press Enter.

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Then (after this and that and after downloading) it looks like this. mvn result

Also, if you look at the explorer on the left, --The my-app directory has been created --The src (for source code) directory has been created under the my-app directory. --Under the src directory, under the main/java/com/mycompany/app directory, under the App.java (application source code), --Under the src directory, under the test/java/com/mycompany/app directory, AppTest.java (application test code) --Pom.xml file (configuration file used by Maven) directly under the my-app directory You can see that each is created. Explorer

Build the sample manually and check the operation

Well then, it's a build. In the terminal at the bottom right of Visual Studio Code, change to the sample application directory.

cd my-app

Next, actually proceed with the build.

mvn package

Along the way, the automatically generated test will be executed. TESTS Finally the build is complete (that is, / workspaces/CCI-CMP-Java-Maven/my-app/target/my-app-1.0-SNAPSHOT.jar is generated). BUILD SUCCESS Next, move on to operation check.

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

After that, it is OK if Hello World! Is output.

at the end

The work on the container is actually done by mounting the local directory (git clone), so click the green > < at the bottom left and ** Remote- from the command palette. Select Containers: Reopen Locally **. Command Palette

Add, update, commit, and push to GitHub. If all goes well, you'll see the .devcontainer and my-app directories on your GitHub repository. GitHub Next time, I'll talk about build and test automation using CircleCI. See you again. Goodbye.

Recommended Posts

Create a Java (Maven) project with VS Code and develop it on a Docker container
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Workspace setting location when connecting remotely with VS Code and working on a Docker container
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Create a Maven project with a command
Create a flyway jar with maven and docker build (migrate) with docker-maven-plugin
Building a haskell environment with Docker + VS Code on Windows 10 Home
Create a Docker Image for redoc-cli and register it on Docker Hub
Starting with installing Docker on EC2 and running Yellowfin in a container
Create a program to post to Slack with GO and make it a container
Create a Lambda Container Image based on Java 15
Try debugging a Java program with VS Code
Build a Java development environment with VS Code
Connect with VS Code from a Windows client to Docker on another server
Getting started with Maven (until you create a Java project and combine external libraries into a single executable JAR)
Build Java development environment with VS Code on Mac
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Build Java development environment with WSL2 Docker VS Code
[Environment construction] Build a Java development environment with VS Code!
Create a private key / public key in CentOS8.2 and connect to SSH with VS Code
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
Run VS Code on Docker
Create a container image for arm64 of Kibana and register it in GitHub Container Registry. Start Elastic Stack with Docker Compose on Raspberry Pi 4 (64bit)
Docker management with VS Code
Create a VS Code Plugin.
Maybe it works! Create an image with Docker and share it!
[Docker] Build a site on Hugo and publish it on GitHub
Try Hello World using plain Java on a Docker container
A memo to start Java programming with VS Code (2020-04 version)
Until you build a project described in scala with Maven and execute it with the scala command.
Create a Vue3 environment with Docker!
Build Metabase with Docker on Lightsail and make it https with nginx
Build a Java project with Gradle
[Note] A story about changing Java build tools with VS Code
[Swift] Create a project with Xcode (ver 12.1) and display "Hello, World!"
Create a high-performance enum with fields and methods like Java with JavaScript
Run React on a Docker container
Create a Java project using Eclipse
Java build with mac vs code
Run PureScript on a Docker container
I tried deploying a Docker container on Lambda with Serverless Framework
Getting Started with Docker with VS Code
[Be careful about changing the version of Xdebug! ] Create a development environment with Xdebug3 + docker + VS Code
Develop gRPC on Windows + Java 7 + Maven 2
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
Access MySQL on a Docker container from a local (host OS) Java program
[Java] Create a jar file with both compressed and uncompressed with the jar command
[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
I wrote a Lambda function in Java and deployed it with SAM
Create a java web application development environment with docker for mac part2
Prepare Java development environment with VS Code
Link Java and C ++ code with SWIG
Create a MySQL environment with Docker from 0-> 1
Quick build maven project using maven docker container
Hello World on Mac VS Code Java
Install Docker and create Java runtime environment
Getting started with Gradle (until you create a Java project and combine external libraries into one executable JAR)
Create a Docker container for your development web server in Ansible on MacOS
Monitor the Docker container and SystemD process on the same host with Zabbix on Ubuntu.
Create an ARM-cpu environment with qemu on mac and run java [Result → Failure]
Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation