-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Access build tools resources #32201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Access build tools resources #32201
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0fbfdfb
Task skeleton with test failing due to missing implementation
alpar-t 02d7bf0
Task implementation and complete task
alpar-t 5bb35ac
No more whitelist, reduce boilerplate
alpar-t ac290de
Merge remote-tracking branch 'origin/master' into access-build-tools-…
alpar-t c46d30b
Merge remote-tracking branch 'origin/master' into access-build-tools-…
alpar-t 8cfbd67
remove unused enum
alpar-t 7a29a1a
Rename in accordance with PR
alpar-t 43ce307
clarify and extend tests
alpar-t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
buildSrc/src/main/java/org/elasticsearch/gradle/ExportElasticsearchBuildResourcesTask.java
This file contains hidden or 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,109 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.elasticsearch.gradle; | ||
|
|
||
| import org.gradle.api.DefaultTask; | ||
| import org.gradle.api.GradleException; | ||
| import org.gradle.api.file.DirectoryProperty; | ||
| import org.gradle.api.file.RegularFileProperty; | ||
| import org.gradle.api.logging.Logger; | ||
| import org.gradle.api.logging.Logging; | ||
| import org.gradle.api.tasks.Classpath; | ||
| import org.gradle.api.tasks.Input; | ||
| import org.gradle.api.tasks.OutputDirectory; | ||
| import org.gradle.api.tasks.SkipWhenEmpty; | ||
| import org.gradle.api.tasks.StopExecutionException; | ||
| import org.gradle.api.tasks.TaskAction; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Export Elasticsearch build resources to configurable paths | ||
| * <p> | ||
| * Wil overwrite existing files and create missing directories. | ||
| * Useful for resources that that need to be passed to other processes trough the filesystem or otherwise can't be | ||
| * consumed from the classpath. | ||
| */ | ||
| public class ExportElasticsearchBuildResourcesTask extends DefaultTask { | ||
|
|
||
| private final Logger logger = Logging.getLogger(ExportElasticsearchBuildResourcesTask.class); | ||
|
|
||
| private final Set<String> resources = new HashSet<>(); | ||
|
|
||
| private DirectoryProperty outputDir; | ||
|
|
||
| public ExportElasticsearchBuildResourcesTask() { | ||
| outputDir = getProject().getLayout().directoryProperty( | ||
| getProject().getLayout().getBuildDirectory().dir("build-tools-exported") | ||
| ); | ||
| } | ||
|
|
||
| @OutputDirectory | ||
| public DirectoryProperty getOutputDir() { | ||
| return outputDir; | ||
| } | ||
|
|
||
| @Input | ||
| @SkipWhenEmpty | ||
| public Set<String> getResources() { | ||
| return Collections.unmodifiableSet(resources); | ||
| } | ||
|
|
||
| @Classpath | ||
| public String getResourcesClasspath() { | ||
| // This will make sure the task is not considered up to date if the resources are changed. | ||
| logger.info("Classpath: {}", System.getProperty("java.class.path")); | ||
| return System.getProperty("java.class.path"); | ||
| } | ||
|
|
||
| public void setOutputDir(DirectoryProperty outputDir) { | ||
| this.outputDir = outputDir; | ||
| } | ||
|
|
||
| public RegularFileProperty resource(String resource) { | ||
| resources.add(resource); | ||
| return getProject().getLayout().fileProperty(outputDir.file(resource)); | ||
| } | ||
|
|
||
| @TaskAction | ||
| public void doExport() { | ||
| if (resources.isEmpty()) { | ||
| throw new StopExecutionException(); | ||
| } | ||
| resources.stream().parallel() | ||
| .forEach(resourcePath -> { | ||
| Path destination = outputDir.get().file(resourcePath).getAsFile().toPath(); | ||
| try (InputStream is = getClass().getClassLoader().getResourceAsStream(resourcePath)) { | ||
| if (is == null) { | ||
| throw new GradleException("Can't export `" + resourcePath + "` from build-tools: not found"); | ||
| } | ||
| Files.copy(is, destination); | ||
| } catch (IOException e) { | ||
| throw new GradleException("Can't write resource `" + resourcePath + "` to " + destination); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| } |
97 changes: 97 additions & 0 deletions
97
buildSrc/src/test/java/org/elasticsearch/gradle/ExportElasticsearchBuildResourcesTaskIT.java
This file contains hidden or 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,97 @@ | ||
| package org.elasticsearch.gradle; | ||
|
|
||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import org.elasticsearch.gradle.test.GradleIntegrationTestCase; | ||
| import org.gradle.testkit.runner.BuildResult; | ||
| import org.gradle.testkit.runner.BuildTask; | ||
| import org.gradle.testkit.runner.GradleRunner; | ||
| import org.gradle.testkit.runner.TaskOutcome; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
|
|
||
| public class ExportElasticsearchBuildResourcesTaskIT extends GradleIntegrationTestCase { | ||
|
|
||
| public static final String PROJECT_NAME = "elasticsearch-build-resources"; | ||
|
|
||
| public void testUpToDateWithSourcesConfigured() { | ||
| GradleRunner.create() | ||
| .withProjectDir(getProjectDir(PROJECT_NAME)) | ||
| .withArguments("clean", "-s") | ||
| .withPluginClasspath() | ||
| .build(); | ||
|
|
||
| BuildResult result = GradleRunner.create() | ||
| .withProjectDir(getProjectDir(PROJECT_NAME)) | ||
| .withArguments("exportBuildResources", "-s", "-i") | ||
| .withPluginClasspath() | ||
| .build(); | ||
| assertTaskSuccessFull(result, ":exportBuildResources"); | ||
| assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml"); | ||
|
|
||
|
|
||
| result = GradleRunner.create() | ||
| .withProjectDir(getProjectDir(PROJECT_NAME)) | ||
| .withArguments("exportBuildResources", "-s", "-i") | ||
| .withPluginClasspath() | ||
| .build(); | ||
| assertEquals(TaskOutcome.UP_TO_DATE, result.task(":exportBuildResources").getOutcome()); | ||
| assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml"); | ||
| } | ||
|
|
||
| public void testImplicitTaskDependencyCopy() { | ||
| BuildResult result = GradleRunner.create() | ||
| .withProjectDir(getProjectDir(PROJECT_NAME)) | ||
| .withArguments("clean", "sampleCopyAll", "-s", "-i") | ||
| .withPluginClasspath() | ||
| .build(); | ||
| assertTaskSuccessFull(result, ":exportBuildResources"); | ||
| assertTaskSuccessFull(result, ":sampleCopyAll"); | ||
| assertBuildFileExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle.xml"); | ||
| } | ||
|
|
||
| public void testImplicitTaskDependencyInputFileOfOther() { | ||
| BuildResult result = GradleRunner.create() | ||
| .withProjectDir(getProjectDir(PROJECT_NAME)) | ||
| .withArguments("clean", "sample", "-s", "-i") | ||
| .withPluginClasspath() | ||
| .build(); | ||
|
|
||
| assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml"); | ||
| } | ||
|
|
||
| private void assertTaskSuccessFull(BuildResult result, String taskName) { | ||
| BuildTask task = result.task(taskName); | ||
| if (task == null) { | ||
| fail("Expected task `" + taskName + "` to be successful, but it did not run"); | ||
| } | ||
| assertEquals(TaskOutcome.SUCCESS, task.getOutcome()); | ||
| } | ||
|
|
||
| private void assertBuildFileExists(BuildResult result, String projectName, String path) { | ||
| Path absPath = getBuildDir(projectName).toPath().resolve(path); | ||
| assertTrue( | ||
| result.getOutput() + "\n\nExpected `" + absPath + "` to exists but it did not", | ||
| Files.exists(absPath) | ||
| ); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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
29 changes: 29 additions & 0 deletions
29
buildSrc/src/testKit/elasticsearch-build-resources/build.gradle
This file contains hidden or 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,29 @@ | ||
| plugins { | ||
| id 'elasticsearch.build' | ||
| } | ||
|
|
||
| ext.licenseFile = file("$buildDir/dummy/license") | ||
| ext.noticeFile = file("$buildDir/dummy/notice") | ||
|
|
||
| exportBuildResources { | ||
| resource 'checkstyle.xml' | ||
| resource 'minimumCompilerVersion' | ||
| } | ||
|
|
||
| task sampleCopyAll(type: Sync) { | ||
| /** Note: no explicit dependency. This works with tasks that use the Provider API a.k.a "Lazy Configuration" **/ | ||
| from exportBuildResources | ||
| into "$buildDir/sampleCopyAll" | ||
| } | ||
|
|
||
| task sample { | ||
| // This does not work, task dependencies can't be providers | ||
| // dependsOn exportBuildResources.resource('minimumRuntimeVersion') | ||
| // Nor does this, despite https://github.com/gradle/gradle/issues/3811 | ||
| // dependsOn exportBuildResources.outputDir | ||
| // for now it's just | ||
| dependsOn exportBuildResources | ||
| doLast { | ||
| println "This task is using ${file(exportBuildResources.resource('minimumRuntimeVersion'))}" | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use other files, e.x. forbidden APIs to avoid confusion.