2017-03-13 22 views
1

我有幾行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設置解決了這個問題 - 不幸的是,評論該行對結果沒有影響。只要路徑是正確的。

+0

WRT的'user.dir'財產,我不相信這改變了CWD以值得信賴的方式...感覺像這是一個NetBeans錯誤? –

+0

@tim_yates ...是的,但是我看到的所有東西都是這樣的(從java 1.2開始)。與Java一樣...... -Duser.dir ='*'pathString' * - 哪個工作正常。 – will

回答

0

非常令人驚訝。這是工作。

的build.gradle:

System.setProperty("user.dir", project.rootDir.toString()) 

apply plugin: 'base' // To add "clean" task to the root project. 
println " user dir:   ${System.getProperty('user.dir')}" 
loadProperties('common.properties') 


println "ends" 
def loadProperties(file){ 
    def properties = new Properties() 
    def propertiesFile = new File(file) 
    if(propertiesFile.exists()) println 'file exists' 
    propertiesFile.withInputStream { 
     properties.load(it) 
    } 
    println properties 
} 

輸出被視爲有望

$ gradle 
Starting a Gradle Daemon (subsequent builds will be faster) 
user dir:   /home/rsettine/Documents/gradle 
file exists 
{abc=1, test=1} 
ends 
:help 

Welcome to Gradle 3.3. 

To run a build, run gradle <task> ... 

To see a list of available tasks, run gradle tasks 

To see a list of command-line options, run gradle --help 

To see more detail about a task, run gradle help --task <task> 

BUILD SUCCESSFUL 

Total time: 8.263 secs 
+0

謝謝@Rao ...我在那裏感覺有點沮喪。它不適用於Windows或Linux。然後...我再次查看了你的輸出 - 你正在命令行(shell)而不是** Netbeans **上運行。 – will

+0

好的。你確認它在你的機器上從命令行工作嗎? – Rao

+0

哦,是的 - 從bash和windows cmd的作品。看來Netbeqans gradle與Gradle deamon溝通,所以它必須運行在一個稍微不同的環境中。我沒有*如何*相同的文件路徑字符串可以給出不同的結果.... will