1616
1717package org .springframework .boot .build .architecture ;
1818
19- import java .util .ArrayList ;
20- import java .util .List ;
21-
2219import org .gradle .api .Plugin ;
2320import org .gradle .api .Project ;
24- import org .gradle .api .Task ;
2521import org .gradle .api .plugins .JavaPlugin ;
2622import org .gradle .api .plugins .JavaPluginExtension ;
2723import org .gradle .api .tasks .SourceSet ;
2824import org .gradle .api .tasks .TaskProvider ;
25+ import org .gradle .api .tasks .compile .JavaCompile ;
2926import org .gradle .language .base .plugins .LifecycleBasePlugin ;
27+ import org .jetbrains .kotlin .gradle .tasks .KotlinCompileTool ;
3028
3129import org .springframework .util .StringUtils ;
3230
@@ -44,26 +42,39 @@ public void apply(Project project) {
4442
4543 private void registerTasks (Project project ) {
4644 JavaPluginExtension javaPluginExtension = project .getExtensions ().getByType (JavaPluginExtension .class );
47- List <TaskProvider <ArchitectureCheck >> packageTangleChecks = new ArrayList <>();
4845 for (SourceSet sourceSet : javaPluginExtension .getSourceSets ()) {
49- TaskProvider <ArchitectureCheck > checkPackageTangles = project .getTasks ()
50- .register ("checkArchitecture" + StringUtils .capitalize (sourceSet .getName ()), ArchitectureCheck .class ,
51- (task ) -> {
52- task .getSourceSet ().set (sourceSet .getName ());
53- task .getCompileClasspath ().from (sourceSet .getCompileClasspath ());
54- task .setClasses (sourceSet .getOutput ().getClassesDirs ());
55- task .getResourcesDirectory ().set (sourceSet .getOutput ().getResourcesDir ());
56- task .dependsOn (sourceSet .getProcessResourcesTaskName ());
57- task .setDescription ("Checks the architecture of the classes of the " + sourceSet .getName ()
58- + " source set." );
59- task .setGroup (LifecycleBasePlugin .VERIFICATION_GROUP );
60- });
61- packageTangleChecks .add (checkPackageTangles );
62- }
63- if (!packageTangleChecks .isEmpty ()) {
64- TaskProvider <Task > checkTask = project .getTasks ().named (LifecycleBasePlugin .CHECK_TASK_NAME );
65- checkTask .configure ((check ) -> check .dependsOn (packageTangleChecks ));
46+ registerArchitectureCheck (sourceSet , "java" , project )
47+ .configure ((task ) -> task .setClasses (project .files (project .getTasks ()
48+ .named (sourceSet .getCompileTaskName ("java" ), JavaCompile .class )
49+ .flatMap ((compile ) -> compile .getDestinationDirectory ()))));
50+ project .getPlugins ()
51+ .withId ("org.jetbrains.kotlin.jvm" ,
52+ (kotlinPlugin ) -> registerArchitectureCheck (sourceSet , "kotlin" , project )
53+ .configure ((task ) -> task .setClasses (project .files (project .getTasks ()
54+ .named (sourceSet .getCompileTaskName ("kotlin" ), KotlinCompileTool .class )
55+ .flatMap ((compile ) -> compile .getDestinationDirectory ())))));
6656 }
6757 }
6858
59+ private TaskProvider <ArchitectureCheck > registerArchitectureCheck (SourceSet sourceSet , String language ,
60+ Project project ) {
61+ TaskProvider <ArchitectureCheck > checkArchitecture = project .getTasks ()
62+ .register (
63+ "checkArchitecture"
64+ + StringUtils .capitalize (sourceSet .getName () + StringUtils .capitalize (language )),
65+ ArchitectureCheck .class , (task ) -> {
66+ task .getSourceSet ().set (sourceSet .getName ());
67+ task .getCompileClasspath ().from (sourceSet .getCompileClasspath ());
68+ task .getResourcesDirectory ().set (sourceSet .getOutput ().getResourcesDir ());
69+ task .dependsOn (sourceSet .getProcessResourcesTaskName ());
70+ task .setDescription ("Checks the architecture of the " + language + " classes of the "
71+ + sourceSet .getName () + " source set." );
72+ task .setGroup (LifecycleBasePlugin .VERIFICATION_GROUP );
73+ });
74+ project .getTasks ()
75+ .named (LifecycleBasePlugin .CHECK_TASK_NAME )
76+ .configure ((check ) -> check .dependsOn (checkArchitecture ));
77+ return checkArchitecture ;
78+ }
79+
6980}
0 commit comments