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 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.
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).
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.
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 **.
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.
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.
(I don't know how the behavior changes when the value of ARG INSTALL_MAVEN =" false "
in the Dockerfile is true/false.)
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.
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.
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.
Finally the build is complete (that is, / workspaces/CCI-CMP-Java-Maven/my-app/target/my-app-1.0-SNAPSHOT.jar
is generated).
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.
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 **.
Add, update, commit, and push to GitHub. If all goes well, you'll see the .devcontainer
and my-app
directories on your GitHub repository.
Next time, I'll talk about build and test automation using CircleCI.
See you again. Goodbye.
Recommended Posts