Getting started with Java projects and build tools

zhuting
3 min readDec 5, 2020

--

Integrated development environments are outstanding tools that help the development by offloading the mechanical tasks from the developer’s shoulders. I am using IntelliJ IDEA

When a project is more complex than a single class, then it is wise to define a project structure. There are few of build tools. We will discuss a bit about the following

  • Make
  • Maven
  • Gradle

Make was the first build tool, and many ideas that modern Java build tools are based on are coming from the good old make .

The makeprogram was originally created in April 1976. It was created when the major programming language was C, but it is not tied to C or any other language. The description file is usually named Makefile , it might look something like the following

run : hello.jar
java -cp hello.jar HelloWorld

hello.jar : HelloWorld.class
jar -cf hello.jar HelloWorld.class

HelloWorld.class : HelloWorld.java
javac HelloWorld.java

As we do not use make for a Java project, there is no reason to get into more details. The following are the important points that you should remember about make:

  • It defines the dependencies of the individual artifacts (targets) in a declarative way
  • It defines the actions to create the missing artifacts in an imperative way

This structure was invented decades ago and has survived up until now for most of the build tools.

Installing Maven

The easiest way to install Maven is to download the binary in tar.gz format. However, the setting path is not going smooth for me, what I followed the instructions here to successfully setting the path

$ cd ~/Downloads
$ tar xvf apache-maven-3.6.3-bin.tar.gz
$ mv apache-maven-3.6.3 /usr/local/
$ cd /usr/local/bin
$ sudo ln -s ../apache-maven-3.6.3/bin/mvn mvn
$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3Java version: 15.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home

Using Maven

Maven helps you create the skeleton of a new project. To do that, type the following command

mvn archetype:generate

Maven will first download the actually available project types from the network and prompt you to select the one you want to use. Maven offers a default value when it asks for your choice. The default is good for the HelloWorld, and we go for.

The next thing Maven asks for is the group ID /the artifact ID and the version of the project. For example

Define value for property 'groupId': judy.java15.exampleDefine value for property 'artifactId': SortTutorialDefine value for property 'version' 1.0-SNAPSHOT: : 1.0.0-SNAPSHOT

When we have specified all the parameters and confirmed, Maven will generate the files that are needed for the project and display the report.

Take a look at the generated directory structure, you can see that it generated the following 3 files:

  • SortTutorial/pox.xml
  • SortTutorial/src/main/java/judy/java15/example/App.java
  • SortTutorial/src/test/java/judy/java15/example/AppTest.java

As Maven was so kind and generated a sample class for the app, we can compile and run it without actual coding, just to see how we can build the project using Maven bu running

mvn package

We will get the following output

Maven fires up, compiles and packages the project automatically.

Now you can start the code using the following command

➜  SortTutorial java -cp target/SortTutorial-1.0.0-SNAPSHOT.jar judy.java15.example.AppHello World!

Installing Gradle

Gradle installation https://gradle.org/install/

The installation proecess is kinda similar so I will skip.

Finally, type the version to verify the installation

gradle -versionWelcome to Gradle 6.7.1!Here are the highlights of this release:- File system watching is ready for production use- Declare the version of Java your build requires- Java 15 supportFor more details see https://docs.gradle.org/6.7.1/release-notes.html------------------------------------------------------------Gradle 6.7.1

Summary

Maven is new, and it uses a different approach. Maven is also the official build tool of the Apache software foundation for the Java project.

Gradle is even newer, and it has started to catch up to Maven these days.

--

--