Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit d03737f

Browse files
authored
Merge pull request #485 from microsoft/clantz/script-improvments
Further refinements to common scripts, update java to use new gradle/maven scripts
2 parents 2dd74c1 + a17e43a commit d03737f

File tree

19 files changed

+559
-239
lines changed

19 files changed

+559
-239
lines changed

containers/java/.devcontainer/Dockerfile

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,25 @@ ARG USER_GID=$USER_UID
1111

1212
# Install needed packages and setup non-root user. Use a separate RUN statement to add your own dependencies.
1313
COPY library-scripts/*.sh /tmp/library-scripts/
14-
RUN apt-get update \
15-
&& /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
16-
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts
14+
RUN /bin/bash /tmp/library-scripts/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
15+
&& if [ ! -d "/docker-java-home" ]; then ln -s "${JAVA_HOME}" /docker-java-home; fi \
16+
&& apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts/common-debian.sh
1717

1818
# [Optional] Install Maven
1919
ARG INSTALL_MAVEN="false"
2020
ARG MAVEN_VERSION=3.6.3
21-
ARG MAVEN_DOWNLOAD_SHA="dev-mode"
22-
ENV MAVEN_HOME /usr/share/maven \
23-
MAVEN_CONFIG /root/.m2
24-
COPY maven-settings.xml /usr/share/maven/ref/
25-
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then \
26-
mkdir -p /usr/share/maven /usr/share/maven/ref \
27-
&& curl -fsSL -o /tmp/apache-maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
28-
&& ([ "${MAVEN_DOWNLOAD_SHA}" = "dev-mode" ] || echo "${MAVEN_DOWNLOAD_SHA} */tmp/apache-maven.tar.gz" | sha512sum -c - ) \
29-
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
30-
&& rm -f /tmp/apache-maven.tar.gz \
31-
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn; \
32-
fi
21+
ARG MAVEN_DOWNLOAD_SHA="no-check"
22+
ENV MAVEN_HOME=/usr/local/share/maven
23+
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then /bin/bash /tmp/library-scripts/maven-debian.sh ${MAVEN_VERSION} ${MAVEN_HOME} ${USERNAME} ${MAVEN_DOWNLOAD_SHA}; fi \
24+
&& rm -f /tmp/library-scripts/maven-debian.sh
3325

3426
# [Optional] Install Gradle
3527
ARG INSTALL_GRADLE="false"
3628
ARG GRADLE_VERSION=5.4.1
37-
ARG GRADLE_DOWNLOAD_SHA="dev-mode"
38-
ENV GRADLE_HOME=/opt/gradle
39-
RUN if [ "${INSTALL_GRADLE}" = "true" ]; then \
40-
curl -sSL --output gradle.zip "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip" \
41-
&& ([ "${GRADLE_DOWNLOAD_SHA}" = "dev-mode" ] || echo "${GRADLE_DOWNLOAD_SHA} *gradle.zip" | sha256sum --check - ) \
42-
&& unzip gradle.zip \
43-
&& rm gradle.zip \
44-
&& mv "gradle-${GRADLE_VERSION}" "${GRADLE_HOME}/" \
45-
&& ln -s "${GRADLE_HOME}/bin/gradle" /usr/bin/gradle; \
46-
fi
47-
48-
# Allow for a consistant java home location for settings - image is changing over time
49-
RUN if [ ! -d "/docker-java-home" ]; then ln -s "${JAVA_HOME}" /docker-java-home; fi
29+
ARG GRADLE_DOWNLOAD_SHA="no-check"
30+
ENV GRADLE_HOME=/usr/local/share/gradle
31+
RUN if [ "${INSTALL_GRADLE}" = "true" ]; then /bin/bash /tmp/library-scripts/gradle-debian.sh ${GRADLE_VERSION} ${GRADLE_HOME} ${USERNAME} ${GRADLE_DOWNLOAD_SHA}; fi \
32+
&& rm -f /tmp/library-scripts/gradle-debian.sh
5033

