11package com .igormaznitsa .jcp .gradle ;
22
3+ import static java .util .Collections .emptyList ;
34import static java .util .Collections .emptyMap ;
45import static org .gradle .api .tasks .SourceSet .MAIN_SOURCE_SET_NAME ;
56
1617import java .util .Arrays ;
1718import java .util .Collections ;
1819import java .util .List ;
20+ import java .util .Optional ;
21+ import java .util .concurrent .atomic .AtomicBoolean ;
1922import java .util .stream .Collectors ;
2023import javax .annotation .Nullable ;
24+ import javax .inject .Inject ;
2125import org .apache .commons .io .FilenameUtils ;
2226import org .gradle .api .DefaultTask ;
2327import org .gradle .api .logging .Logger ;
2630import org .gradle .api .provider .ListProperty ;
2731import org .gradle .api .provider .MapProperty ;
2832import org .gradle .api .provider .Property ;
33+ import org .gradle .api .provider .ProviderFactory ;
2934import org .gradle .api .tasks .Input ;
3035import org .gradle .api .tasks .InputFiles ;
31- import org .gradle .api .tasks .OutputFile ;
36+ import org .gradle .api .tasks .OutputDirectory ;
3237import org .gradle .api .tasks .SourceSetContainer ;
3338import org .gradle .api .tasks .TaskAction ;
3439import org .gradle .api .tasks .TaskExecutionException ;
@@ -144,7 +149,13 @@ public class JcpPreprocessTask extends DefaultTask {
144149 */
145150 private final Property <Boolean > dontOverwriteSameContent ;
146151
147- public JcpPreprocessTask () {
152+ /**
153+ * Auto replace java source folders by the target folder.
154+ */
155+ private final Property <Boolean > autoReplaceSources ;
156+
157+ @ Inject
158+ public JcpPreprocessTask (ProviderFactory providerFactory ) {
148159 super ();
149160 final ObjectFactory factory = this .getProject ().getObjects ();
150161
@@ -167,29 +178,58 @@ public JcpPreprocessTask() {
167178
168179 this .vars = factory .mapProperty (String .class , String .class );
169180
170- final JavaPluginConvention javaPluginConvention = this .getProject ().getConvention ().findPlugin (JavaPluginConvention .class );
171- if (javaPluginConvention == null ) {
172- this .sources = factory .listProperty (File .class );
173- } else {
174- final SourceSetContainer srcSetContainer = javaPluginConvention .getSourceSets ();
175- srcSetContainer .getByName (MAIN_SOURCE_SET_NAME );
176- this .sources = factory .listProperty (File .class ).convention (
177- srcSetContainer
178- .stream ()
179- .filter (x -> MAIN_SOURCE_SET_NAME .equals (x .getName ()))
180- .flatMap (x -> x .getJava ().getSrcDirs ().stream ())
181- .collect (Collectors .toList ())
182- );
183- }
181+ this .sources = factory .listProperty (File .class ).convention (providerFactory .provider (() -> findJavaSourceFolders ().orElse (emptyList ())));
182+
184183 this .configFiles = factory .listProperty (String .class );
185184 this .excludeExtensions = factory .listProperty (String .class ).convention (Collections .singletonList ("xml" ));
186185 this .excludeFolders = factory .listProperty (String .class );
187186 this .fileExtensions = factory .listProperty (String .class ).convention (new ArrayList <>(Arrays .asList ("java" , "txt" , "htm" , "html" )));
188187
189188 this .baseDir = factory .property (File .class ).convention (this .getProject ().getProjectDir ());
189+ this .autoReplaceSources = factory .property (Boolean .class ).convention (false );
190190 this .target = factory .property (File .class ).convention (new File (this .getProject ().getBuildDir (), "java-comment-preprocessor" + File .separatorChar + this .getTaskIdentity ().name ));
191191 }
192192
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+
193233 @ InputFiles
194234 public ListProperty <File > getSources () {
195235 return this .sources ;
@@ -206,7 +246,7 @@ public Property<Boolean> getKeepAttributes() {
206246 }
207247
208248 @ Input
209- @ OutputFile
249+ @ OutputDirectory
210250 public Property <File > getTarget () {
211251 return this .target ;
212252 }
@@ -352,8 +392,9 @@ public void warning(@Nullable final String message) {
352392 }
353393 });
354394
355- logger .info ("Target folder: " + this .target .get ());
356- preprocessorContext .setTarget (this .target .get ());
395+ final File targetFolder = this .target .get ();
396+ logger .info ("Target folder: " + targetFolder );
397+ preprocessorContext .setTarget (targetFolder );
357398
358399 final List <File > sourcesList = this .sources .get ();
359400 if (sourcesList .isEmpty ()) {
@@ -401,5 +442,12 @@ public void warning(@Nullable final String message) {
401442 logger .debug ("Start preprocessing..." );
402443
403444 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+ }
404452 }
405453}
0 commit comments