1
1
package com .igormaznitsa .jcp .gradle ;
2
2
3
- import static java .util .Collections .emptyList ;
4
3
import static java .util .Collections .emptyMap ;
5
- import static org .gradle .api .tasks .SourceSet .MAIN_SOURCE_SET_NAME ;
6
4
7
5
8
6
import com .igormaznitsa .jcp .JcpPreprocessor ;
17
15
import java .util .Arrays ;
18
16
import java .util .Collections ;
19
17
import java .util .List ;
20
- import java .util .Optional ;
21
- import java .util .concurrent .atomic .AtomicBoolean ;
22
18
import java .util .stream .Collectors ;
23
19
import javax .annotation .Nullable ;
24
20
import javax .inject .Inject ;
25
21
import org .apache .commons .io .FilenameUtils ;
26
22
import org .gradle .api .DefaultTask ;
27
23
import org .gradle .api .logging .Logger ;
28
24
import org .gradle .api .model .ObjectFactory ;
29
- import org .gradle .api .plugins .JavaPluginConvention ;
30
25
import org .gradle .api .provider .ListProperty ;
31
26
import org .gradle .api .provider .MapProperty ;
32
27
import org .gradle .api .provider .Property ;
33
28
import org .gradle .api .provider .ProviderFactory ;
34
29
import org .gradle .api .tasks .Input ;
35
30
import org .gradle .api .tasks .InputFiles ;
36
31
import org .gradle .api .tasks .OutputDirectory ;
37
- import org .gradle .api .tasks .SourceSetContainer ;
38
32
import org .gradle .api .tasks .TaskAction ;
39
33
import org .gradle .api .tasks .TaskExecutionException ;
40
34
import org .gradle .execution .commandline .TaskConfigurationException ;
41
35
42
- public class JcpPreprocessTask extends DefaultTask {
36
+ public class JcpTask extends DefaultTask {
43
37
44
38
public static final String ID = "preprocess" ;
45
39
@@ -149,13 +143,8 @@ public class JcpPreprocessTask extends DefaultTask {
149
143
*/
150
144
private final Property <Boolean > dontOverwriteSameContent ;
151
145
152
- /**
153
- * Auto replace java source folders by the target folder.
154
- */
155
- private final Property <Boolean > autoReplaceSources ;
156
-
157
146
@ Inject
158
- public JcpPreprocessTask (ProviderFactory providerFactory ) {
147
+ public JcpTask (ProviderFactory providerFactory ) {
159
148
super ();
160
149
final ObjectFactory factory = this .getProject ().getObjects ();
161
150
@@ -178,58 +167,17 @@ public JcpPreprocessTask(ProviderFactory providerFactory) {
178
167
179
168
this .vars = factory .mapProperty (String .class , String .class );
180
169
181
- this .sources = factory .listProperty (File .class ). convention ( providerFactory . provider (() -> findJavaSourceFolders (). orElse ( emptyList ()))) ;
170
+ this .sources = factory .listProperty (File .class );
182
171
183
172
this .configFiles = factory .listProperty (String .class );
184
173
this .excludeExtensions = factory .listProperty (String .class ).convention (Collections .singletonList ("xml" ));
185
174
this .excludeFolders = factory .listProperty (String .class );
186
175
this .fileExtensions = factory .listProperty (String .class ).convention (new ArrayList <>(Arrays .asList ("java" , "txt" , "htm" , "html" )));
187
176
188
177
this .baseDir = factory .property (File .class ).convention (this .getProject ().getProjectDir ());
189
- this .autoReplaceSources = factory .property (Boolean .class ).convention (false );
190
178
this .target = factory .property (File .class ).convention (new File (this .getProject ().getBuildDir (), "java-comment-preprocessor" + File .separatorChar + this .getTaskIdentity ().name ));
191
179
}
192
180
193
- private Optional <List <File >> findJavaSourceFolders () {
194
- final JavaPluginConvention javaPluginConvention = this .getProject ().getConvention ().findPlugin (JavaPluginConvention .class );
195
-
196
- if (javaPluginConvention == null ) {
197
- this .getLogger ().debug ("Can't find Java plugin" );
198
- return Optional .empty ();
199
- }
200
-
201
- final SourceSetContainer srcSetContainer = javaPluginConvention .getSourceSets ();
202
- return Optional .of (srcSetContainer
203
- .stream ()
204
- .filter (x -> MAIN_SOURCE_SET_NAME .equals (x .getName ()))
205
- .flatMap (x -> x .getJava ().getSrcDirs ().stream ())
206
- .collect (Collectors .toList ()));
207
- }
208
-
209
- private boolean replaceJavaSourceFolders (final File targetFolder ) {
210
- final JavaPluginConvention javaPluginConvention = this .getProject ().getConvention ().findPlugin (JavaPluginConvention .class );
211
- final AtomicBoolean replaced = new AtomicBoolean ();
212
- if (javaPluginConvention == null ) {
213
- this .getLogger ().debug ("Can't find Java plugin" );
214
- } else {
215
- this .getLogger ().debug ("Detected Java plugin, trying to replace its main source set" );
216
- final SourceSetContainer srcSetContainer = javaPluginConvention .getSourceSets ();
217
- srcSetContainer
218
- .stream ()
219
- .filter (x -> MAIN_SOURCE_SET_NAME .equals (x .getName ()))
220
- .forEach (x -> {
221
- x .getJava ().setSrcDirs (Collections .singletonList (targetFolder ));
222
- replaced .set (true );
223
- });
224
- }
225
- return replaced .get ();
226
- }
227
-
228
- @ Input
229
- public Property <Boolean > getAutoReplaceSources () {
230
- return this .autoReplaceSources ;
231
- }
232
-
233
181
@ InputFiles
234
182
public ListProperty <File > getSources () {
235
183
return this .sources ;
@@ -398,7 +346,7 @@ public void warning(@Nullable final String message) {
398
346
399
347
final List <File > sourcesList = this .sources .get ();
400
348
if (sourcesList .isEmpty ()) {
401
- throw new TaskConfigurationException (JcpPreprocessTask .ID , "Source folder list must be defined as 'sources'" , null );
349
+ throw new TaskConfigurationException (JcpTask .ID , "Source folder list must be defined as 'sources'" , null );
402
350
}
403
351
404
352
List <File > preparedSourcesList = new ArrayList <>();
@@ -412,7 +360,7 @@ public void warning(@Nullable final String message) {
412
360
}
413
361
}
414
362
415
- logger .info ("Sources : " + preparedSourcesList );
363
+ logger .info ("Source folders in use : " + preparedSourcesList );
416
364
417
365
preprocessorContext .setSources (preparedSourcesList .stream ().map (File ::getAbsolutePath ).collect (Collectors .toList ()));
418
366
preprocessorContext .setEol (this .eol .get ());
@@ -439,15 +387,8 @@ public void warning(@Nullable final String message) {
439
387
});
440
388
441
389
final JcpPreprocessor preprocessor = new JcpPreprocessor (preprocessorContext );
442
- logger .debug ("Start preprocessing..." );
443
390
391
+ logger .debug ("Preprocessing starting" );
444
392
preprocessor .execute ();
445
-
446
- if (this .autoReplaceSources .get ()) {
447
- logger .info ("Trying auto-replace source folders by the preprocessed one: " + targetFolder );
448
- if (!this .replaceJavaSourceFolders (targetFolder )) {
449
- throw new TaskExecutionException (this , new IllegalStateException ("Can't replace sources automatically, may be some unsupported type of project, use manual replacement!" ));
450
- }
451
- }
452
393
}
453
394
}
0 commit comments