Building a Java Program with Gradle

Follow the steps below to create and run a Java application in Gradle. These instructions assume a computer running Microsoft Windows.

1. If you have not already done so, download Java and a Java Development Kit.

2. In any convenient location, create a new directory named “GradleTest”.

3. Download Gradle. As of this writing, the latest stable version is 2.13, and is available for download at the URL “http://gradle.org/post-download-gradle/” Navigate to that URL in a web browser, click the “Binary Only” link, and download the file to the newly created GradleTest directory.

4. In the GradleTest directory, extract the contents of the downloaded .zip file.

5. Within the extracted directory, locate the file “gradle.bat” and make a note of the directory that contains it. For this example, the file is located in the folder “gradle-2.13-bin\gradle-2.13\bin”.

6. Back in the GradleTest directory, create a new subdirectory named “MyProject”. In the MyProject directory, create a new subdirectory named “src”. In the src directory, create a new directory named “main”, and within “main” create a new directory named “java”.

7. In the newly created “java” directory, create a new text file named “GradleTest.java”, containing the following text:


public class GradleTest
{
	public static void main(String[] args)
	{
		System.out.println("Hello, Gradle!");
	}
}

8. Navigate back up to the GradleTest directory and create a new text file named “build.gradle”, containing the following text:

apply plugin: 'java'

9. Still in the GradleTest directory, create a new text file named “GradleTest-Build.bat”, containing the following text:

gradle build

10. Run the newly created GradleTest-Build.bat script file. A new directory named “build” will be created, containing all the files and subdirectories needed to build and execute the Java program.

11. Still in the GradleTest directory, create a new text file named “GradleTest-Run.bat”, containing the following text:

java -cp build/libs/GradleTest.jar GradleTest
pause

12. Run the script file GradleTest-Run.bat. The message “Hello, Gradle!” will be displayed.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s