- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试做一件看似相当简单的事情:使用 Gradle Copy
任务或方法显示我正在复制的文件的目标路径。
为此,我使用了 CopySpec.eachFile
方法,在该方法中我使用了 FileCopyDetails
来访问目标路径,正如我认为在 Gradle javadoc 中解释的那样。
这是一个使用Copy
任务的示例(与Copy
方法的行为相同):
task copyTaskUsingEachFile(type: Copy) {
from("$projectDir/resources/file-with-tokens.txt")
into('generated/subfolder')
filter(ReplaceTokens, tokens: [sampleProp: 'Hello sample property!'])
eachFile { println """ ---- content of FileCopyDetails object used in eachFile :
object toString = $it
name = $it.name
path = $it.path
relativePath = $it.relativePath
sourceName = $it.sourceName
sourcePath = $it.sourcePath
"""
}
}
问题是:eachFile
中使用的基础FileCopyDetails
对象的每个属性都评估为相同的值:
:copyTaskUsingEachFile
---- content of FileCopyDetails object used in eachFile :
object toString = file 'C:\<some path>\resources\file-with-tokens.txt'
name = file-with-tokens.txt
path = file-with-tokens.txt
relativePath = file-with-tokens.txt
sourceName = file-with-tokens.txt
sourcePath = file-with-tokens.txt
我希望 Gradle 显示:
sourceName = resources/file-with-tokens.txt
path = generated/subfolder/file-with-tokens.txt
仔细阅读 FileCopyDetails
文档后:
Provides details about a file or directory about to be copied, and allows some aspects of the destination file to be modified
我开始认为,在 eachFile
中打印此对象时,“最终”目标路径尚未设置。
我尝试使用 child CopySpec,使用 from (...) {...}
语法,猜测它可能会影响评估时间:
task copyTaskUsingEachFileAndChildCopySpec(type: Copy) {
into 'generated'
from("$projectDir/resources/file-with-tokens.txt") {
into('subfolder')
}
filter(ReplaceTokens, tokens: [sampleProp: 'Hello sample property!'])
eachFile { println """ ---- content of FileCopyDetails object used in eachFile :
object toString = $it
name = $it.name
path = $it.path
relativePath = $it.relativePath
sourceName = $it.sourceName
sourcePath = $it.sourcePath
"""
}
}
这次我得到以下结果:
:copyTaskUsingEachFileAndChildCopySpec
---- content of FileCopyDetails object used in eachFile :
object toString = file 'C:\<some path>\resources\file-with-tokens.txt'
name = file-with-tokens.txt
path = subfolder/file-with-tokens.txt
relativePath = subfolder/file-with-tokens.txt
sourceName = file-with-tokens.txt
sourcePath = file-with-tokens.txt
因此,所有目标路径都显示有它们的子 CopySpec 值,而不是从父 CopySpec 继承的部分(在当前情况下为 generated
)。
所以我有两个问题:
CopySpec.eachFile
中解析 FileCopyDetails
吗?Copy
任务(或方法)复制的文件的完整目标路径吗?任何帮助或部分解释将不胜感激。
更新
使用 Vampire answer ,我知道在 CopySpec.eachFile
中成功打印了我复制的文件的完整目标路径。为此,我使用了 Copy.destinationDir
字段。
task copyMultipleFilesDisplayTargetFolder(type: Copy) {
printTask 'copyMultipleFilesDisplayTargetFolder'
into 'generated'
filter(ReplaceTokens, tokens: [sampleProp: 'Hello sample property!'])
/* Here, we print the complete destination path of the copied files, BUT,
I don't know how to print their complete SOURCE path.
*/
eachFile {
println "file $it.name copied to $destinationDir/$it.path"
println ''
}
from("$projectDir/resources/file-with-tokens.txt") {
into('subfolder')
}
from("$projectDir/resources2/file-with-tokens-2.txt") {
into('subfolder2')
}
from("$projectDir/resources3/file-with-tokens-3.txt") {
into('subfolder3')
}
}
给出:
------------ TASK ------------ copyMultipleFilesDisplayTargetFolder
:copyMultipleFilesDisplayTargetFolder
file file-with-tokens.txt copied to C:\<some path>\generated/subfolder/file-with-tokens.txt
file file-with-tokens-2.txt copied to C:\<some path>\generated/subfolder2/file-with-tokens-2.txt
file file-with-tokens-3.txt copied to C:\<some path>\generated/subfolder3/file-with-tokens-3.txt
现在唯一缺少的是能够以简单的方式打印复制文件的完整源路径。
“简单方法” 我的意思是,例如,在单个 eachFile
中,所有子 CopySpec
都将继承(如上)。
目前,我可以使用 Copy.source
字段和 FileCollection.getAsPath
方法来显示它,但是要集成到 eachFile
中并不容易。
更新 2
Vampire 又说对了,你可以使用 FileCopyDetails.getFile()
来访问代表源的 File
对象,它允许你显示 完整的源码路径:
task copyMultipleFilesDisplayTargetFolder(type: Copy) {
printTask 'copyMultipleFilesDisplayTargetFolder'
into 'generated'
//filter(ReplaceTokens, tokens: [sampleProp: 'Hello sample property!'])
eachFile {
println "file $it.name copied to $destinationDir/$it.path"
/* !!!! BEWARE !!!!
You can't use the below FileCopyDetails.getFile() when also using a filter operation.
Check https://discuss.gradle.org/t/combining-eachfile-filter-results-in-failure/14801/2
*/
println "Initial source path: ${it.getFile().getCanonicalPath()}"
println ''
}
from("$projectDir/resources/file-with-tokens.txt") {
into('subfolder')
}
from("$projectDir/resources2/file-with-tokens-2.txt") {
into('subfolder2')
}
from("$projectDir/resources3/file-with-tokens-3.txt") {
into('subfolder3')
}
}
但是,您不能在使用filter
操作时使用FileCopyDetails.getFile()
!
这是一个神秘的 Gradle 错误,在这里解释:https://discuss.gradle.org/t/combining-eachfile-filter-results-in-failure/14801/2
查看 Gradle 源代码时,DefaultFileCopyDetails
类包含以下代码:
public File getFile() {
if (filterChain.hasFilters()) {
throw new UnsupportedOperationException();
} else {
return fileDetails.getFile();
}
}
不幸的是,这解释了这个问题。
无论如何,在任何其他情况下,我的 2 个初始问题都有新的答案,谢谢!
更新 3(最终版)
再次感谢 Vampire(他专门针对这个问题打开了一个 Gradle 案例),他为我们提供了针对前一个 filter
/getFile
bug 的解决方案。
有关详细信息,请参阅他的 https://github.com/gradle/gradle/issues/1588,但是,简而言之,为避免此问题,在 same CopySpec 中,您只需调用 filter
< strong>AFTER 调用 eachFile
。
我确认它正在运行。
最佳答案
如果您更深入地阅读 JavaDoc,您会在 getPath()
和 getRelativePath()
的第一句中找到解释,即 Returns此文件的路径,相对于复制目标的根目录。
。
在您的第一个示例中,您调用 eachFile
方法的 CopySpec
的复制目标是 generated/subfolder
,因此相对路径目标文件是 file-with-tokens.txt
。
在您的第二个示例中,您调用 eachFile
方法的 CopySpec
的复制目标是生成的
,因此目标的相对路径文件是 subfolder/file-with-tokens.txt
。
将 CopySpec
的复制目标与相对路径连接起来,您将获得目标文件的完整路径。
如果你G。在您的 eachFile
调用之前添加一个 rename
调用,例如 rename { 'foo-' + it }
,然后您会看到相对路径将变为foo-file-with-tokens.txt
而源路径仍然是 file-with-tokens.txt
。
如果您需要文件的源路径,您可以简单地使用FileCopyDetails
的file
属性。每个 FileCopyDetails
也是一个 FileTreeElement
(它的父类(super class)),因此有一个指向源文件的文件属性(File
对象)。
请注意,过滤器
可能会受到干扰。如果您在同一复制规范中且在 eachFile
之前有一个 filter
,或者在您执行的复制规范的父复制规范中有一个 filter
eachFile
,无论是之前还是之后,你都会得到一个UnsupportedOperationException
。如果您在同一复制规范中的 eachFile
之后执行 filter
,它会起作用。参见 https://github.com/gradle/gradle/issues/1588了解更多信息。
关于gradle - Gradle CopySpec.eachFile 中 FileCopyDetails 的评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42670607/
在 spring-security项目,在 build.gradle文件,以下插件可用: apply plugin: 'io.spring.nohttp' apply plugin: 'locks'
我正在尝试使用gradle任务作为“与操作系统无关的shell脚本”。我在编写构建文件的逻辑时没有问题,但是我希望能够从任何文件夹中运行它。 我的第一次尝试是在路径上放置一个带有build.gradl
我在同一目录下有三个具有不同名称的build.gradle dev.build.gradle uat.build.gradle prd.build.gradle 我有4期 “gradle build”
乡亲们 有谁知道如何使用gradle与自定义的build.gradle和settings.gradle一起运行-即两者在一起? 我认为我可以有一个自定义的build.gradle文件(-b标志)-但这
Gradle 配置有两个属性,称为 dependencies和 incoming .根据我的理解,我们可以通过 Gradle 依赖 block 向配置添加依赖,如下所示: dependencies {
我正在测试将我的工件推送到 Nexus Maven 存储库。我正在使用 gradle这样做。 gradle upload有什么区别和 gradle publish ? 最佳答案 目前gradle提供了
这里有一个类似的问题:Gradle properties not being read from ~/.gradle/gradle.properties,但它不能解决我的问题。 在我看来,gradle
如何检索 Gradle 本身的版本 以编程方式从 Gradle 插件中? 最佳答案 刚刚发现可以使用任何一种方法来解决它 getProject().getGradle().getGradleVersi
2 个 gradle 项目,A 和 B,其中 A 定义了对 B 的编译依赖。为 maven 和 gradle 项目启用了 JAR 的重新映射。我试过启用和禁用自定义工具模型。我在 init.d 目录下
如何从 gradle 运行另一个 gradle 脚本。我有多个 gradle 脚本可以在 /test 下运行测试目录。 /build.gradle使用要运行的测试 gradle 文件的名称调用。例如
我已经看到了多模块项目的不同布局,其中有些项目只有一个 build.gradle。而其他项目将有 build.gradle在根和 build.gradle在每个子项目中。我想知道有什么区别以及首选哪种
我对 Gradle 生命周期有一个基本的询问。我的项目是一个多模块项目。 我注意到以下命令: ./gradlew clean bignibou-server:run 不等于运行以下两个命令按顺序运行:
我有一个项目,其中包含一个子项目,如下所示: Root Project |----gradle.build |----SubProject |----|----gradle.build 这里的子项目包
我有 gradle 项目,我想向不使用 gradle 或 maven 等构建工具的项目添加依赖项。 目录结构如下: commonfolder\ gradleProject\ b
我想创建一个类来帮助我加载不同类型的属性(local.properties,gradle.properties,$GRADLE_HOME/gradle.properties,环境变量,系统属性和自定义
我有一个多模块项目,其目录结构如下: proj |-modA |-modB |-modMain \-modSysTest 依赖项是: modB -> modA modMain -> modB modM
我正在尝试创建一个可以在 --offline 模式下工作的 gradle 项目的副本。除了一个步骤,我已经自动化了所有步骤。我无法自动将插件 jar 下载到 gradle 缓存中。 我的离线分发通过指
基本上我想这样做: Use Gradle function from other gradle file 也就是说,从另一个 gradle 脚本中调用一个函数。问题是我的 build.gradle 在
我有一个简单的 gradle 插件,我想试用一下,但我不确定如何“请求”gradle 运行我的测试 这是我要修改的 block (在我的 gradle 构建文件的顶部) buildscript {
在 Android Studio 中创建项目时,我收到以下错误消息。 Failed to import new Gradle project: Could not install Gradle dis
我是一名优秀的程序员,十分优秀!