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:
- Select a container with Java
- Download Antlr
- Add the relevant aliases to the
bash.bashrc
file (even if you’re usingzsh
) - Set the
CLASSPATH
in the environment
The following will handle 2
, 3
, and 4
:
1WORKDIR /usr/local/lib2RUN wget https://www.antlr.org/download/antlr-4.9.2-complete.jar3
4RUN echo "alias antlr4='java -jar /usr/local/lib/antlr-4.9.2-complete.jar'" >> /etc/bash.bashrc5RUN echo "alias grun='java org.antlr.v4.gui.TestRig'" >> /etc/bash.bashrc6
7ENV 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.Dockerfile2
3# [Choice] Java version: 11, 154ARG VARIANT="15"5FROM mcr.microsoft.com/vscode/devcontainers/java:0-${VARIANT}6
7# [Option] Install Maven8ARG INSTALL_MAVEN="false"9ARG MAVEN_VERSION=""10# [Option] Install Gradle11ARG INSTALL_GRADLE="false"12ARG GRADLE_VERSION=""13RUN 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}\""; fi15
16# [Option] Install Node.js17ARG INSTALL_NODE="true"18ARG NODE_VERSION="lts/*"19RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi20
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 ANTLR26WORKDIR /usr/local/lib27RUN wget https://www.antlr.org/download/antlr-4.9.2-complete.jar28
29RUN echo "alias antlr4='java -jar /usr/local/lib/antlr-4.9.2-complete.jar'" >> /etc/bash.bashrc30RUN echo "alias grun='java org.antlr.v4.gui.TestRig'" >> /etc/bash.bashrc31
32ENV CLASSPATH .:/usr/local/lib/antlr-4.9.2-complete.jar:$CLASSPATH33
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