【发布时间】:2017-03-13 13:28:30
【问题描述】:
我有几行 Gradle 脚本和一些 Groovy 方法,我想将它们用作测试项目测试和配置方面的一些想法的基础。涉及的现有脚本需要使用 (Java) 构造:File(与 Gradle file() 方法相反)。
只要我向Properties.load() 调用提供完整的绝对文件路径,该脚本就完美地在命令行和Netbeans 中运行。
后来,当使用合法的相对路径和文件名运行构建(再次在 Netbeans 中)时,我的小函数无法找到该文件(根本!)。这可能会发生。但在这种情况下; Java File.getAbsolutePath() 方法在所有情况下都显示相同的字符串。工作和失败案例。我使用 Linux 准备了这个。结果是在 Windows 10 上生成的。
一定是有原因的。如果我能看到它在这一点上会发生什么,我会感到震惊。摇篮,
-
Working with Files 部分
- 表示相对文件路径正常。
- Java - Writing System Properties
- System.setProperty()
- 我自己对使用
user.dir有点怀疑 - 但是文档似乎支持它,自 Java 1.2 以来就一直存在 - 它不仅适用于 Gradle;在 Java 和 Groovy 中也是如此。
- 同
-Duser.dir=pathString
失败输出:
============ project ============
Gradle version: 3.4.1
Groovy version: 2.4.7
Java version: 1.8.0_121
Root project: 'try'
project dir: '/home/projects/sandbox/lab/gradle-lab/try-gradle'
cwd: '/home/projects/sandbox/lab/gradle-lab/try-gradle'
===================================
xxxxxxxxxxxxxxxxx[ loadProperties testing ]xxxx
cwd: '/home/projects/sandbox/lab/gradle-lab/try-gradle'
user.dir: '/home/projects/sandbox/lab/gradle-lab/try-gradle' ...
loading: 'common.properties' ...
loading: '/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties' ...
* No Such File: '/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties'.
xxxxxxxxxxxxxxxxx[ loadProperties end ]xxxx
工作输出:
xxxxxxxxxxxxxxxxx[ loadProperties testing ]xxxx
cwd: '/home/projects/sandbox/lab/gradle-lab/try-gradle'
user.dir: '/home/projects/sandbox/lab/gradle-lab/try-gradle' ...
loading: '/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties' ...
loading: '/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties' ...
'ext.zzz' => "ext.zzz"
'zzz' => "zzz"
xxxxxxxxxxxxxxxxx[ loadProperties end ]xxxx
Gradle build.gradle 脚本非常简单,只需几行即可报告运行时环境和软件版本等。
build.gradle脚本:
import org.gradle.api.artifacts.*
System.setProperty( "user.dir", project.rootDir.toString() )
/****
* try-s project
*****/
println "";
apply plugin: 'base' // To add "clean" task to the root project.
println "\n ============ project ============";
println "Gradle version: "+ gradle.gradleVersion;
println "Groovy version: "+ GroovySystem.version;
println "Java version: "+ System.getProperty("java.version");
println "";
println "Root project: '${project.rootProject.name}'";
// println " root dir: '${project.rootDir}'";
println " project dir: '${project.projectDir}'";
println " cwd: '${(new File(".")).getCanonicalPath()}'";
// println " user dir: '${System.getProperty("user.dir")}'";
println "";
println " ===================================\n";
println " xxxxxxxxxxxxxxxxx[ loadProperties testing ]xxxx"
println ""
// Does Not Work:
// File.exists() --> false
//
loadProperties( "common.properties" );
// Works - fully qualified file path:
// File.exists() --> true
// and then loads properties from the file
//
loadProperties( "/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties" );
// Works - using project.rootDir property
//
loadProperties( "${project.rootDir}/common.properties" );
// Works - using a path relative to the Netbeans start directory
// Only works when `System.user.dir` has the
// same value as the start directory
// Obviously the path must be relative to
// Netbeans start-up directory -- Likely
// to change for different locations, etc.
//
loadProperties( "../../../sandbox/lab/gradle-lab/try-gradle/common.properties" );
println ""
println " xxxxxxxxxxxxxxxxx[ loadProperties end ]xxxx"
loadProperties()函数:
private def loadProperties( String fileName )
{
File propFile = new File( fileName );
Properties fileProps = new Properties( );
// Check current directory
//
println " cwd: '${(new File(".")).getCanonicalPath()}'";
println " user.dir: '${System.getProperty("user.dir")}' ...";
println " loading: '${fileName}' ...";
println " loading: '${propFile.getCanonicalPath()}' ...";
println "";
if( propFile.exists() )
{
InputStream propStream = new FileInputStream( propFile ); // fileName );
fileProps.load( propStream );
fileProps.each { prop, val ->
println " '${prop}' => \"${val}\"";
}
}
else {
println " * No Such File: '${propFile.getAbsolutePath()}'."
}
} //loadProperties
很遗憾,没有 if( propFile.exists() ) 检查 Gradle 会报告异常:FileNotFoundException(很奇怪)。
查看 propFile.getAbsolutePath() 的输出以及完全限定的文件名字符串或 rootDir+"common.properties 版本的事件 -- 所有四种情况都显示 相同 文件路径名字符串:
'/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties'
对我来说,最重要的是,如何处理两个相同的文件路径和四个中的一个,JVM / Gradle 合作伙伴未找到一个有效的文件路径名。
ps。
我已经知道 Gradle 插件有一个关于不以项目目录作为当前目录开始的错误。通过选择(显然)。我用user.dir 设置解决了这个问题——不幸的是,评论该行对结果没有影响。只要路径正确。
【问题讨论】:
-
WRT
user.dir属性,我不相信这会以可信赖的方式更改 CWD...感觉这是 NetBeans 错误? -
@tim_yates ...是的,但是我所看到的一切都表明这是它的完成方式(从java 1.2开始)。与
Java ... -Duser.dir=pathString相同——确实有效。