Skip to content

Commit

Permalink
feat(gradle-plugin): enable lazy configuration
Browse files Browse the repository at this point in the history
changes:
  - enable lazy evaluation by
    - replacing all Task configurations with gradles Property types
    - using `register` instead of `create` to register the task
  - remove wrongly set task dependencies that can cause circular dependencies. Task dependencies are now inferred by the set classpath. When gradle detects that an output of a task (like compileJava) is used an automatic dependency is formed and `resolve` will always execute compileJava first. The advantage is that this is dynamic. If one does NOT want `compileJava` to be executed it won't be as soon as nothing of compileJava is on the configured classpath
  • Loading branch information
nbrugger-tgm authored and frantuma committed Dec 4, 2024
1 parent df88c8a commit 3d4c54a
Show file tree
Hide file tree
Showing 2 changed files with 300 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import org.gradle.api.Action;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;

public class SwaggerPlugin implements Plugin<Project> {
public void apply(Project project) {
Expand All @@ -21,24 +22,20 @@ public void execute(DependencySet dependencies) {
dependencies.add(project.getDependencies().create("javax.servlet:javax.servlet-api:3.1.0"));
}
});
Task task = project.getTasks().create("resolve", ResolveTask.class);
((ResolveTask)task).setBuildClasspath(config);

try {
if (project.getTasks().findByPath("classes") != null) {
task.dependsOn("classes");
}
if (project.getTasks().findByPath("compileJava") != null) {
task.dependsOn("compileJava");
}
if (project.getTasks().findByPath("compileTestJava") != null) {
task.dependsOn("compileTestJava");
}
if (project.getTasks().findByPath("testClasses") != null) {
task.dependsOn("testClasses");
}
} catch (Exception e) {
project.getLogger().warn("Exception in task dependencies: " + e.getMessage(), e);
}
TaskProvider<ResolveTask> lazyTask = project.getTasks().register("resolve", ResolveTask.class,task -> {
task.buildClasspath.setFrom(config);
task.classpath.setFrom(project.getExtensions().findByType(SourceSetContainer.class).getByName("main").getRuntimeClasspath().getFiles());
task.prettyPrint.convention(false);
task.readAllResources.convention(true);
task.outputFormat.convention(ResolveTask.Format.JSON);
task.skip.convention(false);
task.encoding.convention("UTF-8");
task.sortOutput.convention(Boolean.FALSE);
task.alwaysResolveAppPath.convention(Boolean.FALSE);
task.skipResolveAppPath.convention(Boolean.FALSE);
task.openAPI31.convention(false);
task.convertToOpenAPI31.convention(false);
task.outputDir.convention(project.getLayout().getBuildDirectory().dir("swagger"));
});
}
}
Loading

0 comments on commit 3d4c54a

Please sign in to comment.