Looking back on 2020, the shock of encountering the Remote Development Extension Pack of VS Code cannot be described in words.
For me, with Remote Container these 8 languages, I will conquer all of them because there is a development container sample with a development environment made by MS. Although I was enthusiastic, I was not able to do anything due to my daily busyness, so I first worked on Java (Maven), but here I will try working on Java (gradle) To
-Create a Java (Maven) project with VS Code and develop it on a Docker container
Create a new repository on GitHub. I've named it CCI-SMP-Java-Gradle
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-Gradle.git
It will be like that. When you're done pulling, go to the directory where it was created (for example, cd CCI-SMP-Java-Gradle
) 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 Gradle (gradle
) versions on the terminal.
(I don't know how the behavior changes when the value of ARG INSTALL_GRADLE =" false "
in the Dockerfile is true/false.)
We will create a sample using Building Java Applications Sample on the Gradle official website as an example.
Open a terminal on VSCode, create a project folder (demo
) in it, and go to the project folder.
Run gradle init
to initialize the project.
--For Select type of project to generate, select 2: application. --For Select implementation language, use the default 3: Java, --For Split functionality across multiple subprojects, use the default of 2: no. --For Select build script DSL (select the domain-specific language (DSL) that writes the build script), use the default 1: Groovy. --For Select test framework, use the default 1: JUnit 4, --For Project name, enter the default demo, --For Source package, the default demo,
Specify each.
In addition, the directories and files generated under the demo directory are displayed as follows in VS Code Explorer.
Running ./gradlew run
in the terminal will run the run
task (the files needed to run Gradle will be downloaded to the local ~/.gradle/wrapper/dists
folder) and ** Hello World !** is output.
Running ./gradlew build
in the terminal will run the build
task.
As a result, the following two files are generated.
If you have a build problem, the information provided by Gradle to help you investigate is called Build Scan. To publish Build Scan, run ./gradlew build --scan
in your terminal.
On the way
Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? (Publishing Build Scan to scans.gradle.com requires you to accept the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept the terms? )
If you agree, enter yes to proceed. Finally, the URL of Build Scan will be displayed, so if you enter the email address on the screen where you clicked the URL, you will receive an email, and if you click the URL in the email, you will be able to see Build Scan.
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 demo
directories on your GitHub repository.
Next time, I'll talk about build and test automation using CircleCI.
See you again. Goodbye.
Recommended Posts