【问题标题】:How can it be that a file with full file path Can Not be found?怎么会找不到具有完整文件路径的文件?
【发布时间】: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 相同——确实有效。

标签: java gradle groovy


【解决方案1】:

相当令人惊讶。它正在工作。

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

【讨论】:

  • 谢谢@Rao ...我感觉有点困惑。它不适用于我的 Windows 或 Linux。那么...我再次查看了您的输出——您正在命令行(shell)上运行,而不是来自 Netbeans。
  • 好的。你确认它在你的机器上通过命令行运行吗?
  • 哦,是的——适用于 bash 和 windows cmd。 Netbeqans gradle 似乎与 Gradle 守护进程通信,因此它必须在稍微不同的环境中运行。我不明白 如何 相同的文件路径字符串会产生不同的结果....
  • 尝试从 common.properties 更改为 ./common.properties 看看是否有帮助?
  • 感谢 Rao -- 实际上,这并没有解决问题的重点。两个相同的文件名可以给出不同的结果。在我所知道的每个操作系统下,如果它就在磁盘上,那么它必须是同一个文件,并且没有被触及(它就是这样)。当我们在命令控制台上运行时。发生了非常奇怪的事情。
猜你喜欢
  • 2014-09-26
  • 1970-01-01
  • 2011-02-02
  • 2010-09-16
  • 2021-04-24
  • 2011-07-14
  • 2014-06-28
  • 2014-12-21
相关资源
最近更新 更多