- Gradle - Discussion
- Gradle - Useful Resources
- Gradle - Quick Guide
- Gradle – Eclipse Integration
- Gradle – Deployment
- Gradle – Multi-Project Build
- Gradle – Testing
- Gradle – Build a Groovy Project
- Gradle – Build a JAVA Project
- Gradle – Running a Build
- Gradle – Plugins
- Gradle – Dependency Management
- Gradle – Tasks
- Gradle – Build Script
- Gradle – Installation
- Gradle – Overview
- Gradle – Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Gradle - Build a JAVA Project
This chapter explains how to build a java project using Gradle build file.
First of all, we have to add java plugin to the build script, because, it provides the tasks to compile Java source code, to run the unit tests, to create a Javadoc and to create a JAR file.
Use the following pne in build.gradle file.
apply plugin: java
Java Default Project Layout
Whenever you add a plugin to your build, it assumes a certain setup of your Java project (similar to Maven). Take a look at the following directory structure.
src/main/java contains the Java source code.
src/test/java contains the Java tests.
If you follow this setup, the following build file is sufficient to compile, test, and bundle a Java project.
To start the build, type the following command on the command pne.
C:> gradle build
SourceSets can be used to specify a different project structure. For example, the sources are stored in a src folder rather than in src/main/java. Take a look at the following directory structure.
apply plugin: java sourceSets { main { java { srcDir src } } test { java { srcDir test } } }
init Task Execution
Gradle does not yet support multiple project templates. But it offers an init task to create the structure of a new Gradle project. Without additional parameters, this task creates a Gradle project, which contains the gradle wrapper files, a build.gradle and settings.gradle file.
When adding the --type parameter with java-pbrary as value, a java project structure is created and the build.gradle file contains a certain Java template with Junit. Take a look at the following code for build.gradle file.
apply plugin: java repositories { jcenter() } dependencies { compile org.slf4j:slf4j-api:1.7.12 testCompile junit:junit:4.12 }
In the repositories section, it defines where to find the dependencies. Jcenter is for resolving your dependencies. Dependencies section is for providing information about external dependencies.
Specifying Java Version
Usually, a Java project has a version and a target JRE on which it is compiled. The version and sourceCompatibipty property can be set in the build.gradle file.
version = 0.1.0 sourceCompatibipty = 1.8
If the artifact is an executable Java apppcation, the MANIFEST.MF file must be aware of the class with the main method.
apply plugin: java jar { manifest { attributes Main-Class : com.example.main.Apppcation } }
Example
Create a directory structure as shown in the below screenshot.
Copy the below given java code into App.java file and store into consumerbankingsrcmainjavacomank directory.
package com.bank; /** * Hello world! * */ pubpc class App { pubpc static void main( String[] args ){ System.out.println( "Hello World!" ); } }
Copy the below given java code into AppTset.java file and store into consumerbankingsrc estjavacomank directory.
package com.bank; /** * Hello world! * */ pubpc class App{ pubpc static void main( String[] args ){ System.out.println( "Hello World!" ); } }
Copy the below given code into build.gradle file and placed into consumerbanking directory.
apply plugin: java repositories { jcenter() } dependencies { compile org.slf4j:slf4j-api:1.7.12 testCompile junit:junit:4.12 } jar { manifest { attributes Main-Class : com.example.main.Apppcation } }
To compile and execute the above script use the below given commands.
consumerbanking> gradle tasks consumerbanking> gradle assemble consumerbanking> gradle build
Check all the class files in the respective directories and check consumerbankinguildpb folder for consumerbanking.jar file.
Advertisements