Check Gradle properties in console

You can check the properties set for gradle using below command

gradle properties

If you want to know the gradle properties in details, run the below task in gradle

task  demo1{
 println "System Properties:" 
        System.properties.each { 
                println "   $it" 
        } 
 
        println "Project Properties:" 
        project.properties.each { 
                println "   $it" 
        } 
         
        println "Plugin Properties:" 
        project.plugins.each { 
                println "    plugin: $it" 
                it.properties.each { 
                        println "       $it" 
                } 
        }  
}

You can also find for a property using below command

task printProps {
    doLast {
        println "org.gradle.priority -> ${project.findProperty('org.gradle.priority') ?: 'Value is not set'}"
    }
}

Gradle properties (gradle.propeties)

The gradle properties file should be saved with the name gradle.properties
Below are some of the properties of gradle

org.gradle.caching=false
org.gradle.caching.debug=false
org.gradle.configureondemand=false
org.gradle.console=auto
org.gradle.daemon=true
org.gradle.daemon.idletimeout=10800000
org.gradle.debug=false
org.gradle.java.home=C:/Program Files/Java/jdk1.8.0_191
org.gradle.jvmargs=
org.gradle.logging.level=lifecycle
org.gradle.parallel=
org.gradle.warning.mode=all
org.gradle.priority=normal
 
#proxy settings
#systemProp.http.proxyHost=www.somehost.org
#systemProp.http.proxyPort=8080
#systemProp.http.proxyUser=userid
#systemProp.http.proxyPassword=password
#systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
 
#systemProp.https.proxyHost=www.somehost.org
#systemProp.https.proxyPort=8080
#systemProp.https.proxyUser=userid
#systemProp.https.proxyPassword=password
#systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost

The gradle properties file can be placed at 2 different location i.e GRADLE_USER_HOME directory and APPLICATION ROOT directory, but if the file is placed on both the location then GRADLE_USER_HOME directory takes precedence.

The same above mentioned properties can also be mentioned via command line( i.e system properties, e.g. when -Dgradle.user.home is set on the command line. )

Automatically generating java project structure in Gradle

Gradle generated java project structure using Gradle Build init plugin

To find the list of gradle supported project structure you can fire the below command in command prompt

gradle init

The “java-application” build init type is not inferable. It must be explicitly specified.

It has the following features:

  • Uses the “application” plugin to produce a command-line application implemented using Java
  • Uses the “jcenter” dependency repository
  • Uses JUnit for testing
  • Has directories in the conventional locations for source code
  • Contains a sample class and unit test, if there are no existing source or test files

Alternative test framework can be specified by supplying a --test-framework argument value. To use a different test framework, execute one of the following commands:

  • gradle init --type java-application --test-framework spock: Uses Spock for testing instead of JUnit
  • gradle init --type java-application --test-framework testng: Uses TestNG for testing instead of JUnit

You can specify the init type directly by using the below command

gradle init --type java-application

Writing sample build script(Hello World)

Gradle is based on the concept of task and you write these task in a file called build.gradle.

Below is a sample build.gradle file which has a task called helloWorld which prints the text ‘Hello world.’ on console.

Now to execute this build script we need to go to the directory where the build.gradle file is store and execute the below command command

gradle <task_name>

Now since our task name is helloWorldin the build.gradle file we will execute it with the command gradle helloWorld (-q for quitely) and below is the output of the command

Looping

task count {
    doLast {
        4.times { print "$it " }
    }
}

Creating an Adhoc task(Avoidance API)

tasks.register("helloNewTask") {
    group = 'Welcome'
    description = 'Produces a greeting'

    doLast {
        println 'Hello Avoidance API'
    }
}

Costume task (Avoidance API)

class Greeting extends DefaultTask {  
    String message 
    String recipient

    @TaskAction 
    void sayGreeting() {
        println "${message}, ${recipient}!" 
    }
}

tasks.register("hello", Greeting) { 
    group = 'Welcome'
    description = 'Produces a world greeting'
    message = 'Hello' 
    recipient = 'World'
}

Setup Gradle

  1. Download the gradle and copy paste it into your program files after extracting the zip file Example : “C:\Program Files\gradle-5.1.1″.
  2. Check if java is installed on the system by using command java -version on your command prompt if not then install it.
  3. Set new System Variable GRADLE_HOME as gradle installation directory example “C:\Program Files\gradle-5.1.1
  4. Append the PATH variable with “C:\Program Files\gradle-5.1.1\bin
  5. Now open up and command prompt and write gradle -v to check if the Gradle is properly installed on your system.

Install Gradle in Ubuntu :

Install JDK

sudo apt update
sudo apt install openjdk-8-jdk

Install Gradle

$ wget https://services.gradle.org/distributions/gradle-6.4.1-bin.zip -P /tmp
$ sudo unzip -d /opt/gradle /tmp/gradle-*.zip

$ sudo vi /etc/profile.d/gradle.sh
export GRADLE_HOME=/opt/gradle/gradle-6.4.1
export PATH=${GRADLE_HOME}/bin:${PATH}

$ source /etc/profile.d/gradle.sh

$ gradle -v

Pushing files to an online repository

  1. You need a Github account.
  2. You need to create a repository and get the URL of the repository. There are two types of URL https and ssh. For this demo we will use https.
    Example : https://github.com/tyson332/DemoRepository.git
  3. Now we need to add the above remote URL into git and give it a name, command to do that is
git remote add <short_name> <repository_url>

You can remove the remote using the keyword remove

git remote remove <short_name>

You can change the url of the existing remote repository using set-url command

git remote set-url <short_name> <repository_url>

You can also rename the short name using the command rename

git remote rename <short_name_old> <short_name_new>

You can check the list of remotes that you have already added using the command git remote -v

git remote add demo https://github.com/tyson332/DemoRepository.git
git remote remove demo
git remote -v

Note : When you clone a repository using an URL it automatically adds the remote for that url under the name of origin which is the default name provided by git.

Now lets push the files to the remote repository using the below command

git push -u <short_name> <branch_to_be_committed>

The -u attribute in the above command signifies that you need to save the configuration of committing files to repository of that branch always, so next time you need to push you can do just a git push and all the combination of repository url and branch is already maintained in the GIT configuration it automatically pushes it for you. You can also check this out in the local git configuration file (.git/config)

You can see these files manually on Github

If you want more details about a remote you can run the command

git remote show <remote> <short_name>