How to Compile a Maven or Gradle Project from GitHub

August 5, 2021

Sometimes you come across a Java project that is managed with Maven (pom.xml) or Gradle (build.gradle) and you need to be able to compile the full project into a jar.

As it turns out, that's pretty easy. Just follow along.

Install the JDK

First, you need the JDK which allows you to compile Java source files into Java class files, which are ultimately packed into a jar. If you don't already have it, just get the latest version (JDK 15) here: https://jdk.java.net/15/.

These instructions will be for Windows 10, but other Operating Systems will be similar:

  1. Download Windows\x64
  2. Make a new folder called .tools inside of C:\Users\YOUR_USERNAME
  3. Move the downloaded ZIP file to the new .tools directory
  4. Unzip the file to get a new folder. Delete the ZIP file.
  5. Move the folder called jdk-15.0.1, inside of the folder, to the outside of the folder. Then, delete the folder so you only have jdk-15.0.1 inside of .tools.
  6. Right-click This PC and press Properties.  Click Advanced system settings. Click Environment Variables....
  7. Inside of the User variables for YOUR_USERNAME section, press New....
  8. Use JAVA_HOME for the Variable name and C:\Users\YOUR_USERNAME.tools\jdk-15.0.1 for the Variable value.
  9. Press OK. Select Path and press Edit.... Press New. Enter %JAVA_HOME%\bin.
  10. Press OK three times. Close settings.
  11. Open CMD and type java -version.

You should see something like:

openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9-18)
OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)

You have survived installing the JDK. Let's move on to Maven and Gradle.

Install Maven and Gradle

These steps are going to be surprisingly similar to installing the JDK, so I'll hold out on the full instructions. You can use the previous set of steps, with different values that I will enumerate. Check it out:

Let's start with Maven:

  1. Download it here: https://maven.apache.org/download.cgi (grab the Binary zip archive).
  2. Copy the apache-maven-3.6.3 folder inside to your .tools directory from before.
  3. Go to the Environment Variables and add a new User variable called MVN_HOME with the value C:\Users\YOUR_USERNAME.tools\apache-maven-3.6.3.
  4. Edit Path, add a new entry with the value %MVN_HOME%\bin.
  5. Close all of that, open CMD, and type mvn --version.

You should see something like:

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Users\YOUR_USERNAME\.tools\apache-maven-3.6.3
Java version: 15.0.1, vendor: Oracle Corporation, runtime: C:\Users\YOUR_USERNAME>\.tools\jdk-15.0.1
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

That's it for Maven! Let's do Gradle:

  1. Download it here: https://gradle.org/releases/ (find the latest (i.e. v6.8) and grab the binary-only).
  2. Copy the gradle-6.8 folder inside to your .tools directory from before.
  3. Go to Environment Variables and add a new User variable called GRADLE_HOME with the value C:\Users\YOUR_USERNAME.tools\gradle-6.8.
  4. Edit Path, add a new entry with the value %GRADLE_HOME%\bin.
  5. Close all of that, open CMD, and type gradle --version.

You should see something like:

Welcome to Gradle 6.8!

Here are the highlights of this release:
- Faster Kotlin DSL script compilation
- Vendor selection for Java toolchains
- Convenient execution of tasks in composite builds
- Consistent dependency resolution

For more details see https://docs.gradle.org/6.8/release-notes.html

------------------------------------------------------------
Gradle 6.8
------------------------------------------------------------
Build time: 2021-01-08 16:38:46 UTC
Revision: b7e82460c5373e194fb478a998c4fcfe7da53a7e
Kotlin: 1.4.20\nGroovy: 2.5.12
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 15.0.1 (Oracle Corporation 15.0.1+9-18)
OS: Windows 10 10.0 amd64\

And we're done with Gradle. We're on the home stretch. Last on the list is getting the source code from GitHub with Git.

Installing Git

If you don't already have Git installed, you will need to follow this section. Otherwise skip it.

  1. Download Git here: https://git-scm.com/ (press Download 2.30.0 for Windows).
  2. Install it as usual. When you get to Adjusting your PATH environment, ensure that Git from the command line and also from 3rd-party software is selected. Continue with the rest of the defaults.
  3. Open CMD, and type git --version.

You should see something like:

git version 2.30.0.windows.1

That's it for Git. Nice one.

Compiling the Maven or Gradle Project

This is the finish line. You're almost there. Find the project that you want to compile on GitHub.

For example's sake, we'll use https://github.com/PyvesB/advanced-achievements. (Please note: if the program you are planning to compile is a paid resource but has its source released, this generally means you are welcome to compile it yourself. Do also note, however, that this implies you do not and will not require support, as none shall be given to you. This is a good way to see if you end up liking a program (or plugin, in the Minecraft community). That said, if you do indeed end up liking the program you should pay for it if you have the means.)

  1. Go to the GitHub page https://github.com/PyvesB/advanced-achievements.
  2. In the top right, you should see a green button that says Code with a down arrow next to it. Click that, ensure HTTPS is selected, and copy the link to your clipboard. For our example, it should be https://github.com/PyvesB/advanced-achievements.git.
  3. Open CMD and type cd Documents to go to your Documents folder.
  4. Then, type git clone https://github.com/PyvesB/advanced-achievements.git.
  5. You should now have a new folder called advanced-achievements in your Documents folder.
  6. Type cd advanced-achievements to enter the new folder.

Earlier, you figured out whether you should use Maven (a pom.xml file exists) or Gradle (a build.gradle file exists).

Your next and last step is:

For Maven:

  1. Type mvn clean install.
  2. Get your compiled JAR from the target folder. You'll want the larger jar. Note: If the project is multi-module, like our example Advanced Achievements is, then you'll have multiple folders with each module's jar. In Advanced Achievement's case, you probably want the plugin. So, go into the advanced-achievements-plugin folder, and then the target folder, and grab the largest JAR file.

For Gradle:

  1. Does the project have a gradlew file in the same folder as the build.gradle file? If so, go to step 2. If not, go to step 3.
  2. Type ./gradlew clean build shadowJar. Go to step 4.
  3. Type gradle clean build shadowJar.
  4. Get your compiled JAR from the build/libs folder. You'll want the larger jar. Same comment as before. If the project is multi-module, enter the folder that you want and then the build/libs folder inside of that will have the jar you so desire.

That's it!

Nice. You should have your jar now! Enjoy.

Mastodon