[英]Gradle project and resources folder and logical root for creating files
I have a problem understanding how Intellij is working with a Gradle project and the resources folder.我在理解 Intellij 如何处理 Gradle 项目和资源文件夹时遇到问题。
I have created a default Gradle project its created a module 'group', and a module when looking in the module group the src/main/resources folder shows as a 'resource folder' (however it doesn't in the stand-alone module, where groovy/java/resources are all grey).我创建了一个默认的 Gradle 项目,它创建了一个模块“组”,当查看模块组时,一个模块 src/main/resources 文件夹显示为“资源文件夹”(但它不在独立模块中) ,其中 groovy/java/resources 都是灰色的)。
So that sort out seems to work when compiling code generally.因此,在编译代码时,这种排序似乎可以正常工作。
I tried however to create a file in Groovy script like this然而,我尝试在这样的 Groovy 脚本中创建一个文件
File newFile = new File ("resources/temp.txt")
def fpath = newFile.toURL()
if (!newFile.exists()) {
println "creating new $fpath file "
newFile.createNewFile()
}
However run you run this it fails at bit like this但是运行你运行它它有点像这样失败
creating new file:/D:/Intellij - Azure/quickstart-java/graph/src/main/groovy/playpen/resources/temp.txt file
Caught: java.io.IOException: The system cannot find the path specified
java.io.IOException: The system cannot find the path specified
at java_io_File$createNewFile$1.call(Unknown Source)
at playpen.TinkerPop-Example.run(TinkerPop-Example.groovy:47)
The File seems to have relative root .../src/main/groovy/playpen
which is where my script is.该文件似乎具有相对根.../src/main/groovy/playpen
,这是我的脚本所在的位置。 there is no src/main/groovy/playpen/resources/
so it fails没有src/main/groovy/playpen/resources/
所以它失败了
if a use File("/resources/temp.txt") and look at the URL it shows as
D:\\resources\\temp.txt` defaulting to same drive as where the script is defined.如果使用File("/resources/temp.txt") and look at the URL it shows as
D:\\resources\\temp.txt` File("/resources/temp.txt") and look at the URL it shows as
默认为与定义脚本的驱动器相同的驱动器。
If you remove the resources prefix - the file gets created in playpen - again assumed root is same as the source program script.如果您删除资源前缀 - 该文件将在 playpen 中创建 - 再次假定 root 与源程序脚本相同。
What I want is to read a file from the 'resources' folder but unless I go to absolute file paths it just ignores the 'resources' folder and only looks in the Groovy source folders.我想要的是从“资源”文件夹中读取文件,但除非我转到绝对文件路径,否则它只会忽略“资源”文件夹,只查看 Groovy 源文件夹。
So for example if I copy the temp.txt into the resources
folder and run this例如,如果我将 temp.txt 复制到resources
文件夹中并运行它
File newFile = new File ("temp.txt")
def fpath = newFile.toURL()
if (!newFile.exists()) {
println "creating new $fpath file "
newFile.createNewFile()
} else {
println "reading file at $fpath"
}
it just creates a new temp.txt in the playpen package where the script runs and doesn't see a copy from 'resources' folder.它只是在脚本运行的 playpen 包中创建一个新的 temp.txt,并且看不到“资源”文件夹中的副本。
So what format of 'file name' do I use so that the 'resources' folder is naturally used to resolve file names - without having to use absolute file names?那么我使用什么格式的“文件名”,以便“资源”文件夹自然地用于解析文件名——而不必使用绝对文件名?
Equally if want to create a File programmatically and save that in the 'resources' folder where the script runs from src/main/groovy/playpen
, what's the path name that puts it in the correct location.同样,如果想以编程方式创建一个文件并将其保存在脚本从src/main/groovy/playpen
运行的“资源”文件夹中,那么将它放在正确位置的路径名是什么。
I'm missing something basic here and can't figure out how to read/or write from the resources folder.我在这里缺少一些基本的东西,无法弄清楚如何从资源文件夹中读取/或写入。
ended up with brute force and ignorance on this one - someone may have a 'smarter'answer, but this one appears to be working.最终以蛮力和无知来解决这个问题 - 有人可能有一个“更聪明”的答案,但这个答案似乎有效。 Slightly tweaked some code i got working.稍微调整了一些我开始工作的代码。
I'm using groovy here rather than java (just less boilerplate noise), and nice File groovy methods我在这里使用 groovy 而不是 java(只是更少的样板噪音),以及漂亮的 File groovy 方法
steps - (1)first locate root of your IDE/project using System.getProperty("user.dir") (2) get the current resource file for 'this' - the class you run from ide (3) see if the resource is in $root/out/test/.. dir - it so its a test else its your normal code.步骤 - (1) 首先使用 System.getProperty("user.dir") 定位您的 IDE/项目的根目录 (2) 获取“this”的当前资源文件 - 您从 ide 运行的类 (3) 查看资源在 $root/out/test/.. 目录中 - 所以它是一个测试,否则它是你的正常代码。 (4) set resourcePath now to correct 'location' either in src/main/resources or src/test/resources/ - then build rest of file from this stub (5) check if file exists - if so delete and rewrite this (you can make this cleverer) (6) create file and fill its contents (4) 现在将 resourcePath 设置为更正 src/main/resources 或 src/test/resources/ 中的“位置” - 然后从此存根构建文件的其余部分 (5) 检查文件是否存在 - 如果存在,则删除并重写它(您可以使这个更聪明)(6)创建文件并填充其内容
job done this file should now appear where you expect it. job done 这个文件现在应该出现在你期望的地方。 Happy to take cleverer ways to get do this - but for anyone else stuck this seems to do the trick很高兴采取更聪明的方法来做到这一点 - 但对于其他人来说,这似乎可以解决问题
void exportToFile (dir=null, fileName=null) {
boolean isTest = false
String root = System.getProperty("user.dir")
URL url = this.getClass().getResource("/")
File loc = new File(url.toURI())
String canPath = loc.getCanonicalPath()
String stem = "$root${File.separatorChar}out${File.separatorChar}test"
if (canPath.contains(stem))
isTest = true
String resourcesPath
if (isTest)
resourcesPath = "$root${File.separatorChar}src${File.separatorChar}test${File.separatorChar}resources"
else
resourcesPath = "$root${File.separatorChar}src${File.separatorChar}main${File.separatorChar}resources"
String procDir = dir ?: "some-dir-string"
if (procDir.endsWith("$File.separatorChar"))
procDir = procDir - "$File.separatorChar"
String procFileName = fileName ?: "somedefaultname"
String completeFileName
File exportFile = "$resourcesPath${File.separatorChar}$procDir${File.separatorChar}${procFileName}"
exportFile = new File(completeFileName)
println "path: $procDir, file:$procFileName, full: $completeFileName"
exportFile.with {
if (exists())
delete()
createNewFile()
text = toString()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.