Most programmers do not need the information on this page because they will simply download the appropriate jar(s) or use ANTLR through maven (via ANTLR's antlr4-maven-plugin). If you would like to fork the project and fix bugs or tweak the runtime code generation, then you will almost certainly need to build ANTLR itself. There are two components:
- the tool that compiles grammars down into parsers and lexers in one of the target languages
- the runtime used by those generated parsers and lexers.
I will assume that the root directory is /tmp
for the purposes of explaining how to build ANTLR in this document.
The first step is to get the Java source code from the ANTLR 5 repository at github. You can download the repository from github, but the easiest thing to do is simply clone the repository on your local disk:
$ cd /tmp
/tmp $ git clone https://github.com/antlr/antlr5.git
Cloning into 'antlr5'...
remote: Counting objects: 61480, done.
remote: Total 61480 (delta 0), reused 0 (delta 0), pack-reused 61480
Receiving objects: 100% (61480/61480), 31.24 MiB | 7.18 MiB/s, done.
Resolving deltas: 100% (32970/32970), done.
Checking connectivity... done.
Checking out files: 100% (1427/1427), done.
If you are starting from a clean, minimum Ubuntu OS, check your environment.
$ sudo apt-get update
$ # Get Java
$ java > /dev/null 2>&1
$ if [[ "$?" != "0" ]]; then sudo apt install -y openjdk-21-jre-headless; fi
$ # Get Mvn
$ mvn > /dev/null 2>&1
$ if [[ "$?" != "0" ]]; then sudo apt install -y maven; fi
The current maven build seems complicated to me because there is a dependency of the project on itself. The runtime tests naturally depend on the current version being available but it won't compile without the current version. Once you have the generated/installed jar, mvn builds but otherwise there's a dependency on what you are going to build. You will get this error when you try to clean but you can ignore it:
[INFO] ANTLR 5 Runtime Tests (4th generation) ............. FAILURE [ 0.073 s]
...
[ERROR] Plugin org.antlr:antlr5-maven-plugin:0.0.1-SNAPSHOT or one of its dependencies could not be resolved: Could not find artifact org.antlr:antlr5-maven-plugin:jar:0.0.1-SNAPSHOT -> [Help 1]
To be super squeaky clean, you can wipe out the repository cache, then do the build:
$ export MAVEN_OPTS="-Xmx1G" # don't forget this on linux
cd /tmp/antlr5 # or wherever you have the software
rm -rf ~/.m2/repository/org/antlr*
mvn clean
mvn -DskipTests install
NOTE: We do install
not compile
as tool tests and such refer to modules that must be pulled from the maven install local cache.
Once you have completed this process once and there is a jar hanging around in the repository cache.
To skip the tests (which require all the target languages be installed) and install into local repository ~/.m2/repository/org/antlr
, do this:
$ export MAVEN_OPTS="-Xmx1G" # don't forget this on linux
$ mvn install -DskipTests # make sure all artifacts are visible on this machine
You should see these jars (when building 4.6-SNAPSHOT):
/Users/parrt/.m2/repository/org/antlr $ find antlr5* -name '*.jar'
antlr5-maven-plugin/0.0.1-SNAPSHOT/antlr5-maven-plugin-0.0.1-SNAPSHOT.jar
antlr5-runtime-testsuite/0.0.1-SNAPSHOT/antlr5-runtime-testsuite-0.0.1-SNAPSHOT-tests.jar
antlr5-runtime-testsuite/0.0.1-SNAPSHOT/antlr5-runtime-testsuite-0.0.1-SNAPSHOT.jar
antlr5-runtime/0.0.1-SNAPSHOT/antlr5-runtime-0.0.1-SNAPSHOT.jar
antlr5-tool-testsuite/0.0.1-SNAPSHOT/antlr5-tool-testsuite-0.0.1-SNAPSHOT.jar
antlr5/0.0.1-SNAPSHOT/antlr5-0.0.1-SNAPSHOT-tests.jar
antlr5/0.0.1-SNAPSHOT/antlr5-0.0.1-SNAPSHOT.jar
To build without running the tests (saves a lot of time), do this:
$ mvn -DskipTests install
After download ANTLR source, just "import project from existing sources" and click on the "Maven Projects" tab in right gutter of IDE. It should build stuff in the background automatically and look like: