Build.gradle: Difference between revisions

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


The name of the build file can be changed using [[Gradle_Concepts_ToDeplete#Project_Descriptor|project descriptors]]:
=Internal=
* [[Gradle_Concepts#Configuration_Scripts_and_Core_Types|Gradle Concepts]]
* [[Gradle Project#Overview|Project]]


<syntaxhighlight lang='groovy'>
=Overview=
project(':projectA').buildFileName = 'projectA.gradle'
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]].
</syntaxhighlight>
 
=Comments=


=Example=
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
// This is a comment
</syntaxhighlight>


=DSL=
apply plugin: 'java'


The following elements are part of [https://docs.gradle.org/current/dsl/org.gradle.api.Project.html Project] API.
// 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"


==configurations.all==
repositories {
 
    mavenLocal()
Applies enclosed to all [[Gradle_Concepts_ToDeplete#Dependency_Configuration|configurations]].
     mavenCentral()
 
<syntaxhighlight lang='groovy'>
configurations.all {
     resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
}
}
</syntaxhighlight>


Alternative:
dependencies {
 
    implementation 'org.slf4j:slf4j-api:1.7.12'
<syntaxhighlight lang='groovy'>
     testImplementation 'junit:junit:4.+'
configurations.all {
     resolutionStrategy {
        cacheDynamicVersionsFor 10, 'minutes'
    }
}
}
</syntaxhighlight>
===Configuration===
====resolutionStrategy====
{{External|https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html}}
=====cacheDynamicVersionsFor=====
Configures [[Gradle_Concepts_ToDeplete#Dynamic_Version|dynamic version]] caching threshold.
<syntaxhighlight lang='groovy'>
resolutionStrategy.cacheDynamicVersionsFor 10, 'minutes'
</syntaxhighlight>
=====cacheChangingModulesFor=====
==configurations==


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


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


Used when [[Gradle Gradle_Concepts_ToDeplete#External_Plugins|external plugins]] need to be downloaded.
compileJava {
 
     options.compilerArgs += ['-Xlint']
<syntaxhighlight lang='groovy'>
buildscript {
     repositories {
        maven { url 'https://plugins.gradle.org/m2/' }
    }
  dependencies {
    classpath 'com.netflix.nebula:gradle-ospackage-plugin:4.3.0'
  }
}
}
</syntaxhighlight>


==<span id='Repositories'></span>repositories==
javadoc {
 
    options.charSet = 'UTF-8'
{{External|https://docs.gradle.org/current/userguide/declaring_repositories.html}}
 
[[Gradle_Concepts_ToDeplete#Repository|Repositories]] are declared in a 'repositories' block:
 
<syntaxhighlight lang='groovy'>
repositories {
    ...
}
}
</syntaxhighlight>
Multiple repositories can be declared in the same block. Syntax examples are available for [[Gradle_Concepts_ToDeplete#Custom_Maven_Repository|custom Maven repositories]], [[Gradle_Concepts_ToDeplete#flatDir|directory-based repositories]], [[Gradle_Concepts_ToDeplete#Maven_Central|Maven Central]], etc. The most common setting is declaring that our dependencies are available in Maven Central:


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


=Multi-Project Configuration=
=Comments=
 
==allprojects==
 
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.


<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
allprojects {
// This is a comment
  repositories {
    ...
  }
}
</syntaxhighlight>
</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.
=Code=
 
==subprojects==
 
The <tt>subprojects</tt> block is used to add configuration times for sub-projects only.


The build script may include general-purpose Java and Groovy code:
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
subprojects {
    ...
}
</syntaxhighlight>


=Custom Tasks=
class Experimental {


{{Internal|Gradle Custom Task#Overview|Custom Tasks}}
    private String name;


=Respond to Build Lifecycle Events=
    Experimental(String name) {


Example of how to respond to [[Gradle_Concepts_ToDeplete#Build_Phases|project build lifecycle events]].
        this.name = name;
    }


<syntaxhighlight lang='groovy'>
    @Override
allprojects {
    public String toString() {
      afterEvaluate { project ->
        return name;
          if (project.hasTests) {
    }
              println "Adding test task to $project"
              project.task('test') {
                  doLast {
                        println "running tests for $project"
                  }
              }
          }
    }
}
}
</syntaxhighlight>


<syntaxhighlight lang='groovy'>
def experimental = new Experimental("blue");
gradle.afterProject {project, projectState ->
      if (projectState.failure) {
          println "Evaluation of $project FAILED"
      } else {
          println "Evaluation of $project succeeded"
      }
}
</syntaxhighlight>


=Respond to Task Lifecycle Events=
task test {
 
    doLast {
Examples of how to respond to [[Gradle_Concepts_ToDeplete#Task|task]] lifecycle events.
        println experimental
 
    }
==Task Creation==
 
<syntaxhighlight lang='groovy'>
tasks.whenTaskAdded { task ->
      task.ext.srcDir = 'src/main/java'
}
}
</syntaxhighlight>
</syntaxhighlight>
==Task Execution==
<syntaxhighlight lang='groovy'>
gradle.taskGraph.beforeTask { Task task ->
      println "executing $task ..."
}
gradle.taskGraph.afterTask { Task task, TaskState state ->
      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
    }
}