Skip to content

Commit

Permalink
Add convention sample for single build Gradle plugin and Maven extension
Browse files Browse the repository at this point in the history
  • Loading branch information
erichaagdev committed Jul 29, 2024
1 parent 0ad93f7 commit f4e7a9c
Show file tree
Hide file tree
Showing 42 changed files with 2,232 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ jobs:
sed -i '/distributionSha256Sum.*/d' gradle/wrapper/gradle-wrapper.properties
./gradlew wrapper --gradle-version=${{ matrix.versions.version }} --no-scan
./gradlew wrapper --gradle-version=${{ matrix.versions.version }} --no-scan
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.GE_SOLUTIONS_ACCESS_TOKEN }}
- name: Verify example build
id: build
working-directory: convention-develocity-gradle-plugin/examples/gradle_${{ matrix.versions.sample }}
run: ./gradlew build -Ddevelocity.url=https://ge.solutions-team.gradle.com
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.GE_SOLUTIONS_ACCESS_TOKEN }}
DEVELOCITY_ACCESS_KEY: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}
- name: Verify Build Scan published
if: ${{ !steps.build.outputs.build-scan-url }}
run: echo "::error ::No Build Scan published"; exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ jobs:
- name: Set Maven version
if: ${{ matrix.versions.version != '(Current)' }}
working-directory: convention-develocity-maven-extension/examples/maven_${{ matrix.versions.sample }}
run: ./mvnw wrapper:wrapper -Dmaven=${{ matrix.versions.version }} -Dgradle.enterprise.url=https://ge.solutions-team.gradle.com
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.GE_SOLUTIONS_ACCESS_TOKEN }}
run: ./mvnw wrapper:wrapper -Dmaven=${{ matrix.versions.version }} -Ddevelocity.scan.disabled
- name: Verify example build
working-directory: convention-develocity-maven-extension/examples/maven_${{ matrix.versions.sample }}
run: ./mvnw clean verify -Ddevelocity.url=https://ge.solutions-team.gradle.com
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.GE_SOLUTIONS_ACCESS_TOKEN }}
DEVELOCITY_ACCESS_KEY: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}
123 changes: 123 additions & 0 deletions .github/workflows/convention-develocity-shared-verification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
name: Verify Convention Develocity Shared

on:
push:
branches: [ main ]
paths: [ 'convention-develocity-shared/**' ]
pull_request:
branches: [ main ]
paths: [ 'convention-develocity-shared/**' ]
workflow_dispatch:

jobs:
build:
name: Build Convention Develocity Shared
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 8
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v3
- name: Build with Gradle
working-directory: convention-develocity-shared
run: ./gradlew build publishToMavenLocal
- name: Upload convention
uses: actions/upload-artifact@v4
with:
name: convention-develocity-shared
path: ~/.m2/repository/com/myorg

verification_gradle:
name: Verify Example Build for Gradle ${{ matrix.versions.version }}
needs: [build]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
versions:
- sample: '5'
version: '5.0'
- sample: '6'
version: '6.0.1'
- sample: '6.9_and_later'
version: '6.9.4'
- sample: '6.9_and_later'
version: '7.0.2'
- sample: '6.9_and_later'
version: '8.0.2'
- sample: '6.9_and_later'
version: '(Current)'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 8
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v3
- name: Download plugin
uses: actions/download-artifact@v4
with:
name: convention-develocity-shared
path: ~/.m2/repository/com/myorg
- name: Set Gradle version
if: ${{ matrix.versions.version != '(Current)' }}
working-directory: convention-develocity-shared/examples/gradle_${{ matrix.versions.sample }}
run: |
sed -i '/distributionSha256Sum.*/d' gradle/wrapper/gradle-wrapper.properties
./gradlew wrapper --gradle-version=${{ matrix.versions.version }} --no-scan
./gradlew wrapper --gradle-version=${{ matrix.versions.version }} --no-scan
- name: Verify example build
id: build
working-directory: convention-develocity-shared/examples/gradle_${{ matrix.versions.sample }}
run: ./gradlew build -Ddevelocity.url=https://ge.solutions-team.gradle.com
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}
- name: Verify Build Scan published
if: ${{ !steps.build.outputs.build-scan-url }}
run: echo "::error ::No Build Scan published"; exit 1

