我有几行Gradle脚本和一些Groovy方法,我想用它作为测试项目测试和配置的一些想法的基础。涉及需要使用(Java)构造的现有脚本:文件(与Gradle file()
方法相反)。
只要我为Properties.load()
调用提供完整的绝对文件路径,脚本就会在命令行和Netbeans中运行 。
后来,当使用合法的相对路径和文件名运行build(再次在Netbeans中)时,我的小函数无法找到该文件( at all !)。那可能发生。但在这种情况下; Java File.getAbsolutePath()
方法在所有情况下都显示相同的字符串。工作和失败案例。我用Linux编写了这个。结果在Windows 10上生成。
必须有一个理由。如果我能在这一点上看到它可能是什么,我就被打破了。 Gradle,
user.dir
有点怀疑 - 但是文档似乎支持它,自Java 1.2以来一直存在-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
版本 - 所有四种情况都显示 < em>相同的 文件路径名字符串:
&#39; /home/projects/sandbox/lab/gradle-lab/try-gradle/common.properties'
对我来说,底线是,如何处理两个相同的文件路径和四个中的一个,只有一个有效的文件路径名是JVM / Gradle伙伴关系找不到的。
PS。
我已经知道Gradle插件有一个关于不以项目dir作为当前目录的错误。通过选择(显然)。我使用user.dir
设置修复此问题 - 很遗憾,评论该行对结果没有任何影响。只要路径是正确的。
答案 0 :(得分: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