This is a very simple tutorial for building a simple Java app with Gradle. The purpose is to get a feel for Gradle. It also explains how to install and use Gradle Wrapper.
I won't explain the build script in Groovy, this grammar in this sense, or anything like that [^ 1]. But I think you can get a feel for it. [^ 1]: Not detailed enough to explain in the first place
It is a condition that JDK6 or higher is downloaded. You don't need an IDE, just a text editor.
First, create a Java project to build with Gradle. Keep the project as simple as possible to focus on Gradle's description.
Create the following directory structure under your favorite directory.
└── src
└── main
└── java
└── hello
Create two classes,'HelloWorld.java' and'Greeter.java', under the src / main / java / hello
directory.
src/main/java/hello/HelloWorld.java
package hello;
public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}
src/main/java/hello/Greeter.java
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}
Let's install Gradle.
You can download the binaries from http://www.gradle.org/downloads. DL-extract gradle-version-bin.zip
and put the path to the bin
folder.
You can also use the following installer.
Run the following command to see if it was installed
gradle
If the installation is successful, you will see a welcome message.
>gradle
Starting a Gradle Daemon (subsequent builds will be faster)
:help
Welcome to Gradle 3.2.1.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
Total time: 8.667 secs
This completes the Gradle installation.
gradle tasks Execute the following command under the project directory.
gradle tasks
The gradle tasks
command displays the tasks you can perform with gradle. At this point you should see the following basic tasks:
:tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'hello'.
components - Displays the components produced by root project 'hello'. [incubating]
dependencies - Displays all dependencies declared in root project 'hello'.
dependencyInsight - Displays the insight into a specific dependency in root project 'hello'.
dependentComponents - Displays the dependent components of components in root project 'hello'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'hello'. [incubating]
projects - Displays the sub-projects of root project 'hello'.
properties - Displays the properties of root project 'hello'.
tasks - Displays the tasks runnable from root project 'hello'.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL
Total time: 1.244 secs
Well, it doesn't make much sense in the initial state, but if you want to know what kind of task can be executed, it is a good idea to keep in mind that this command is used.
Create a build.gradle
file directly under the project folder.
build.gradle
apply plugin: 'java'
This line indicates that you are building this project using the Java plugin.
Run gradle tasks
again. You'll see new tasks added, such as building the project, creating the JavaDoc, and running the tests.
Let's build it right away.
gradle build
gradle build
will be used frequently from now on. As the name implies, it is a command to execute a build. After a few seconds, you will see "BUILD SUCCESS FUL" indicating that the build is complete.
Take a look at the build
folder to see the build results. There are several folders.
classes Contains the .class file when the Java code was compiled. This time HelloWorld.class'and'Greeter.class' should have been created
dependency-cache Contains the .jar of the dependent module. It is empty at this time.
libs The created library (usually a jar or war) is stored.
Now that the build is complete, let's run it. It is possible to execute it using the java
command, but since it is a big deal, let's execute it from gradle. Change build.gradle
as follows.
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
The application plugin allows you to start an application with the gradle run
command.
>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Hello world!
BUILD SUCCESSFUL
Total time: 1.786 secs
The samples so far do not depend on any external library, so there is not much taste in using gradle.
So, let's change it so that not only "Hello World!" But also the current date and time are displayed. You can use the Java date-time API of the Java standard library, but I'll use Joda Time libraries for illustration.
First, modify HelloWorld.java as follows:
HelloWorld.java
package hello;
import org.joda.time.LocalTime;
public class HelloWorld {
public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
System.out.println("The current local time is: " + currentTime);
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}
Hello World uses Joda Time's Local Time to get and display the current time.
Now run gradle build
. It fails because Joda Time cannot be resolved.
Now let's have gradle resolve the dependency. Please change build.gradle
.
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'hello.HelloWorld'
repositories {
mavenCentral()
}
jar {
baseName = 'gs-gradle'
version = '0.1.0'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile "joda-time:joda-time:2.2"
testCompile "junit:junit:4.12"
}
The repositories
block specifies that the Maven central repository will be used for the repository.
The jar block specifies the name of the jar. In this case it would be gs-gradle-0.1.0.jar
.
The dependencies
block declares dependencies on Joda Time. Specifically, the joda-time library in the joda-time group, version 2.2.
compile
indicates that the dependency is valid only at compile time. It's the compile
scope in Maven. Other dependency types
providedCompile
Required at compile time, but at runtime like the dependency Servlet API provided by the container. Provided in Maven.testCompile
Dependencies required when compiling, time and testing. Not included in build artifacts. Like jUnit. Test in Maven.Now, run gradle build
again. gradle resolves the Joda Time dependency from the Mave central repository and the build succeeds.
The project should run fine.
>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
The current local time is: 01:56:47.665
Hello world!
BUILD SUCCESSFUL
Total time: 1.308 secs
Gradle Wapper When it comes to using this kind of tool in a project consisting of multiple people, it is troublesome to prepare an installation procedure manual for other members and to use different versions.
That's where Gradle Wapper comes in. By installing Gradle Wapper in your project, anyone can build a project with the same version of gradle regardless of the environment such as Windows, Mac, Linux.
Installing Gradle Wapper is easy, just execute the following command directly under the project.
$ gradle wrapper --gradle-version 2.13
When the task is completed, several folders and directories will be created.
└── <project folder>
└── gradlew
└── gradlew.bat
└── gradle
└── wrapper
└── gradle-wrapper.jar
└── gradle-wrapper.properties
This completes the installation of Gradle Wrapper. It's easy to use, just execute "gradlew (.bat)" directly under the project instead of the gradle command.
./gradlew build
Of course, you can run other commands as well.
./gradlew run
Just commit the file created with gradle init
to git or svn (and just hit this command and write a line in the readme) and no one will have to do it via gradlew You can use gradle with. It's well thought out.
With this, I think you can get a feel for Gradle. It is not possible to build a full-scale project with the contents of this article alone. However, the hurdle to read other Gradle-related articles should have been lowered. Get the information you need and get the most out of Gradle.
I hope this article has given you a chance to start using gradle.
Recommended Posts