Skip to content

Commit

Permalink
Configure a comunity plugin by Extension
Browse files Browse the repository at this point in the history
Similar to core plugins, also community plugins (or your own plugins)
may provide extensions to configure them.
In the Kotlin DSL and the Groovy DSL, an extension is accessed directly
by its name. In class-baed approaches, we need to retrieve it via
'extensions.getByType()' and provide the implementation type of the
extension there.
  • Loading branch information
jjohannes committed Jan 6, 2022
1 parent a4d53b5 commit 39baa4d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@
(.showStackTraces (.getTestLogging test) true)
))))
(.add (.getDependencies project) "implementation" "org.junit.jupiter:junit-jupiter:5.7.2")

; Configure a community plugin - example Spotless
(.format (.getByType (.getExtensions project) SpotlessExtension) "buildFiles" (reify Action (execute [this format] (do
(.target format (into-array Object ["build.gradle.kts"]))
(.trimTrailingWhitespace format)
(.endWithNewline format)
))))
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ tasks.named("test") {
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
}

// Configure a community plugin - example Spotless
spotless {
format("buildFiles") {
target("build.gradle.kts")
trimTrailingWhitespace()
endWithNewline()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ abstract class MyJavaBasePlugin implements Plugin<Project> {
it.testLogging.showStackTraces = true
}
project.dependencies.add(TEST_IMPLEMENTATION_CONFIGURATION_NAME, "org.junit.jupiter:junit-jupiter:5.7.2")

// Configure a community plugin - example Spotless
def spotless = project.extensions.getByType(SpotlessExtension)
spotless.format("buildFiles") {
it.target("build.gradle.kts")
it.trimTrailingWhitespace()
it.endWithNewline()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ public void apply(Project project) {
test.getTestLogging().setShowStackTraces(true);
});
project.getDependencies().add(TEST_IMPLEMENTATION_CONFIGURATION_NAME, "org.junit.jupiter:junit-jupiter:5.7.2");

// Configure a community plugin - example Spotless
SpotlessExtension spotless = project.getExtensions().getByType(SpotlessExtension.class);
spotless.format("buildFiles", format -> {
format.target("build.gradle.kts");
format.trimTrailingWhitespace();
format.endWithNewline();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.7.2")
}

// Configure a community plugin - example Spotless
spotless {
format("buildFiles") {
target("build.gradle.kts")
trimTrailingWhitespace()
endWithNewline()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ abstract class MyJavaBasePlugin : Plugin<Project> {
it.testLogging.showStackTraces = true
}
dependencies.add(TEST_IMPLEMENTATION_CONFIGURATION_NAME, "org.junit.jupiter:junit-jupiter:5.7.2")

// Configure a community plugin - example Spotless
val spotless = extensions.getByType(SpotlessExtension::class.java)
spotless.format("buildFiles") {
it.target("build.gradle.kts")
it.trimTrailingWhitespace()
it.endWithNewline()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ abstract class MyJavaBasePlugin extends Plugin[Project] {
test.getTestLogging.setShowStandardStreams(true)
} : Unit)
project.getDependencies.add(TEST_IMPLEMENTATION_CONFIGURATION_NAME, "org.junit.jupiter:junit-jupiter:5.7.2")

// Configure a community plugin - example Spotless
def spotless = project.getExtensions.getByType(classOf[SpotlessExtension])
spotless.format("buildFiles", (format: FormatExtension) => {
format.target("build.gradle.kts")
format.trimTrailingWhitespace()
format.endWithNewline()
})
}
}

0 comments on commit 39baa4d

Please sign in to comment.