verification:
name: Verify Example Build for Maven ${{ matrix.versions.version }}
needs: [build]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
versions:
- sample: '3'
version: '3.6.3'
- sample: '3'
version: '3.8.8'
- sample: '3'
version: '(Current)'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 8
uses: actions/setup-java@v4
with:
java-version: '8'
distribution: 'temurin'
cache: maven
- name: Download extension
uses: actions/download-artifact@v4
with:
name: convention-develocity-shared
path: ~/.m2/repository/com/myorg
- name: Set Maven version
if: ${{ matrix.versions.version != '(Current)' }}
working-directory: convention-develocity-shared/examples/maven_${{ matrix.versions.sample }}
run: ./mvnw wrapper:wrapper -Dmaven=${{ matrix.versions.version }} -Ddevelocity.scan.disabled
- name: Verify example build
working-directory: convention-develocity-shared/examples/maven_${{ matrix.versions.sample }}
run: ./mvnw clean verify -Ddevelocity.url=https://ge.solutions-team.gradle.com
env:
DEVELOCITY_ACCESS_KEY: ${{ secrets.DV_SOLUTIONS_ACCESS_KEY }}
3 changes: 3 additions & 0 deletions convention-develocity-shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Convention Develocity Shared

[//]: # (todo)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id("java-gradle-plugin")
id("maven-publish")
}

repositories {
gradlePluginPortal()
}

dependencies {
implementation("com.gradle:develocity-gradle-plugin:3.17.6")
implementation("com.gradle:common-custom-user-data-gradle-plugin:2.0.2")
implementation(project(":convention-develocity-shared"))
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}

gradlePlugin {
plugins {
create("develocityConventions") {
// CHANGE ME: change for your organization
id = "com.myorg.convention-develocity-gradle-plugin"
displayName = "Convention Develocity Gradle Plugin"
description = "A Gradle plugin to apply and configure the Develocity Gradle plugin for com.myorg"
implementationClass = "com.myorg.ConventionDevelocityGradlePlugin"
}
}
}

