-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
gradle-enterprise-conventions-maven-extension/src/main/java/com/example/CiUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.example; | ||
|
||
import java.util.Optional; | ||
|
||
final class CiUtils { | ||
|
||
private CiUtils() { | ||
} | ||
|
||
static boolean isCi() { | ||
return envVariable("CI").isPresent() || sysProperty("CI").isPresent(); | ||
} | ||
|
||
static Optional<String> envVariable(String name) { | ||
return Optional.ofNullable(System.getenv(name)); | ||
} | ||
|
||
static Optional<String> sysProperty(String name) { | ||
return Optional.ofNullable(System.getProperty(name)); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...extension/src/main/java/com/example/ConventionMavenExtensionGradleEnterpriseListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example; | ||
|
||
import com.gradle.maven.extension.api.GradleEnterpriseApi; | ||
import com.gradle.maven.extension.api.GradleEnterpriseListener; | ||
import com.gradle.maven.extension.api.cache.BuildCacheApi; | ||
import com.gradle.maven.extension.api.scan.BuildScanApi; | ||
import org.apache.maven.execution.MavenSession; | ||
import org.codehaus.plexus.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@SuppressWarnings("unused") | ||
@Component( | ||
role = GradleEnterpriseListener.class, | ||
hint = "gradle-enterprise-conventions-maven-extension", | ||
description = "Configures Gradle Enterprise Build Scans and caching for Maven builds" | ||
) | ||
public final class ConventionMavenExtensionGradleEnterpriseListener implements GradleEnterpriseListener { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(ConventionMavenExtensionGradleEnterpriseListener.class); | ||
|
||
@Override | ||
public void configure(GradleEnterpriseApi api, MavenSession session) { | ||
logger.debug("Executing extension: " + getClass().getSimpleName()); | ||
CustomGradleEnterpriseConfig customGradleEnterpriseConfig = new CustomGradleEnterpriseConfig(); | ||
|
||
logger.debug("Configuring Gradle Enterprise"); | ||
customGradleEnterpriseConfig.configureGradleEnterprise(api); | ||
logger.debug("Finished configuring Gradle Enterprise"); | ||
|
||
logger.debug("Configuring build scan publishing and applying build scan enhancements"); | ||
BuildScanApi buildScan = api.getBuildScan(); | ||
customGradleEnterpriseConfig.configureBuildScanPublishing(buildScan); | ||
customGradleEnterpriseConfig.extendBuildScan(api, session); | ||
logger.debug("Finished configuring build scan publishing and applying build scan enhancements"); | ||
|
||
logger.debug("Configuring build cache"); | ||
BuildCacheApi buildCache = api.getBuildCache(); | ||
customGradleEnterpriseConfig.configureBuildCache(buildCache); | ||
logger.debug("Finished configuring build cache"); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
...e-conventions-maven-extension/src/main/java/com/example/CustomGradleEnterpriseConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.example; | ||
|
||
import com.gradle.maven.extension.api.GradleEnterpriseApi; | ||
import com.gradle.maven.extension.api.cache.BuildCacheApi; | ||
import com.gradle.maven.extension.api.cache.NormalizationProvider.RuntimeClasspathNormalization; | ||
import com.gradle.maven.extension.api.scan.BuildScanApi; | ||
import org.apache.maven.execution.MavenSession; | ||
|
||
import static com.example.CiUtils.isCi; | ||
|
||
/** | ||
* Provide standardized Gradle Enterprise configuration. | ||
* By applying the extension, these settings will automatically be applied. | ||
*/ | ||
final class CustomGradleEnterpriseConfig { | ||
|
||
void configureGradleEnterprise(GradleEnterpriseApi gradleEnterprise) { | ||
//todo: change to your own Gradle Enterprise instance | ||
gradleEnterprise.setServer("https://ge.example.com"); | ||
gradleEnterprise.setAllowUntrustedServer(false); | ||
} | ||
|
||
void configureBuildScanPublishing(BuildScanApi buildScans) { | ||
buildScans.publishAlways(); | ||
buildScans.setUploadInBackground(!isCi()); | ||
buildScans.capture(capture -> capture.setGoalInputFiles(true)); | ||
} | ||
|
||
void configureBuildCache(BuildCacheApi buildCache) { | ||
// Enable the local build cache for all local and CI builds | ||
buildCache.getLocal().setEnabled(true); | ||
|
||
// For short-lived CI agents, it makes sense to disable the local build cache: | ||
// buildCache.getLocal().setEnabled(!isCi()); | ||
|
||
buildCache.getRemote().setEnabled(true); | ||
buildCache.getRemote().setStoreEnabled(isCi()); | ||
|
||
buildCache.registerNormalizationProvider(context -> | ||
context.configureRuntimeClasspathNormalization(this::configureNormalizations) | ||
); | ||
} | ||
|
||
void configureNormalizations(RuntimeClasspathNormalization normalization) { | ||
// https://docs.gradle.com/enterprise/maven-extension/#normalization | ||
// For example, to normalize the "Implementation-Version" attribute of META-INF/MANIFEST.MF files: | ||
// normalization.configureMetaInf(metaInf -> | ||
// metaInf.addIgnoredAttributes("Implementation-Version") | ||
// ); | ||
} | ||
|
||
void extendBuildScan(GradleEnterpriseApi api, MavenSession session) { | ||
BuildScanApi buildScan = api.getBuildScan(); | ||
|
||
// Use `buildScan` to create commonly needed tags, values, and links. For example: | ||
// buildScan.tag("Sample Tag"); | ||
// buildScan.value("Sample Name", "Sample Value"); | ||
// buildScan.link("Sample Link", "https://example.com"); | ||
} | ||
} |