in src/main/java/software/amazon/smithy/gradle/SmithyPlugin.java [49:111]
public void apply(Project project) {
// Ensure that the Java plugin is applied.
project.getPluginManager().apply(JavaPlugin.class);
// Register the Smithy extension so that tasks can be configured.
SmithyExtension extension = project.getExtensions().create("smithy", SmithyExtension.class);
// Register the smithyValidate task.
TaskProvider<Validate> validateProvider = project.getTasks()
.register("smithyValidate", Validate.class);
// Register the "smithyBuildJar" task. It's configured once the extension is available.
TaskProvider<SmithyBuildJar> buildJarProvider = project.getTasks()
.register("smithyBuildJar", SmithyBuildJar.class);
// Register the "smithyTags" task.
TaskProvider<SmithyTagsTask> smithyTagsProvider = project.getTasks()
.register("smithyTags", SmithyTagsTask.class);
validateProvider.configure(validateTask -> {
validateTask.dependsOn("jar");
validateTask.dependsOn(buildJarProvider);
Task jarTask = project.getTasks().getByName("jar");
// Only run the validate task if the jar task is enabled and did work.
validateTask.setEnabled(jarTask.getEnabled());
validateTask.onlyIf(t -> jarTask.getState().getDidWork());
// Use only model discovery with the built JAR + the runtime classpath when validating.
validateTask.setAddRuntimeClasspath(true);
validateTask.setClasspath(jarTask.getOutputs().getFiles());
// Set to an empty collection to ensure it doesn't include the source models in the project.
validateTask.setModels(project.files());
validateTask.setAllowUnknownTraits(extension.getAllowUnknownTraits());
});
buildJarProvider.configure(buildJarTask -> {
registerSourceSets(project);
addCliDependencies(project);
buildJarTask.setAllowUnknownTraits(extension.getAllowUnknownTraits());
// We need to manually add these files as a dependency because we
// dynamically inject the Smithy CLI JARs into the classpath.
Configuration smithyCliFiles = project.getConfigurations().getByName("smithyCli");
buildJarTask.getInputs().files(smithyCliFiles);
});
// This plugin supports loading Smithy models from various locations, including
// META-INF/smithy. It also creates a staging directory for all of the merged
// resources that were found in each search location. This can cause conflicts
// between the META-INF/smithy files and staging directory, so we need to
// ignore duplicate conflicts.
ProcessResources task = project.getTasks().withType(ProcessResources.class).getByName("processResources");
task.setDuplicatesStrategy(DuplicatesStrategy.EXCLUDE);
task.dependsOn(buildJarProvider);
smithyTagsProvider.configure(smithyTagsTask -> {
Task jarTask = project.getTasks().getByName("jar");
// Only run the smithyTags task if the jar task is enabled.
smithyTagsTask.setEnabled(jarTask.getEnabled());
});
project.getTasks().getByName("jar").dependsOn(smithyTagsProvider);
project.getTasks().getByName("test").dependsOn(validateProvider);
}