我有幾行Gradle腳本和一些Groovy方法,我想用它作爲測試圍繞項目測試和配置的一些想法的基礎。涉及需要使用(Java)構造的現有腳本:文件(與Gradle file()
方法相反)。怎麼可能找不到具有完整文件路徑的文件?
腳本運行完美在命令行和Netbeans只要我提供完整的絕對文件路徑到Properties.load()
調用。
後來,當使用合法的相對路徑和文件名運行構建(再次在Netbeans中)時,我的小函數無法找到文件(,全部爲!)。這可能發生。但在這種情況下, Java File.getAbsolutePath()
方法在所有情況下顯示相同的字符串。工作和失敗的情況。我準備使用Linux。結果在Windows 10上生成。
必須有一個原因。如果我能看到它現在可能會發生什麼,我會受到打擊。的搖籃,
- Working with Files部分
- 說相對文件路徑確定。
- 爪哇 - Writing System Properties
- System.setProperty()
- 我自己是如何使用
user.dir
有點可疑 - 但是,文檔似乎將它備份起來,因爲Java 1.2的 - 在那裏它的工作原理不僅與搖籃;在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())
檢查報告搖籃的excepton:FileNotFoundException
(奇怪的是)。
看的propFile.getAbsolutePath()
和輸出完全合格的文件名字符串或rootDir+"common.properties
版本的事件 - 所有四種情況顯示相同文件路徑名字符串:
'/home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties'
對我來說,底線是,怎麼可能兩個IDE經過處理的文件路徑和四個中的一個,只有一個有效的文件路徑名是JVM/Gradle夥伴關係找不到的。
ps。
我已經知道Gradle插件有一個錯誤,涉及不以項目目錄作爲當前目錄開始。由選擇(顯然)。我用user.dir
設置解決了這個問題 - 不幸的是,評論該行對結果沒有影響。只要路徑是正確的。
WRT的'user.dir'財產,我不相信這改變了CWD以值得信賴的方式...感覺像這是一個NetBeans錯誤? –
@tim_yates ...是的,但是我看到的所有東西都是這樣的(從java 1.2開始)。與Java一樣...... -Duser.dir ='*'pathString' * - 哪個工作正常。 – will