Antlr in a Container

How to configure ANTLR in a Devcontainer

Updated: 03 September 2023

To install ANTLR in a VSCode DevContainer you need to:

  1. Select a container with Java
  2. Download Antlr
  3. Add the relevant aliases to the bash.bashrc file (even if you’re using zsh)
  4. Set the CLASSPATH in the environment

The following will handle 2, 3, and 4:

1
WORKDIR /usr/local/lib
2
RUN wget https://www.antlr.org/download/antlr-4.9.2-complete.jar
3
4
RUN echo "alias antlr4='java -jar /usr/local/lib/antlr-4.9.2-complete.jar'" >> /etc/bash.bashrc
5
RUN echo "alias grun='java org.antlr.v4.gui.TestRig'" >> /etc/bash.bashrc
6
7
ENV CLASSPATH .:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH

Integrating with the generated Java image from VSCode you will have the following full Dockerfile

.devcontainer/Dockerfile

1
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.166.1/containers/java/.devcontainer/base.Dockerfile
2
3
# [Choice] Java version: 11, 15
4
ARG VARIANT="15"
5
FROM mcr.microsoft.com/vscode/devcontainers/java:0-${VARIANT}
6
7
# [Option] Install Maven
8
ARG INSTALL_MAVEN="false"
9
ARG MAVEN_VERSION=""
10
# [Option] Install Gradle
11
ARG INSTALL_GRADLE="false"
12
ARG GRADLE_VERSION=""
13
RUN if [ "${INSTALL_MAVEN}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven \"${MAVEN_VERSION}\""; fi \
14
&& if [ "${INSTALL_GRADLE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/sdkman/bin/sdkman-init.sh && sdk install gradle \"${GRADLE_VERSION}\""; fi
15
16
# [Option] Install Node.js
17
ARG INSTALL_NODE="true"
18
ARG NODE_VERSION="lts/*"
19
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
20
21
# [Optional] Uncomment this section to install additional OS packages.
22
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
23
# && apt-get -y install --no-install-recommends <your-package-list-here>
24
25
# Install ANTLR
26
WORKDIR /usr/local/lib
27
RUN wget https://www.antlr.org/download/antlr-4.9.2-complete.jar
28
29
RUN echo "alias antlr4='java -jar /usr/local/lib/antlr-4.9.2-complete.jar'" >> /etc/bash.bashrc
30
RUN echo "alias grun='java org.antlr.v4.gui.TestRig'" >> /etc/bash.bashrc
31
32
ENV CLASSPATH .:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH
33
34
# [Optional] Uncomment this line to install global node packages.
35
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1