5134
# [Optional] Install Node.js for use with web applications - update the INSTALL_NODE arg in devcontainer.json to enable.
5235
ARG INSTALL_NODE="false"
@@ -56,7 +39,7 @@ ENV NVM_SYMLINK_CURRENT=true \
5639
PATH=${NVM_DIR}/current/bin:${PATH}
5740
COPY library-scripts/node-debian.sh /tmp/library-scripts/
5841
RUN if [ "$INSTALL_NODE" = "true" ]; then /bin/bash /tmp/library-scripts/node-debian.sh "${NVM_DIR}" "${NODE_VERSION}" "${USERNAME}"; fi \
59-
&& rm -rf /var/lib/apt/lists/* /tmp/library-scripts
42+
&& apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts/node-debian.sh
6043

6144
# [Optional] Uncomment this section to install additional OS packages.
6245
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
7+
# Syntax: ./gradle-debian.sh <gradle version> [gradle home] [non-root-user] [gradle SHA 256]
8+
9+
GRADLE_VERSION=$1
10+
GRADLE_HOME=${2:-"/usr/local/share/gradle"}
11+
USERNAME=${3:-"vscode"}
12+
GRADLE_DOWNLOAD_SHA=${4:-"no-check"}
13+
14+
set -e
15+
16+
if [ -d "${GRADLE_HOME}" ]; then
17+
echo "Gradle already installed."
18+
exit 0
19+
fi
20+
21+
if [ -z "$1" ]; then
22+
echo -e "Required argument missing.\n\ngradle-debian.sh <gradle version> [gradle home] [non-root-user] [gradle SHA 256]"
23+
exit 1
24+
fi
25+
26+
if [ "$(id -u)" -ne 0 ]; then
27+
echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
28+
exit 1
29+
fi
30+
31+
# Treat a user name of "none" or non-existant user as root
32+
if [ "${USERNAME}" = "none" ] && ! id -u ${USERNAME} > /dev/null 2>&1; then
33+
USERNAME=root
34+
fi
35+
36+
# Install curl, apt-get dependencies if missing
37+
if ! type curl > /dev/null 2>&1; then
38+
export DEBIAN_FRONTEND=noninteractive
39+
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
40+
apt-get update
41+
fi
42+
apt-get -y install --no-install-recommends ca-certificates curl gnupg2
43+
fi
44+
45+
# Function to su if user exists and is not root
46+
suIf() {
47+
if [ "${USERNAME}" != "root" ]; then
48+
su ${USERNAME} -c "$@"
49+
else
50+
"$@"
51+
fi
52+
}
53+
54+
# Install Gradle
55+
echo "Downloading Gradle..."
56+
suIf "$(cat \
57+
<< EOF
58+
mkdir -p /tmp/downloads
59+
curl -sSL --output /tmp/downloads/archive-gradle.zip https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip
60+
([ "${GRADLE_DOWNLOAD_SHA}" = "no-check" ] || echo "${GRADLE_DOWNLOAD_SHA} */tmp/downloads/archive-gradle.zip" | sha256sum --check - )
61+
unzip -q /tmp/downloads/archive-gradle.zip -d /tmp/downloads/
62+
EOF
63+
)"
64+
mv -f /tmp/downloads/gradle* ${GRADLE_HOME}
65+
chown ${USERNAME}:root ${GRADLE_HOME}
66+
ln -s ${GRADLE_HOME}/bin/gradle /usr/local/bin/gradle
67+
rm -rf /tmp/downloads
68+
echo "Done."
69+
70+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
#-------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
5+
#-------------------------------------------------------------------------------------------------------------
6+
7+
# Syntax: ./maven-debian.sh <maven version> [maven home] [non-root user] [maven SHA 512]
8+
9+
MAVEN_VERSION=$1
10+
MAVEN_HOME=${2:-"/usr/local/share/maven"}
11+
USERNAME=${3:-"vscode"}
12+
MAVEN_DOWNLOAD_SHA=${4:-"no-check"}
13+
14+
set -e
15+
16+
if [ -d "${MAVEN_HOME}" ]; then
17+
echo "Maven already installed."
18+
exit 0
19+
fi
20+
21+
if [ -z "$1" ]; then
22+
echo -e "Required argument missing.\n\nmaven-debian.sh <maven version> [maven home] [non-root user] [maven SHA 512]"
23+
exit 1
24+
fi
25+
26+
if [ "$(id -u)" -ne 0 ]; then
27+
echo -e 'Script must be run a root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
28+
exit 1
29+
fi
30+
31+
# Treat a user name of "none" or non-existant user as root
32+
if [ "${USERNAME}" = "none" ] && ! id -u ${USERNAME} > /dev/null 2>&1; then
33+
USERNAME=root
34+
fi
35+
36+
# Install curl, apt-get dependencies if missing
37+
if ! type curl > /dev/null 2>&1; then
38+
export DEBIAN_FRONTEND=noninteractive
39+
if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
40+
apt-get update
41+
fi
42+
apt-get -y install --no-install-recommends ca-certificates curl gnupg2
43+
fi
44+
45+
# Function to su if user exists and is not root
46+
suIf() {
47+
if [ "${USERNAME}" != "root" ]; then
48+
su ${USERNAME} -c "$@"
49+
else
50+
"$@"
51+
fi
52+
}
53+
54+
# Creat folder, add maven settings
55+
mkdir -p ${MAVEN_HOME} ${MAVEN_HOME}/ref
56+
tee ${MAVEN_HOME}/ref/maven-settings.xml > /dev/null \
57+
<< EOF
58+
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
59+
<localRepository>${MAVEN_HOME}/ref/repository</localRepository>
60+
</settings>
61+
EOF
62+
chown -R ${USERNAME}:root ${MAVEN_HOME}
63+
64+
# Install Maven
65+
echo "Downloading Maven..."
66+
suIf "$(cat \
67+
<< EOF
68+
curl -fsSL -o /tmp/maven.tar.gz https://archive.apache.org/dist/maven/maven-3/${MAVEN_VERSION}/binaries/apache-maven-${MAVEN_VERSION}-bin.tar.gz
69+
([ "${MAVEN_DOWNLOAD_SHA}" = "dev-mode" ] || echo "${MAVEN_DOWNLOAD_SHA} */tmp/maven.tar.gz" | sha512sum -c - )
70+
tar -xzf /tmp/maven.tar.gz -C ${MAVEN_HOME} --strip-components=1
71+
rm -f /tmp/maven.tar.gz
72+
EOF
73+
)"
74+
ln -s ${MAVEN_HOME}/bin/mvn /usr/local/bin/mvn
75+
echo "Done."

containers/java/.devcontainer/maven-settings.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

containers/java/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,6 @@ Given how frequently web applications use Node.js for front end code, this conta
9898

9999
5. Finally, press <kbd>F1</kbd> and run **Remote-Containers: Reopen Folder in Container** to start using the definition.
100100

101-
6. If you want to include maven or gradle build tools into your dev container, please uncomment the corresponding steps from the `containers/java-11/.devcontainer/Dockerfile`, and run **Remote-Containers: Rebuild Container** to rebuild the dev container.
102-
103101
## Testing the definition
104102

105103
This definition includes some test code that will help you verify it is working as expected on your system. Follow these steps:

0 commit comments

Comments
 (0)