Build.gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://docs.gradle.org/current/userguide/writing_build_scripts.html


=Internal=
* [[Gradle_Concepts#Configuration_Scripts_and_Core_Types|Gradle Concepts]]
* [[Gradle Project#Overview|Project]]


=Overview=
build.gradle is referred to as an Gradle build script. It is the script interface to a [[Gradle_Project#Project_API_and_build.gradle_DSL|Project instance]].


=Multi-Project Configuration=
=Example=
<syntaxhighlight lang='groovy'>
 
apply plugin: 'java'
 
// name comes from the directory name or it can be changed in settings.gradle
group = "playground.example"
version = "0.1.0"
description = "A Gradle example project"


==allprojects==
repositories {
    mavenLocal()
    mavenCentral()
}


The <tt>allprojects</tt> block is used to add configuration items that will apply to all [[Gradle_Concepts_ToDeplete#Multi-Project_Build|sub-projects]], as well as the root project.
dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.12'
    testImplementation 'junit:junit:4.+'
}


<syntaxhighlight lang='groovy'>
configurations {
allprojects {
     testImplementation.exclude group: 'com.example'
  repositories {
     ...
  }
}
}
</syntaxhighlight>


"allprojects" is a property that contains a list with the current project and all its subprojects underneath it. It can be called with a closure, and the statements of the closure are delegated to the projects.
configurations.all {
    resolutionStrategy.cacheChangingModulesFor 3600, 'seconds'
}


==subprojects==
compileJava {
    options.compilerArgs += ['-Xlint']
}


The <tt>subprojects</tt> block is used to add configuration times for sub-projects only.
javadoc {
    options.charSet = 'UTF-8'
}


<syntaxhighlight lang='groovy'>
task runMain(type: JavaExec) {
subprojects {
    classpath = sourceSets.main.runtimeClasspath
     ...
     main = "io.novaordis.playground.gradle.java.Main"
}
}
</syntaxhighlight>
</syntaxhighlight>


=Respond to Build Lifecycle Events=
=Comments=
 
Example of how to respond to [[Gradle_Concepts_ToDeplete#Build_Phases|project build lifecycle events]].


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
allprojects {
// This is a comment
      afterEvaluate { project ->
          if (project.hasTests) {
              println "Adding test task to $project"
              project.task('test') {
                  doLast {
                        println "running tests for $project"
                  }
              }
          }
    }
}
</syntaxhighlight>
</syntaxhighlight>


=Code=
The build script may include general-purpose Java and Groovy code:
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
gradle.afterProject {project, projectState ->
      if (projectState.failure) {
          println "Evaluation of $project FAILED"
      } else {
          println "Evaluation of $project succeeded"
      }
}
</syntaxhighlight>


=Respond to Task Lifecycle Events=
class Experimental {
 
    private String name;


Examples of how to respond to [[Gradle_Concepts_ToDeplete#Task|task]] lifecycle events.
    Experimental(String name) {


==Task Creation==
        this.name = name;
    }


<syntaxhighlight lang='groovy'>
    @Override
tasks.whenTaskAdded { task ->
    public String toString() {
      task.ext.srcDir = 'src/main/java'
        return name;
    }
}
}
</syntaxhighlight>


==Task Execution==
def experimental = new Experimental("blue");


<syntaxhighlight lang='groovy'>
task test {
gradle.taskGraph.beforeTask { Task task ->
    doLast {
      println "executing $task ..."
        println experimental
    }
}
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
</syntaxhighlight>
      if (state.failure) {
          println "FAILED"
      }
      else {
          println "done"
      }
}</syntaxhighlight>

Latest revision as of 22:10, 11 October 2020

External

Internal

Overview

build.gradle is referred to as an Gradle build script. It is the script interface to a Project instance.

Example

apply plugin: 'java'

// name comes from the directory name or it can be changed in settings.gradle
group = "playground.example"
version = "0.1.0"
description = "A Gradle example project"

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.12'
    testImplementation 'junit:junit:4.+'
}

configurations {
    testImplementation.exclude group: 'com.example'
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 3600, 'seconds'
}

compileJava {
    options.compilerArgs += ['-Xlint']
}

javadoc {
    options.charSet = 'UTF-8'
}

task runMain(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = "io.novaordis.playground.gradle.java.Main"
}

Comments

// This is a comment

Code

The build script may include general-purpose Java and Groovy code:

class Experimental {

    private String name;

    Experimental(String name) {

        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

def experimental = new Experimental("blue");

task test {
    doLast {
        println experimental
    }
}