Add source file extension in gradle


(Viktor Gusev) #1

I am trying to tell gradle that files with the ‘*.dsl’ extension should be compiled as groovy files, so I have added a source set with the inclusion pattern and changed the compile task includes property:

sourceSets {

dsl_scripts {

groovy {

include ‘**/*.dsl’

}

}

}

compileDsl_scriptsGroovy.includes = [’**/*.dsl’]

But when I run the build under a debug mode it skips all ‘*.dsl’ files with the following message:

Skipping task ‘:compileDsl_scriptsGroovy’ as it has no source files

The following line successfully outputs all the files I try to compile:

println sourceSets.dsl_scripts.allSource.matching({include ‘**/*.dsl’}).getFiles()

Also, I found the following snippet in gradle sources:

FileCollection groovyJavaOnly = spec.getSource().filter(new Spec() {

public boolean isSatisfiedBy(File element) {

return element.getName().endsWith(".groovy") || element.getName().endsWith(".java");

}

});

spec.setSource(new SimpleFileCollection(groovyJavaOnly.getFiles()));

Is it possible to override? If not, can we add ‘extensions’ property to GroovyCompile?


(Peter Niederwieser) #2

Every source directory set has a filter that gets applied after the regular includes/excludes. For the ‘groovy’ source directory set, this filter defaults to including ‘.java’ and ‘.groovy’ files. So instead of adding to ‘groovy.include’, adding to ‘groovy.filter.include’ should do the job. No other configuration should be necessary.


(Viktor Gusev) #3

Following your advice I changed the build script to:

sourceSets {
     ...
     dsl_scripts
}
  sourceSets.dsl_scripts.groovy.filter.includes = ['**/*.dsl']
compileDsl_scriptsGroovy.source.filter { include '**/*.dsl'}

The message in debug mode disappeared, but the compiler is called as:

14:29:26.690 [DEBUG] [org.gradle.api.internal.tasks.compile.NormalizingGroovyCompiler] Java compiler arguments: -d /home/myuser/project/build -g -classpath /home/myuser/.gradle/caches/artifacts-13/filestore/org.codehaus.groovy/groovy-all/1.8.6/jar/96a26bfa6e5aea2e3c46df44a19de93a2acd2366/groovy-all-1.8.6.jar:/home/myuser/…cglib-2.2.1-v20090111.jar:/home/myuser/.gradle/caches/artifacts-13/…4cfdf/jsr305-1.3.9.jar

And no dsl files are added to the end of the compilation command. So for some reason the filter is not applied correctly, what do I miss?


(Peter Niederwieser) #4

With ‘includes =’, you are overriding the defaults, and hence you are only including ‘.dsl’ files. What you need is this line:

sourceSets.dsl_scripts.groovy.filter.include(’**/*.dsl’)

You don’t need to configure the task.


(Viktor Gusev) #5

I removed the configuration for the GroovyCompile task and changed ‘includes =’ to ‘include(…)’, but the compilation command from NormalizingGroovyCompiler still does not include the ‘*.dsl’ files as last arguments, but

println sourceSets.dsl_scripts.groovy.matching({include '**/*.dsl'}).getFiles()

successfully prints all of them.

println sourceSets.dsl_scripts.groovy.filter.includes

prints

[**/*.java, **/*.groovy, **/*.dsl]

(Peter Niederwieser) #6

Hmm, seems like a regression introduced by NormalizingGroovyCompiler then. We added another filter there because some people had problems with non-source files being passed to the compiler when they configured their GroovyCompile tasks with sources other than from source sets.

I’ll add an issue for this. In the meantime, ‘compileDsl_scriptsGroovy.groovyOptions.useAnt = true’ should solve the problem.


(Viktor Gusev) #7

With

compileDsl_scriptsGroovy.options.useAnt = true
compileDsl_scriptsGroovy.groovyOptions.useAnt = true

i get the first message:

15:15:13.302 [INFO] [org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter] Skipping task ‘:compileDsl_scriptsJava’ as it has no source files.

> gradle -v
Gradle 1.0

(Viktor Gusev) #8

I get:

15:15:15.351 [DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:groovyc] /home/myuser/…/dsl_scripts/groovy/…/scripts/include/Included.dsl skipped - don’t know how to handle it

Does ant take into account the ‘org.codehaus.groovy.source.Extensions’ file?


(Peter Niederwieser) #9

Does ant take into account the org.codehaus.groovy.source.Extensions file?

Yes it should. There doesn’t seem to be a way to configure additional extensions directly on the Ant groovyc task though.


(Viktor Gusev) #10

ok, thank you for the efforts, I will wait for the fix… afaiu: the bug is in NormalizingGroovyCompiler, line:

return element.getName().endsWith(".groovy") || element.getName().endsWith(".java");

as it filters out all other extensions and does not take the filter into account. Please post the bug number when the issue description is ready.


(Peter Niederwieser) #11

I’ve created http://issues.gradle.org/browse/GRADLE-2372 to track this issue.


(Peter Niederwieser) #12

As I said, that line is there for a reason. But we may have to look for another solution. Meanwhile, ‘useAnt = true’ together with a ‘META-INF/services/org.codehaus.groovy.source.Extensions’ file should be an easy enough way of getting your files to compile. Another option is to compile your ‘.dsl’ files directly with the Ant groovyc task (from Gradle of course) and setting script extension to ‘.dsl’.


(Viktor Gusev) #13

I am not sure the ant groovyc task reads META-INF/services/org.codehaus.groovy.source.Extensions: '[DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter]

[ant:groovyc] /home/myuser/…/dsl_scripts/groovy/…/scripts/include/Included.dsl

skipped - don’t know how to handle it’ Probably I will copy all scripts to a temporal folder, change the extension to ‘groovy’, and compile to the build directory.


(Peter Niederwieser) #14

I am not sure the ant groovyc task reads META-INF/services/org.codehaus.groovy.source.Extensions

It does. I looked it up in the source code.


(Viktor Gusev) #15

in this case, there should be some other reason why ant does not know how to handle *.dsl files as I have no problems with compiling *.dsl files from a command line using groovyc of the same version as gradle’s groovy compiler. Also I checked that gradle can read the ‘org.codehaus.groovy.transform.ASTTransformation’ file from the same ‘services’ folder.