Staff Engineer @ GrubHub
CKUG Co-Organizer
@wakingrufus
@wakingrufus@mastodon.technology
wakingrufus
plugins {
id "java"
id "jacoco"
}
repositories {
mavenCentral()
}
dependencies {
implementations("commons-io:commons-io:2.7")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.+")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.+")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.4.+")
testImplementation("org.assertj:assertj-core:3.23.1")
}
test {
useJUnitPlatform()
}
def isNonStable = { String version ->
def stableKeyword = ['RELEASE', 'FINAL', 'GA']
.any { it -> version.toUpperCase().contains(it) }
def regex = /^[0-9,.v-]+(-r)?$/
return !stableKeyword && !(version ==~ regex)
}
def excludeList = ["guice", "guava"]
tasks.named("dependencyUpdates").configure {
resolutionStrategy {
componentSelection {
all {
if (isNonStable(it.candidate.version)) {
reject('Release candidate')
} else if (excludeList.contains(it.candidate.module)){
reject('dependency excluded from upgrades')
}
}
}
}
}
plugins {
id "java"
id "jacoco"
id "com.myorg.gradle.depupdates"
}
Basic scripts: doesn't matter
Complex scripts: Groovy
Plugins: Kotlin
plugins {
kotlin("jvm")
`kotlin-dsl`
`java-gradle-plugin`
}
plugins {
kotlin("jvm")
`kotlin-dsl`
`java-gradle-plugin`
}
plugins {
kotlin("jvm")
`kotlin-dsl`
`java-gradle-plugin`
}
plugins {
kotlin("jvm")
`kotlin-dsl`
`java-gradle-plugin`
}
gradlePlugin {
plugins {
create("myPlugin") {
id = "com.myorg.myplugin"
implementationClass = "com.myorg.myplugin.MyPlugin"
}
}
}
plugins {
kotlin("jvm")
`kotlin-dsl`
`java-gradle-plugin`
}
gradlePlugin {
plugins {
create("myPlugin") {
id = "com.myorg.myplugin"
implementationClass = "com.myorg.myplugin.MyPlugin"
}
}
}
abstract class MyTask : DefaultTask() {
@get:OutputFile
abstract val outputFile: Property<File>
@get:InputFile
abstract val inputFile: Property<File>
@TaskAction
fun doWork(){
outputFile.get().writeText(doThing(inputFile.get()))
}
}
abstract class MyTask : DefaultTask() {
companion object {
@JvmStatic
fun create(project: Project,
taskName: String = "myTask"): MyTask {
return project.tasks.create<MyTask>(taskName).apply {
// TODO
}
}
}
}
abstract class MyTask : DefaultTask() {
companion object {
@JvmStatic
fun create(project: Project,
taskName: String = "myTask"): MyTask {
return project.tasks.create<MyTask>(taskName).apply {
outputFile.set(input.map {
project.buildDir.resolve("$it.txt")
})
input.convention("name")
}
}
}
}
abstract class MyTask : DefaultTask() {
companion object {
@JvmStatic
fun create(project: Project,
taskName: String = "myTask"): MyTask {
return project.tasks.create<MyTask>(taskName).apply {
outputs.upToDateWhen { false }
}
}
}
}
abstract class MyTask : DefaultTask() {
companion object {
@JvmStatic
fun create(project: Project,
taskName: String = "myTask"): MyTask {
val ext = project.extensions.findByType<MyExtension>()
return project.tasks.create<MyTask>(taskName).apply {
input.set(ext.setting)
}
}
}
}
open class MyExtension(objects: ObjectFactory) {
companion object {
@JvmStatic
fun create(project: Project): MyExtension {
return project.extensions.create<MyExtension>("my")
.apply { name.convention(project.rootProject.name) }
}
}
val name: Property<String> = objects.property(String::class.java)
fun name(newName: String) {
name.set(newName)
}
}
my {
name("customName")
}
extensions.findByType<MyExtension>() {
name("customName")
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
}
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
val ext = MyExtension.create(project)
val task = MyTask.create(project)
}
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.apply(JavaPlugin::class.java)
}
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.withType(JavaPlugin::class) {
project.repositories {
// configure custom repo
}
project.withConvention(JavaPluginConvention::class) {
sourceSets.create("customSourceSet"){
// configure custom source set
}
}
}
}
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
val ext = MyExtension.create(project)
val task = MyTask.create(project)
project.afterEvaluate {
task.inputFile = ext.inputFile
}
}
}
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
val ext = MyExtension.create(project)
val task = MyTask.create(project)
task.inputFile.set(ext.inputFile) // bind properties
}
}
my.enabled=false
./gradlew -Pmy.enabled=false
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
if(!project.hasProperty("my.enabled")
|| project.property("my.enabled")){
// plugin code
}
}
}
val rootProject = ProjectBuilder.builder().build() as ProjectInternal
val subProject = ProjectBuilder.builder()
.withParent(rootProject)
.build() as ProjectInternal
subProject.plugins.apply(JavaPlugin::class.java)
subProject.plugins.apply(MyPlugin::class.java)
subProject.evaluate()
rootProject.evaluate()
assertThat(subProject.tasks.findByName("myTask")).isNotNull()
subProject.tasks.findByName("myTask").doWork()
rootProjectDir.resolve("build.gradle").apply {
createNewFile()
writeText("""plugins {
id("java")
id("com.myorg.myplugin")
}""".trimMargin()
)
}
val buildResult = GradleRunner.create()
.withProjectDir(rootProjectDir)
.withPluginClasspath()
.withArguments("build", "--stacktrace")
.withGradleVersion("7.5")
.forwardOutput().build()
assertThat(buildResult.task(":myTask")?.outcome).isEqualTo(SUCCESS)
wrapper {
gradleVersion = "7.5.1"
}
gradle wrapper
wrapper {
gradleVersion = "7.5.1"
}
./gradlew build
wrapper {
distributionUrl(
"https://myorg.com/wrapper/gradle-7.5.1-mywrapper-2.1.0.zip"
)
}
@wakingrufus
@wakingrufus@mastodon.technology