Coder
Coder

Reputation: 490

How to use something similar to fileset and pathconvert of ant in Gradle

I am trying to convert an Ant project to Gradle. One target is executing a Java Main class. And list of java files are passed into the Main class as argument.

Ant Code :

<target name="someThing">
        <fileset dir="src/main/java" id="input.files">
            <include name="**/api/SomeApi.java"/>
            <include name="**/temporaryuserdata/api/Some2API.java"/>
        </fileset>
        <pathconvert pathsep=" " property="javaFiles" refid="input.files"/>
        <java classname="com.some.tool.MainClass" classpathref="buildtools.classpath" fork="true" failonerror="true">
            <arg value="${javaFiles}"/>
        </java>
    </target>

I converted it to gradle using the ant support in gradle in following way: Gradle Code:

task someThing(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    main = 'com.some.tool.MainClass'
    ant.fileset(dir : '', id: 'input.files'){
        include(name : "**/api/SomeApi.java")
        include(name : "**/api/Some2API.java")
    }
    ant.pathconvert(pathsep : " ", refid: "input.files", property: "javaFiles")
    args = [ant.properties["javaFiles"]]
}

It works as expected. However, I don't want to use the ant things in gradle.

Can somebody help me get the same output of fileset and pathconvert using the proper gradle and groovy tools?

Upvotes: 0

Views: 1508

Answers (2)

lance-java
lance-java

Reputation: 27996

You could do this

FileTree files = fileTree('src/main/java').matching {
    include '**/api/SomeApi.java' 
    include '**/api/SomeApi2.java' 
} 
args = [files.asPath]

But really you should use the JavaCompile task for this rather than JavaExec

Upvotes: 1

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3016

Gradle has great api to work with files:

https://docs.gradle.org/current/userguide/working_with_files.html

For example you can use fileTree:

FileTree tree = fileTree(dir: 'src', includes: ['**/api/SomeApi.java', '**/api/Some2API.java'])

Upvotes: 1

Related Questions