0% found this document useful (0 votes)
98 views6 pages

Add A Gradle Dependency and Its Related Repository Url. Code

This document provides instructions for various Gradle tasks and configurations, including: adding dependencies; making a JAR executable; differentiating dependency scopes; creating a custom plugin task; using alternate source sets; overriding the Gradle wrapper version; and profiling a Gradle project.

Uploaded by

Divya Chhabra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views6 pages

Add A Gradle Dependency and Its Related Repository Url. Code

This document provides instructions for various Gradle tasks and configurations, including: adding dependencies; making a JAR executable; differentiating dependency scopes; creating a custom plugin task; using alternate source sets; overriding the Gradle wrapper version; and profiling a Gradle project.

Uploaded by

Divya Chhabra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Add a gradle dependency and its related repository url.

Code:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
sourceSets{
main{
java.srcDirs=['src']
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Output:

2. Using java plugin, make changes in the manifest to make the jar executable. Using java
-jar JAR_NAME, the output should be printed as "Hello World"
Code:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven{
url "https://mvnrepository.com/artifact/com.google.code.gson/gson"
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}

PrintHello.java

public class PrintHello {

public static void main( String[] args ){


System.out.println( "Hello World!" );
}
}

Output:

3. Differentiate between the different dependency scopes: compile, runtime, testCompile,


testRuntime using different dependencies being defined in your build.gradle.
Compile:
We will make use of dependencies that are required to compile the production source of the
project at compile time.
Test Compile:
These dependencies are required to compile the test source of the project.It also contains the
compiled production classes and the compile time dependencies.
Runtime:
These dependencies are used at runtime by production classes. It also conatains the compile
time dependencies by default.
Test Runtime:
These dependencies are required to run the tests. It also contains runtime and test
compile dependencies.
4. Create a custom plugin which contains a custom task which prints the current date-
time. Using that plugin in your project, execute that task after the jar task executes.
Code:
showDate.groovy

build.gradle
Manifest.mf

Output:

5. Instead of using default source set, use src/main/javaCode1, src/main/javaCode2 to be


taken as code source. Make sure that the JAR created contains files from both the
directories and not from src/main/java.
1. Create 2 folders in the src/main named javaCode1 and javaCode2, each of these folders
have java classes with name Code1 and Code2.
2. In gradle.build

Output:
6. Override the Gradle Wrapper task to install a different version of gradle. Make sure
that the task written in Q4 also executes with it.

On the terminal:
Output:

In gradle.wrapper.properties file

7. Run the gradle profile command and attach the resulting files.
On the terminal:
Project Report on the browser:

You might also like