java - 怎么会找不到完整文件路径的文件呢?

标签 java gradle groovy

我有几行 Gradle 脚本和一些 Groovy 方法,我想将其用作测试有关项目测试和配置的一些想法的基础。涉及的现有脚本需要使用 (Java) 构造:File(与 Gradle file() 方法相反)。

只要我向 Properties.load() 调用提供完整的绝对文件路径,该脚本就可以在命令行和 Netbeans 中完美运行。

后来,当使用合法的相对路径和文件名运行构建(再次在 Netbeans 中)时,我的小函数无法找到该文件(根本!)。这可能会发生。但在这种情况下; Java File.getAbsolutePath() 方法在所有情况下都显示相同的字符串。工作和失败案例。我使用 Linux 准备了这个。结果在 Windows 10 上生成。

一定有原因。如果我现在能看到它会发生什么,我会很震惊。 Gradle ,

  • Working with Files 部分
    • 表示相对文件路径没问题。
  • Java - Writing System Properties
    • System.setProperty()
    • 我自己对使用 user.dir 有点怀疑 - 然而文档似乎支持它,从 Java 1.2 开始就存在
    • 它不仅适用于 Gradle;还适用于 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 设置修复了这个问题 - 不幸的是,注释该行对结果没有任何影响。只要路径正确。

最佳答案

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

构建.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

关于java - 怎么会找不到完整文件路径的文件呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42764854/

相关文章:

java - org.apache.jasper.JasperException : Unable to compile class for JSP encountered when using spring

java - 无法确定任务 ':app:compileDebugJavaWithJavac' 的依赖关系。在 Cordova 有问题

java - 使用Gradle的AWS Dev env设置

java - jeditor Pane 的打印

java - Android:以编程方式设置方向需要时间,直到布局实际发生变化

java - Spring - 如何正确映射 Controller ?

unit-testing - Gradle TestNG结果输出

unit-testing - Grails-操作方法-使用 Controller 参数的单元测试服务

groovy - 如何在 Groovy 中整齐地打印新行?

groovy - 在 gradle 项目中使用 archivesBaseName 没有效果