Skip to content

Instantly share code, notes, and snippets.

@ahndmal
Last active March 17, 2024 16:41
Show Gist options
  • Save ahndmal/16e838615db42edb1e616979c836ed3f to your computer and use it in GitHub Desktop.
Save ahndmal/16e838615db42edb1e616979c836ed3f to your computer and use it in GitHub Desktop.
import java.time.Duration
plugins {
id 'java'
}
group 'com.anma.gr'
version '1.0.0'
gradle.beforeProject {
println(">> Before project")
}
repositories {
mavenCentral()
}
logger.quiet(' ========= Starting the build ==========')
//compileJava {
// options.release = 17
//}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
jar {
manifest {
attributes("Implementation-Title": "Gradle",
"Implementation-Version": archiveVersion
"Main-Class": "com.example.Main")
}
}
sourceSets {
main {
java.srcDir("src/main/java")
}
}
dependencies {
implementation gradleApi()
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
tasks.named("test", Test) {
useJUnitPlatform()
maxHeapSize = '1G'
ignoreFailures = true
}
// Tasks
// DefaultTask
class GreetingTask extends DefaultTask {
@TaskAction
static def greet() {
println('>>> Greeting task successful!')
}
}
// register
def helloTask = tasks.register('hello') {
doLast {
if (false) {
throw new StopExecutionException()
}
println('Hello world!')
}
description = "This is hello task that prints hello message..."
onlyIf {
targetCompatibility.java11
}
timeout = Duration.ofSeconds(10)
}
logger.quiet("Task name is $helloTask.name")
// container
def container = getTasks()
def task3 = container.create("task3")
task3.doFirst {
println(" >>> task3")
}
tasks.named('hello').get().enabled(true)
tasks.register('loadfile') {
def resourceDirectory = file('./antLoadfileResources')
doLast {
def files = resourceDirectory.listFiles().sort()
files.each { File file ->
if (file.isFile()) {
ant.loadfile(srcFile: file, property: file.name)
println " *** $file.name ***"
println "${ant.properties[file.name]}"
}
}
}
}
tasks.register('copyReport', Copy) {
from layout.buildDirectory.file("reports/my-report.pdf")
into layout.buildDirectory.dir("toArchive")
}
tasks.register('copyAllPdfReportsForArchiving', Copy) {
from layout.buildDirectory.dir("reports")
include "**/*.pdf"
into layout.buildDirectory.dir("toArchive")
}
// =========== Plugins
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
def extension = project.extensions.create('greeting', GreetingPluginExtension)
project.task('helloGreet') {
doLast {
println "${extension.message.get()} from ${extension.greeter.get()}"
}
}
}
}
interface GreetingPluginExtension {
Property<String> getMessage()
Property<String> getGreeter()
}
apply plugin: GreetingPlugin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment