简体   繁体   中英

SourceDirectorySet of a single file

I'm trying to create a custom gradle source set consisting of a set of files (no necessarily located in the same directory). Each file may be located in a directory that contains other files that are not supposed to be part of the source directory set.

How to create an instance of a SourceDirectorySet representing a single file?

I'd like to use such instances to configure java sources using the source method:

sourceSets {
    custom {
        java {
            source singleFileSourceDirectorySet
            source singleFileSourceDirectorySet2
            // ...
        }
    }
}

Create a SourceDirectorySet for parent directory of each file (line 6). Attach a filter that accepts only the selected file (line 7).

sourceSets {
    custom {
        java {
            final java.nio.file.Path srcPath = rootDir.toPath().resolve('path/to/a/File.java')
            final SourceDirectorySet sds = getObjects().sourceDirectorySet("name", "desc")
            sds.srcDir(srcPath.getParent().toFile())
            sds.filter { java.nio.file.Files.isSameFile(it.toPath(), srcPath) }
            source sds
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM