Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use *-conventions name pattern for precompiled convention plugins i…
…n samples
  • Loading branch information
rieske committed Aug 19, 2020
commit fa530a38b6592260a9db37983df8dd536351bf0a
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ We want to apply a set of code quality checking rules to all the submodules and

This is achieved by layering two separate plugins in project's `buildSrc` module:

* `com.example.java-convention` - configures conventions that are generic for any Java project in the organization.
* `com.example.library` - adds publishing configuration to publish to the organization's repository and checks for mandatory content in a README.
* `com.example.java-conventions` - configures conventions that are generic for any Java project in the organization.
* `com.example.library-conventions` - adds publishing configuration to publish to the organization's repository and checks for mandatory content in a README.

All plugins created in this sample contain functional tests that use link:{userManualPath}/test_kit.html[TestKit] to verify their behavior.

This sample does not have any project source code and only lays out a hypothetical project structure where two library modules depend on a shared internal module.

The internal module uses `com.example.java-convention` plugin:
The internal module uses `com.example.java-conventions` plugin:
====
include::sample[dir="groovy",files="internal-module/build.gradle[]"]
include::sample[dir="kotlin",files="internal-module/build.gradle.kts[]"]
====

The two libraries use `com.example.library` plugin that in turn uses `com.example.java-convention`.
The two libraries use `com.example.library-conventions` plugin that in turn uses `com.example.java-conventions`.
====
include::sample[dir="groovy",files="library-a/build.gradle[];library-b/build.gradle[]"]
include::sample[dir="kotlin",files="library-a/build.gradle.kts[];library-b/build.gradle.kts[]"]
Expand All @@ -32,7 +32,7 @@ include::sample[dir="kotlin",files="library-a/build.gradle.kts[];library-b/build

=== Applying an external plugin in precompiled script plugin

The `com.example.java-convention` plugin uses SpotBugs plugin to perform static code analysis.
The `com.example.java-conventions` plugin uses SpotBugs plugin to perform static code analysis.

SpotBugs is an external plugin - external plugins link:{userManualPath}/custom_plugins.html#applying_external_plugins_in_precompiled_script_plugins[need to be added as implementation dependencies] before they can be applied in a precompiled script plugin:
====
Expand All @@ -46,28 +46,28 @@ include::sample[dir="kotlin",files="buildSrc/build.gradle.kts[tags=repositories-

Once the dependency is added, the external plugin can be applied in precompiled script plugin by id:
====
include::sample[dir="groovy",files="buildSrc/src/main/groovy/com.example.java-convention.gradle[tags=apply-external-plugin]"]
include::sample[dir="kotlin",files="buildSrc/src/main/kotlin/com.example.java-convention.gradle.kts[tags=apply-external-plugin]"]
include::sample[dir="groovy",files="buildSrc/src/main/groovy/com.example.java-conventions.gradle[tags=apply-external-plugin]"]
include::sample[dir="kotlin",files="buildSrc/src/main/kotlin/com.example.java-conventions.gradle.kts[tags=apply-external-plugin]"]
====

=== Applying other precompiled script plugins

Precompiled script plugins can apply other precompiled script plugins.

The `com.example.library` plugin applies the `com.example.java-convention` plugin:
The `com.example.library-conventions` plugin applies the `com.example.java-conventions` plugin:
====
include::sample[dir="groovy",files="buildSrc/src/main/groovy/com.example.library.gradle[tags=plugins]"]
include::sample[dir="kotlin",files="buildSrc/src/main/kotlin/com.example.library.gradle.kts[tags=plugins]"]
include::sample[dir="groovy",files="buildSrc/src/main/groovy/com.example.library-conventions.gradle[tags=plugins]"]
include::sample[dir="kotlin",files="buildSrc/src/main/kotlin/com.example.library-conventions.gradle.kts[tags=plugins]"]
====

=== Using classes from the main source set

Precompiled script plugins can use classes defined in the main source set of the plugins project.

In this sample, `com.example.library` plugin uses a custom task class from `buildSrc/src/main/java` to configure library README checks:
In this sample, `com.example.library-conventions` plugin uses a custom task class from `buildSrc/src/main/java` to configure library README checks:
====
include::sample[dir="groovy",files="buildSrc/src/main/groovy/com.example.library.gradle[tags=use-java-class]"]
include::sample[dir="kotlin",files="buildSrc/src/main/kotlin/com.example.library.gradle.kts[tags=use-java-class]"]
include::sample[dir="groovy",files="buildSrc/src/main/groovy/com.example.library-conventions.gradle[tags=use-java-class]"]
include::sample[dir="kotlin",files="buildSrc/src/main/kotlin/com.example.library-conventions.gradle.kts[tags=use-java-class]"]
====

For more details on authoring custom Gradle plugins, consult the link:{userManualPath}/custom_plugins.html[user manual].
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.example.java-convention'
id 'com.example.java-conventions'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.example.library'
id 'com.example.library-conventions'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'com.example.library'
id 'com.example.library-conventions'
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("com.example.java-convention")
id("com.example.java-conventions")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("com.example.library")
id("com.example.library-conventions")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("com.example.library")
id("com.example.library-conventions")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ We want to apply a set of code quality checking rules to both types of projects

This is achieved by layering three separate plugins:

* `com.example.java-convention` - configures conventions that are generic for any Java project in the organization.
* `com.example.library` - adds publishing configuration to publish to the organization's repository.
* `com.example.service` - adds integration tests and checks for mandatory content in a README.
* `com.example.java-conventions` - configures conventions that are generic for any Java project in the organization.
* `com.example.library-conventions` - adds publishing configuration to publish to the organization's repository.
* `com.example.service-conventions` - adds integration tests and checks for mandatory content in a README.

We've put the plugins in the `plugins` directory in the sample. All plugins created in this sample contain functional tests that use link:{userManualPath}/test_kit.html[TestKit] to verify their behavior.

Expand All @@ -25,7 +25,7 @@ include::sample[dir="kotlin",files="settings.gradle.kts[];consumer-service/build

=== Applying an external plugin in precompiled script plugin

The `com.example.java-convention` plugin uses SpotBugs plugin to perform static code analysis.
The `com.example.java-conventions` plugin uses SpotBugs plugin to perform static code analysis.

SpotBugs is an external plugin - external plugins link:{userManualPath}/custom_plugins.html#applying_external_plugins_in_precompiled_script_plugins[need to be added as implementation dependencies] before they can be applied in a precompiled script plugin:
====
Expand All @@ -39,28 +39,28 @@ include::sample[dir="kotlin",files="plugin/build.gradle.kts[tags=repositories-an

Once the dependency is added, the external plugin can be applied in precompiled script plugin by id:
====
include::sample[dir="groovy",files="plugin/src/main/groovy/com.example.java-convention.gradle[tags=apply-external-plugin]"]
include::sample[dir="kotlin",files="plugin/src/main/kotlin/com.example.java-convention.gradle.kts[tags=apply-external-plugin]"]
include::sample[dir="groovy",files="plugin/src/main/groovy/com.example.java-conventions.gradle[tags=apply-external-plugin]"]
include::sample[dir="kotlin",files="plugin/src/main/kotlin/com.example.java-conventions.gradle.kts[tags=apply-external-plugin]"]
====

=== Applying other precompiled script plugins

Precompiled script plugins can apply other precompiled script plugins.

The `com.example.library` and `com.example.service` plugins both apply the `com.example.java-convention` plugin:
The `com.example.library-conventions` and `com.example.service-conventions` plugins both apply the `com.example.java-conventions` plugin:
====
include::sample[dir="groovy",files="plugin/src/main/groovy/com.example.library.gradle[tags=plugins];plugin/src/main/groovy/com.example.service.gradle[tags=plugins]"]
include::sample[dir="kotlin",files="plugin/src/main/kotlin/com.example.library.gradle.kts[tags=plugins];plugin/src/main/kotlin/com.example.service.gradle.kts[tags=plugins]"]
include::sample[dir="groovy",files="plugin/src/main/groovy/com.example.library-conventions.gradle[tags=plugins];plugin/src/main/groovy/com.example.service-conventions.gradle[tags=plugins]"]
include::sample[dir="kotlin",files="plugin/src/main/kotlin/com.example.library-conventions.gradle.kts[tags=plugins];plugin/src/main/kotlin/com.example.service-conventions.gradle.kts[tags=plugins]"]
====

=== Using classes from the main source set

Precompiled script plugins can use classes defined in the main source set of the plugins project.

In this sample, `com.example.service` plugin uses a custom task class from `src/main/java` to configure service README checks:
In this sample, `com.example.service-conventions` plugin uses a custom task class from `src/main/java` to configure service README checks:
====
include::sample[dir="groovy",files="plugin/src/main/groovy/com.example.service.gradle[tags=use-java-class]"]
include::sample[dir="kotlin",files="plugin/src/main/kotlin/com.example.service.gradle.kts[tags=use-java-class]"]
include::sample[dir="groovy",files="plugin/src/main/groovy/com.example.service-conventions.gradle[tags=use-java-class]"]
include::sample[dir="kotlin",files="plugin/src/main/kotlin/com.example.service-conventions.gradle.kts[tags=use-java-class]"]
====

For more details on authoring custom Gradle plugins, consult the link:{userManualPath}/custom_plugins.html[user manual].
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
plugins {
id 'com.example.service' version '1.0'
id 'com.example.service-conventions' version '1.0'
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
plugins {
id("com.example.service") version "1.0"
id("com.example.service-conventions") version "1.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
plugins {
id 'java-library'
id 'maven-publish'
id 'com.example.java-convention'
id 'com.example.java-conventions'
}
// end::plugins[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// tag::plugins[]
plugins {
id 'com.example.java-convention'
id 'com.example.java-conventions'
}
// end::plugins[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class JavaConventionPluginTest extends PluginTest {
def setup() {
buildFile << """
plugins {
id 'com.example.java-convention'
id 'com.example.java-conventions'
}
"""
}
Expand All @@ -17,9 +17,9 @@ class JavaConventionPluginTest extends PluginTest {
testProjectDir.newFolder('src', 'main', 'java', 'com', 'example')
testProjectDir.newFile('src/main/java/com/example/Foo.java') << """
package com.example;

import java.util.*;

class Foo {
void bar() {
}
Expand All @@ -40,10 +40,10 @@ class JavaConventionPluginTest extends PluginTest {
testProjectDir.newFolder('src', 'main', 'java', 'com', 'example')
testProjectDir.newFile('src/main/java/com/example/Foo.java') << """
package com.example;

class Foo {
final static public String FOO = "BAR";

void bar() {
}
}
Expand All @@ -63,7 +63,7 @@ class JavaConventionPluginTest extends PluginTest {
testProjectDir.newFolder('src', 'main', 'java', 'com', 'example')
testProjectDir.newFile('src/main/java/com/example/Foo.java') << """
package com.example;

class Foo {
void bar() {
String s = null;
Expand All @@ -84,7 +84,7 @@ class JavaConventionPluginTest extends PluginTest {
testProjectDir.newFolder('src', 'main', 'java', 'com', 'example')
testProjectDir.newFile('src/main/java/com/example/Foo.java') << """
package com.example;

public class Foo {
@Deprecated
public void deprecatedMethod() {}
Expand All @@ -93,7 +93,7 @@ class JavaConventionPluginTest extends PluginTest {

testProjectDir.newFile('src/main/java/com/example/Bar.java') << """
package com.example;

public class Bar {
public void bar() {
new Foo().deprecatedMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class LibraryPluginTest extends PluginTest {
def setup() {
buildFile << """
plugins {
id 'com.example.library'
id 'com.example.library-conventions'
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ServicePluginTest extends PluginTest {
def setup() {
buildFile << """
plugins {
id 'com.example.service'
id 'com.example.service-conventions'
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
plugins {
`java-library`
`maven-publish`
id("com.example.java-convention")
id("com.example.java-conventions")
}
// end::plugins[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// tag::plugins[]
plugins {
id("com.example.java-convention")
id("com.example.java-conventions")
}
// end::plugins[]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class JavaConventionPluginTest : PluginTest() {
fun init() {
buildFile.appendText("""
plugins {
id("com.example.java-convention")
id("com.example.java-conventions")
}
""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LibraryPluginTest : PluginTest() {
fun init() {
buildFile.appendText("""
plugins {
id("com.example.library")
id("com.example.library-conventions")
}
""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ServicePluginTest : PluginTest() {
fun init() {
buildFile.appendText("""
plugins {
id("com.example.service")
id("com.example.service-conventions")
}
""")
}
Expand Down