-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap VersionPropertiesLoader in a BuildService to decouple build logi…
…c projects (#78704) By using a BuildService we can decouple the configuration of projects but keep making expensive operations (e.g. parsing properties file or reading minimumJavaCompiler from file) only once per build. This bring us closer to decouple projects and get ultimatively configuration cache compliant. In general we will bring in more BuildServices for the main build to follow along that Pattern and ultimatevely remove usages of allprojects and subprojects in our build.
- Loading branch information
Showing
7 changed files
with
141 additions
and
30 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
...ain/java/org/elasticsearch/gradle/internal/conventions/VersionPropertiesBuildService.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,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal.conventions; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.JavaVersion; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.provider.ProviderFactory; | ||
import org.gradle.api.services.BuildService; | ||
import org.gradle.api.services.BuildServiceParameters; | ||
import org.gradle.initialization.layout.BuildLayout; | ||
import org.gradle.initialization.layout.BuildLayoutFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
import java.util.function.Function; | ||
|
||
abstract class VersionPropertiesBuildService implements BuildService<VersionPropertiesBuildService.Params>, AutoCloseable { | ||
|
||
private final Properties properties; | ||
|
||
@Inject | ||
public VersionPropertiesBuildService(ProviderFactory providerFactory) { | ||
File infoPath = getParameters().getInfoPath().getAsFile().get(); | ||
try { | ||
File propertiesInputFile = new File(infoPath, "version.properties"); | ||
properties = VersionPropertiesLoader.loadBuildSrcVersion(propertiesInputFile, providerFactory); | ||
properties.computeIfAbsent("minimumJava", s -> resolveMinimumJavaVersion(infoPath)); | ||
} catch (IOException e) { | ||
throw new GradleException("Cannot load VersionPropertiesBuildService", e); | ||
} | ||
} | ||
|
||
private JavaVersion resolveMinimumJavaVersion(File infoPath) { | ||
final JavaVersion minimumJavaVersion; | ||
File minimumJavaInfoSource = new File(infoPath, "src/main/resources/minimumCompilerVersion"); | ||
try { | ||
String versionString = FileUtils.readFileToString(minimumJavaInfoSource); | ||
minimumJavaVersion = JavaVersion.toVersion(versionString); | ||
} catch (IOException e) { | ||
throw new GradleException("Cannot resolve minimum compiler version via VersionPropertiesBuildService", e); | ||
} | ||
return minimumJavaVersion; | ||
} | ||
|
||
public Properties getProperties() { | ||
return properties; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
} | ||
|
||
public interface Params extends BuildServiceParameters { | ||
RegularFileProperty getInfoPath(); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
.../src/main/java/org/elasticsearch/gradle/internal/conventions/VersionPropertiesPlugin.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,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal.conventions; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.provider.Provider; | ||
import org.gradle.initialization.layout.BuildLayout; | ||
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
|
||
public class VersionPropertiesPlugin implements Plugin<Project> { | ||
|
||
private BuildLayout buildLayout; | ||
|
||
@Inject | ||
VersionPropertiesPlugin(BuildLayout buildLayout) { | ||
this.buildLayout = buildLayout; | ||
} | ||
|
||
@Override | ||
public void apply(Project project) { | ||
// Register the service if not done yet | ||
File infoPath = new File(buildLayout.getRootDirectory(), "build-tools-internal"); | ||
Provider<VersionPropertiesBuildService> serviceProvider = project.getGradle().getSharedServices() | ||
.registerIfAbsent("versions", VersionPropertiesBuildService.class, spec -> { | ||
spec.getParameters().getInfoPath().set(infoPath); | ||
}); | ||
project.getExtensions().add("versions", serviceProvider.forUseAtConfigurationTime().get().getProperties()); | ||
} | ||
} |
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
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
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
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