在 Java Gradle 项目中设置资源文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41315434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:17:23  来源:igfitidea点击:

Setting the Resource Folder in a Java Gradle project

javagradlebuild.gradle

提问by Chris Tei

I am trying to set up a Gradle Java project, but for some reason I can't seem to register the resources folder with the build. Whenever I try to write to a file, it writes to the project root and not the resources folder.

我正在尝试设置一个 Gradle Java 项目,但由于某种原因,我似乎无法在构建中注册资源文件夹。每当我尝试写入文件时,它都会写入项目根目录而不是资源文件夹。

My project directory is as follows:

我的项目目录如下:

  • src
    • main
      • java
      • resources
  • 源文件
    • 主要的
      • 爪哇
      • 资源

Here is my build.gradle file

这是我的 build.gradle 文件

apply plugin: 'java'
apply plugin: 'application'

repositories {
        mavenCentral()
}

mainClassName='Test'


sourceSets {
        main {
                java {
                        srcDirs= ["src/main/java"]
                }
                resources {
                        srcDirs= ["src/main/resources"]
                }
        }
}

And here is my Test.java that I'm using to see if I can access the resources folder

这是我的 Test.java,我用它来查看是否可以访问资源文件夹

import java.io.*;

public class Test {

        public static void main(String[] args) throws Exception {

                FileWriter writer = new FileWriter(new File("Test.txt"));
                writer.write("Test");
                writer.close();

        }

}

回答by Jose Martinez

Your current directory can be retrieved from the system property user.dir. E.g. System.getProperty("user.dir").

可以从系统属性中检索当前目录user.dir。例如System.getProperty("user.dir")

The resources directory is not the same as the working directory, which is the directory that your application executed from. The resource directory is used mainly for storing files that were created before runtime, so that they can be accessed during runtime. It is not meant to be written to during runtime. So that's why when you set the resource path in Gradle it is not going to set your current working directory to the resource path.

资源目录与工作目录不同,工作目录是您的应用程序从中执行的目录。资源目录主要用于存放运行前创建的文件,以便运行时访问。它不打算在运行时写入。这就是为什么当您在 Gradle 中设置资源路径时,它不会将您当前的工作目录设置为资源路径。

You can still do what you want to do, but you will have to write to ./src/main/resources/Test.txt. I know that sometimes while you are developing your application you would want to be able to write to the resource folder. But just note that when the application is running in the wild, it is not expected that you will be writing to the resource folder, especially since more often than not it will be inside a JAR file.

你仍然可以做你想做的事,但你必须写信给./src/main/resources/Test.txt. 我知道有时在开发应用程序时,您可能希望能够写入资源文件夹。但请注意,当应用程序在野外运行时,您不会写入资源文件夹,尤其是因为它通常位于 JAR 文件中。