Skip to content

Latest commit

 

History

History
113 lines (100 loc) · 3.33 KB

03-1-P-Gradle.adoc

File metadata and controls

113 lines (100 loc) · 3.33 KB

Gradle: A Build Framework

Example Gradle application

This section focuses on writing a Gradle (https://gradle.org/) build script that builds a single Gradle project referred to as computation. The project with the below gradle structure is available for you to download and import in your IDE.

  1. Go to this link and download the computation.zip. Once it is downloaded, extract the project from zip.

  2. Open your IDE and import the extracted project(from step 1) as Existing Gradle Project. Gradle Import

  3. After improting the project, check that your imorted project has the required structure as shown below:

    computation
    ├── build.gradle
    └── src
        ├── main
        │   └── java
        │       ├── application
        │       │   └── CompApp.java
        │       ├── computation
        │       │   └── Computation.java
        │       └── view
        │           └── ComputationPage.java
        └── test
            └── java
                └── computation
                    ├── AllTests.java
                    ├── ComputationTestAddSubstract.java
                    └── ComputationTestDivideMultiply.java
  4. Open the build.gradle file and check for the relevant parts as discussed in the steps (5 to 9) below.

  5. java and the application plugins are added in the build configuration script build.gradle.

    apply plugin: 'java'
    // This plugin has a predefined 'run' task that we can reuse to use Gradle to execute our application
    apply plugin: 'application'
  6. JUnit libraries are added in the dependencies section.

    repositories {
        mavenCentral()
    }
    dependencies {
        testImplementation "junit:junit:4.12"
    }
  7. A task compile(type: JavaCompile) has been added to specify all source files (both application and test) and set the build/bin as destination dir to put all compiled class files in.

    task compile(type: JavaCompile) {
      classpath = sourceSets.main.compileClasspath
      classpath += sourceSets.test.runtimeClasspath
      sourceSets.test.java.outputDir = file('build/bin')
      sourceSets.main.java.outputDir = file('build/bin')
    }
    Note
    One can specify source sets and their variables the following way:
    /*
     * specifying sourceSets is not necessary in this case, since
     * we are applying the default folder structure assumed by Gradle
     */
    sourceSets {
      main {
        java { srcDir 'src/main/java' }
      }
      test {
        java { srcDir 'src/test/java'}
      }
    }
  8. The main class has been specified as shown below. Now, you can proceed and run the application.

    mainClassName='application.CompApp'

    In the command line issue gradle run

  9. The jar Gradle task (defined by the java plugin) has been added to produce an executable jar file into distributable/.

    jar {
      destinationDir=file('distributable')
      manifest {
        // It is smart to reuse the name of the main class variable instead of hardcoding it
        attributes "Main-Class": "$mainClassName"
      }
    }
Note
The settings.gradle and its usage is to be shown later.