Skip to content

Commit e1882f8

Browse files
authored
Remove lazy evaluation from plugin properties building (#85871)
Plugins and modules use PluginBuildPlugin in gradle to build their plugin properties file. Internally that uses an afterEvaluate since the plugin properties extension will not have been filled in yet when the plugin is first applied. This commit removes the afterEvaluate in favor of configuring the plugin properties map for filling in the template within the pluginProperties task configuration, which is applied when the task is actually realized in the task graph.
1 parent 1ba783b commit e1882f8

File tree

2 files changed

+31
-47
lines changed

2 files changed

+31
-47
lines changed

build-tools/src/main/java/org/elasticsearch/gradle/plugin/PluginBuildPlugin.java

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
import org.gradle.api.plugins.JavaPlugin;
3535
import org.gradle.api.plugins.JavaPluginExtension;
3636
import org.gradle.api.provider.Provider;
37-
import org.gradle.api.publish.PublishingExtension;
38-
import org.gradle.api.publish.maven.MavenPublication;
39-
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
4037
import org.gradle.api.tasks.Copy;
4138
import org.gradle.api.tasks.SourceSetContainer;
4239
import org.gradle.api.tasks.Sync;
@@ -74,42 +71,6 @@ public void apply(final Project project) {
7471
configureDependencies(project);
7572

7673
final var bundleTask = createBundleTasks(project, extension);
77-
project.afterEvaluate(project1 -> {
78-
configurePublishing(project1, extension);
79-
String name = extension.getName();
80-
project1.setProperty("archivesBaseName", name);
81-
project1.setDescription(extension.getDescription());
82-
83-
if (extension.getName() == null) {
84-
throw new InvalidUserDataException("name is a required setting for esplugin");
85-
}
86-
87-
if (extension.getDescription() == null) {
88-
throw new InvalidUserDataException("description is a required setting for esplugin");
89-
}
90-
91-
if (extension.getType().equals(PluginType.BOOTSTRAP) == false && extension.getClassname() == null) {
92-
throw new InvalidUserDataException("classname is a required setting for esplugin");
93-
}
94-
95-
Map<String, Object> map = new LinkedHashMap<>(12);
96-
map.put("name", extension.getName());
97-
map.put("description", extension.getDescription());
98-
map.put("version", extension.getVersion());
99-
map.put("elasticsearchVersion", Version.fromString(VersionProperties.getElasticsearch()).toString());
100-
map.put("javaVersion", project1.getExtensions().getByType(JavaPluginExtension.class).getTargetCompatibility().toString());
101-
map.put("classname", extension.getType().equals(PluginType.BOOTSTRAP) ? "" : extension.getClassname());
102-
map.put("extendedPlugins", extension.getExtendedPlugins().stream().collect(Collectors.joining(",")));
103-
map.put("hasNativeController", extension.isHasNativeController());
104-
map.put("requiresKeystore", extension.isRequiresKeystore());
105-
map.put("type", extension.getType().toString());
106-
map.put("javaOpts", extension.getJavaOpts());
107-
map.put("licensed", extension.isLicensed());
108-
project1.getTasks().withType(Copy.class).named("pluginProperties").configure(copy -> {
109-
copy.expand(map);
110-
copy.getInputs().properties(map);
111-
});
112-
});
11374
project.getConfigurations().getByName("default").extendsFrom(project.getConfigurations().getByName("runtimeClasspath"));
11475

11576
// allow running ES with this plugin in the foreground of a build
@@ -134,14 +95,6 @@ private static NamedDomainObjectContainer<ElasticsearchCluster> testClusters(Pro
13495
return (NamedDomainObjectContainer<ElasticsearchCluster>) project.getExtensions().getByName(extensionName);
13596
}
13697

137-
private static void configurePublishing(Project project, PluginPropertiesExtension extension) {
138-
if (project.getPlugins().hasPlugin(MavenPublishPlugin.class)) {
139-
PublishingExtension publishingExtension = project.getExtensions().getByType(PublishingExtension.class);
140-
MavenPublication elastic = publishingExtension.getPublications().maybeCreate("elastic", MavenPublication.class);
141-
elastic.setArtifactId(extension.getName());
142-
}
143-
}
144-
14598
private static void configureDependencies(final Project project) {
14699
var dependencies = project.getDependencies();
147100
dependencies.add("compileOnly", "org.elasticsearch:elasticsearch:" + VersionProperties.getElasticsearch());
@@ -189,6 +142,35 @@ public void execute(Task task) {
189142
copy.dependsOn(copyPluginPropertiesTemplate);
190143
copy.from(templateFile);
191144
copy.into(new File(project.getBuildDir(), "generated-resources"));
145+
146+
if (extension.getName() == null) {
147+
throw new InvalidUserDataException("name is a required setting for esplugin");
148+
}
149+
150+
if (extension.getDescription() == null) {
151+
throw new InvalidUserDataException("description is a required setting for esplugin");
152+
}
153+
154+
if (extension.getType().equals(PluginType.BOOTSTRAP) == false && extension.getClassname() == null) {
155+
throw new InvalidUserDataException("classname is a required setting for esplugin");
156+
}
157+
158+
Map<String, Object> map = new LinkedHashMap<>(12);
159+
map.put("name", extension.getName());
160+
map.put("description", extension.getDescription());
161+
map.put("version", extension.getVersion());
162+
map.put("elasticsearchVersion", Version.fromString(VersionProperties.getElasticsearch()).toString());
163+
map.put("javaVersion", project.getExtensions().getByType(JavaPluginExtension.class).getTargetCompatibility().toString());
164+
map.put("classname", extension.getType().equals(PluginType.BOOTSTRAP) ? "" : extension.getClassname());
165+
map.put("extendedPlugins", extension.getExtendedPlugins().stream().collect(Collectors.joining(",")));
166+
map.put("hasNativeController", extension.isHasNativeController());
167+
map.put("requiresKeystore", extension.isRequiresKeystore());
168+
map.put("type", extension.getType().toString());
169+
map.put("javaOpts", extension.getJavaOpts());
170+
map.put("licensed", extension.isLicensed());
171+
172+
copy.expand(map);
173+
copy.getInputs().properties(map);
192174
});
193175
// add the plugin properties and metadata to test resources, so unit tests can
194176
// know about the plugin (used by test security code to statically initialize the plugin in unit tests)

build-tools/src/main/java/org/elasticsearch/gradle/plugin/PluginPropertiesExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public String getName() {
6666
}
6767

6868
public void setName(String name) {
69+
this.project.setProperty("archivesBaseName", name);
6970
this.name = name;
7071
}
7172

@@ -82,6 +83,7 @@ public String getDescription() {
8283
}
8384

8485
public void setDescription(String description) {
86+
project.setDescription(description);
8587
this.description = description;
8688
}
8789

0 commit comments

Comments
 (0)