java - 如何确定Gradle中的源目录优先级?

标签 java gradle

我的build.gradle中有以下片段

 main {
        resources.srcDir "src/main/resources-$env"
    }

文件夹
src/main/resources

默认情况下为源。

我想实现src/main/resources-$envresources具有更高的优先级。

现在,如果我有一个名为config.properties的文件

在一些源代码中,我写了:
Properties.class.getClassLoader().getResourceAsStream('config.properties');

路径
src/main/resources/config.properties

具有更高的优先级。
  • 是否保证此优先级?
  • 是否可以更改此行为?
  • 最佳答案

    您可能会这样做:

    main {
        // important to use '=' so you set rather than add
        resources.srcDirs = ["src/main/resources-$env", "src/main/resources"]
    }
    

    或者您可能希望将资源合并到一个文件夹中
    task mergeResources(type: Copy) {
        def envResources = fileTree("src/main/resources-$env")
        def defaultResources = fileTree("src/main/resources").minus(envResources)
    
        from envResources
        from defaultResources
        into "$buildDir/mergedResources"
    }
    
    main {
        resources.srcDirs = ["$buildDir/mergedResources"]
    }
    
    // wire the task into the DAG
    compile.dependsOn mergeResources
    

    关于java - 如何确定Gradle中的源目录优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765997/

    相关文章:

    在 Gradle 上创建新项目时 Android Studio 卡住了 : Configure Project or Gradle: Resolve Dependencies ':classpath:'

    android - Gradle 突然不再构建 Arm8Release

    android - Gradle 无法使用 JCenter 构建(使用 Unity + Firebase)

    java - JPA - 外键的 ElementCollection

    Java:向 ChangeEvent 添加文本字段

    java - Xjc 在 xmlns :xsi and xsi:noNamespaceSchemaLocation 的 XSD 属性定义上抛出 "Invalid attribute value, not a valid NCName"

    android-studio - 为 Android Studio gradle 添加 maven 依赖

    gradle - 我如何使用 cmd 或 gitbash 对 gradle 项目进行简单刷新

    java - 使用 Spring Boot : Could not resolve placeholder 进行 Maven 资源过滤

    java - 新的 ObjectInputStream() block