tasks.withType<ValidatePlugins>().configureEach {
failOnWarning = true
enableStricterValidation = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.myorg;

import com.gradle.CommonCustomUserDataGradlePlugin;
import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import com.gradle.develocity.agent.gradle.DevelocityPlugin;
import com.myorg.configurable.GradleDevelocityConfigurable;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.initialization.Settings;
import org.gradle.util.GradleVersion;

/**
* An example Gradle plugin for enabling and configuring Develocity features
*/
final class ConventionDevelocityGradlePlugin implements Plugin<Object> {

@Override
public void apply(Object target) {
if (target instanceof Settings) {
if (!isGradle6OrNewer()) {
throw new GradleException("For Gradle versions prior to 6.0, the Convention Develocity plugin must be applied to the Root project");
}
configureGradle6OrNewer((Settings) target);
} else if (target instanceof Project) {
if (isGradle6OrNewer()) {
throw new GradleException("For Gradle versions 6.0 and newer, the Convention Develocity plugin must be applied to Settings");
} else if (isGradle5OrNewer()) {
Project project = (Project) target;
if (!project.equals(project.getRootProject())) {
throw new GradleException("For Gradle versions prior to 6.0, the Convention Develocity plugin must be applied to the Root project");
}
configureGradle5(project);
} else {
throw new GradleException("For Gradle versions prior to 5.0, the Convention Develocity plugin is not supported");
}
}
}

private void configureGradle6OrNewer(Settings settings) {
settings.getPluginManager().apply(DevelocityPlugin.class);
settings.getPluginManager().apply(CommonCustomUserDataGradlePlugin.class);

DevelocityConventions develocityConventions = new DevelocityConventions();
develocityConventions.configureDevelocity(new GradleDevelocityConfigurable(settings.getExtensions().getByType(DevelocityConfiguration.class)));
// todo develocityConventions.configureBuildCache(...);
}

private void configureGradle5(Project project) {
project.getPluginManager().apply(DevelocityPlugin.class);
project.getPluginManager().apply(CommonCustomUserDataGradlePlugin.class);

DevelocityConventions develocityConventions = new DevelocityConventions();
develocityConventions.configureDevelocity(new GradleDevelocityConfigurable(project.getExtensions().getByType(DevelocityConfiguration.class)));
// configureBuildCache is not called because the build cache cannot be configured via a plugin prior to Gradle 6.0
}

private static boolean isGradle6OrNewer() {
return GradleVersion.current().compareTo(GradleVersion.version("6.0")) >= 0;
}

private static boolean isGradle5OrNewer() {
return GradleVersion.current().compareTo(GradleVersion.version("5.0")) >= 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.myorg.configurable;

import com.gradle.develocity.agent.gradle.DevelocityConfiguration;

public final class GradleDevelocityConfigurable implements DevelocityConfigurable {

private final DevelocityConfiguration develocity;

public GradleDevelocityConfigurable(DevelocityConfiguration develocity) {
this.develocity = develocity;
}

@Override
public void setServer(String server) {
develocity.getServer().set(server);
}

@Override
public void setProjectId(String projectId) {
develocity.getProjectId().set(projectId);
}

@Override
public void setAllowUntrustedServer(boolean allowUntrustedServer) {
develocity.getAllowUntrustedServer().set(allowUntrustedServer);
}

@Override
public void setAccessKey(String accessKey) {
develocity.getAccessKey().set(accessKey);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
plugins {
id("java")
id("maven-publish")
}

repositories {
mavenCentral()
}

dependencies {
compileOnly("org.apache.maven:maven-core:3.9.8")
compileOnly("org.codehaus.plexus:plexus-component-annotations:2.2.0")

implementation("com.gradle:develocity-maven-extension:1.21.5")
implementation("com.gradle:common-custom-user-data-maven-extension:2.0")
implementation(project(":convention-develocity-shared"))
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}

publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.myorg;

import com.gradle.develocity.agent.maven.api.DevelocityApi;
import com.gradle.develocity.agent.maven.api.DevelocityListener;
import com.myorg.configurable.MavenDevelocityConfigurable;
import org.apache.maven.execution.MavenSession;
import org.codehaus.plexus.component.annotations.Component;

/**
* An example Maven extension for enabling and configuring Develocity features.
*/
@Component(
role = DevelocityListener.class,
hint = "convention-develocity-maven-extension",
description = "Configures the Develocity Maven extension for com.myorg"
)
public final class ConventionDevelocityMavenExtensionListener implements DevelocityListener {

@Override
public void configure(DevelocityApi develocity, MavenSession session) {
DevelocityConventions develocityConventions = new DevelocityConventions();
develocityConventions.configureDevelocity(new MavenDevelocityConfigurable(develocity));
// todo develocityConventions.configureBuildCache(...);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.myorg.configurable;

import com.gradle.develocity.agent.maven.api.DevelocityApi;

public final class MavenDevelocityConfigurable implements DevelocityConfigurable {

private final DevelocityApi develocity;

public MavenDevelocityConfigurable(DevelocityApi develocity) {
this.develocity = develocity;
}

@Override
public void setServer(String server) {
develocity.setServer(server);
}

@Override
public void setProjectId(String projectId) {
develocity.setProjectId(projectId);
}

@Override
public void setAllowUntrustedServer(boolean allowUntrustedServer) {
develocity.setAllowUntrustedServer(allowUntrustedServer);
}

@Override
public void setAccessKey(String accessKey) {
develocity.setAccessKey(accessKey);
}

}
Loading

0 comments on commit f4e7a9c

Please sign in to comment.