5
5
import javax .inject .Inject ;
6
6
7
7
import org .gradle .api .Action ;
8
+ import org .gradle .api .Project ;
8
9
import org .gradle .api .file .ProjectLayout ;
9
10
import org .gradle .api .model .ObjectFactory ;
10
11
import org .gradle .api .provider .ListProperty ;
11
12
import org .gradle .api .provider .Property ;
12
13
import org .gradle .api .tasks .Input ;
14
+ import org .gradle .api .tasks .Internal ;
13
15
import org .gradle .api .tasks .Nested ;
14
16
15
17
import lombok .Getter ;
@@ -23,14 +25,14 @@ public class StrictNullCheckExtension {
23
25
@ Nested
24
26
private final PackageInfo packageInfo ;
25
27
26
- @ Nested
27
- private final Source source ;
28
+ @ Internal
29
+ private final Project project ;
28
30
29
31
@ Inject
30
- public StrictNullCheckExtension (final ObjectFactory objects , final ProjectLayout layout ) {
32
+ public StrictNullCheckExtension (final ObjectFactory objects , final ProjectLayout layout , final Project project ) {
31
33
this .generatedDir = objects .property (String .class );
32
34
this .packageInfo = objects .newInstance (PackageInfo .class );
33
- this .source = objects . newInstance ( Source . class ) ;
35
+ this .project = project ;
34
36
35
37
this .generatedDir .convention (
36
38
layout
@@ -46,8 +48,44 @@ public void packageInfo(final Action<PackageInfo> action) {
46
48
action .execute (this .packageInfo );
47
49
}
48
50
49
- public void source (final Action <Source > action ) {
50
- action .execute (this .source );
51
+ public void addFindBugs (final String version ) {
52
+ this .addDependency ("com.google.code.findbugs:jsr305:" .concat (version ));
53
+ }
54
+
55
+ public void addFindBugs () {
56
+ this .addFindBugs ("3.0.2" );
57
+ }
58
+
59
+ public void addSpotBugs (final String version ) {
60
+ this .addDependency ("com.github.spotbugs:spotbugs-annotations:" .concat (version ));
61
+ }
62
+
63
+ public void addSpotBugs () {
64
+ this .addSpotBugs ("4.7.3" );
65
+ }
66
+
67
+ public void addEclipse (final String version ) {
68
+ this .addDependency ("org.eclipse.jdt:org.eclipse.jdt.annotation:" .concat (version ));
69
+ }
70
+
71
+ public void addEclipse () {
72
+ this .addEclipse ("2.2.700" );
73
+ }
74
+
75
+ private void addDependency (final String notation ) {
76
+ final var dependency = this .project .getDependencies ().create (notation );
77
+ final var allCompileOnly = this .project
78
+ .getConfigurations ()
79
+ .matching (config -> {
80
+ final var name = config .getName ();
81
+ return name .startsWith ("compileOnly" ) || name .endsWith ("CompileOnly" );
82
+ });
83
+
84
+ allCompileOnly .configureEach (config ->
85
+ config
86
+ .getDependencies ()
87
+ .add (dependency )
88
+ );
51
89
}
52
90
53
91
@ Getter
@@ -112,42 +150,4 @@ public void useEclipse() {
112
150
);
113
151
}
114
152
}
115
-
116
- @ Getter
117
- public static class Source {
118
-
119
- @ Input
120
- private final ListProperty <String > dependencies ;
121
-
122
- @ Inject
123
- public Source (final ObjectFactory objects ) {
124
- this .dependencies = objects .listProperty (String .class );
125
-
126
- this .dependencies .convention (List .of ());
127
- }
128
-
129
- public void addFindBugs (final String version ) {
130
- this .dependencies .add ("com.google.code.findbugs:jsr305:" .concat (version ));
131
- }
132
-
133
- public void addFindBugs () {
134
- this .addFindBugs ("3.0.2" );
135
- }
136
-
137
- public void addSpotBugs (final String version ) {
138
- this .dependencies .add ("com.github.spotbugs:spotbugs-annotations:" .concat (version ));
139
- }
140
-
141
- public void addSpotBugs () {
142
- this .addSpotBugs ("4.7.3" );
143
- }
144
-
145
- public void addEclipse (final String version ) {
146
- this .dependencies .add ("org.eclipse.jdt:org.eclipse.jdt.annotation:" .concat (version ));
147
- }
148
-
149
- public void addEclipse () {
150
- this .addEclipse ("2.2.700" );
151
- }
152
- }
153
153
}
0 commit comments