[英]Include a file from a specific source folder in Gradle
I have a java project which I want to build in gradle and the source is in the following structure,我有一个想要在 gradle 中构建的 java 项目,源代码如下所示,
src
|
+-main
| |
| +-java
| |
| +-package
| |
| +- Utils.java
| +- Other.java
| +- Files.java
| +- Folders.java
+-awesome
|
+-java
|
+-package
|
+- Utils.java
Now, I would like to build the files from the sourceSet src/main/java
but including the Utils.java
in src/awesome/java
instead of from the main
sourceset.现在,我想从 sourceSet
src/main/java
构建文件,但在src/awesome/java
包含Utils.java
而不是来自main
。
I have tried the following options (unsuccessfully):我尝试了以下选项(未成功):
1. Giving the full file path in exclude
: 1. 在
exclude
给出完整的文件路径:
sourceSets {
main {
java {
srcDir "src/awesome/java"
exclude "${projectDir}/src/main/java/package/Utils.java"
}
}
}
Output: error: duplicate class package.Utils
输出:错误:重复的类 package.Utils
Looks like giving the full file path does not work in the exclude pattern.看起来提供完整的文件路径在排除模式中不起作用。
2. Using 2 java closures: 2. 使用 2 个 java 闭包:
sourceSets {
main {
java {
exclude "package/Utils.java"
}
java {
srcDir "src/awesome/java"
}
}
}
But this removes the Utils.java
class from both the source locations.但这会从两个源位置中删除
Utils.java
类。
Is it possible to provide a single source file from another location in gradle?是否可以从 gradle 中的另一个位置提供单个源文件?
Note: I cannot compile the Utils.java
class alone since it has dependencies with the other classes.注意:我不能单独编译
Utils.java
类,因为它与其他类有依赖关系。 (I know it's bad, no need to rub it in). (我知道这很糟糕,不需要揉搓)。
Also copying the file to the main/java/package
location is not possible since it could pollute the SCM.也无法将文件复制到
main/java/package
位置,因为它可能会污染 SCM。
I suggest you insert a task into the DAG to merge the sources and build the merged folder我建议你在 DAG 中插入一个任务来合并源并构建合并的文件夹
Eg:例如:
sourceSets.main.java {
srcDirs = ["$buildDir/mergedSources"]
}
task mergeSources(Type:Copy) {
from 'src/awesome/java'
from 'src/main/java'
duplicatesStrategy = DuplicatesStrategy.EXCLUDE // (see https://docs.gradle.org/current/javadoc/org/gradle/api/file/DuplicatesStrategy.html#EXCLUDE)
into "$buildDir/mergedSources"
}
compileJava.dependsOn mergeSources
Also copying the file to the main/java/package location is not possible since it could pollute the SCM.
也无法将文件复制到 main/java/package 位置,因为它可能会污染 SCM。
Assuming you are already excluding $buildDir
from SCM this won't 'pollute' SCM假设您已经从 SCM 中排除了
$buildDir
,这不会“污染”SCM
It looks like we can exclude specific files using the exclude(Closure)
or exclude(Spec)
methods instead of passing Strings.看起来我们可以使用
exclude(Closure)
或exclude(Spec)
方法而不是传递字符串来排除特定文件。
Here's how I achieved it,这是我如何实现它,
sourceSets {
main {
java {
srcDir "src/awesome/java"
exclude { fileTreeElement ->
fileTreeElement.file.equals("${projectDir}/src/main/java/package/Utils.java" as File)
}
}
}
}
This way, it includes the Utils.java
from awesome subfolder and excludes the Utils.java
from main subfolder.通过这种方式,它包括
Utils.java
从真棒子文件夹和排除Utils.java
从主要子文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.