Java 在Gradle生成中替换文件

Java 在Gradle生成中替换文件,java,groovy,gradle,build,Java,Groovy,Gradle,Build,我试图用Gradle构建脚本生成的新文件替换资源文件夹(src/main/resources)中的文件。我很难做到这一点;排除似乎被记住,并阻止添加我的新文件 这里有一个简短的例子来说明这种行为 项目结构: TestProject -- src/main/java ---- entry ------ EntryPoint.java ---- run ------ HelloWorldTest.java -- src/main/resources ---- test.properties // F

我试图用Gradle构建脚本生成的新文件替换资源文件夹(src/main/resources)中的文件。我很难做到这一点;排除似乎被记住,并阻止添加我的新文件

这里有一个简短的例子来说明这种行为

项目结构:

TestProject
-- src/main/java
---- entry
------ EntryPoint.java
---- run
------ HelloWorldTest.java
-- src/main/resources
---- test.properties // FILE TO REPLACE
src/main/resources中的test.properties内容:

错误的文件,带有额外的文本,以便根据大小清楚地显示哪个文件正在放入jar中

build.gradle:

apply plugin: 'java'

task makeProp {
    def propDir = new File(buildDir, "props")
    ext.propFile = new File(propDir, "test.properties")
    outputs.file propFile

    doLast {
        propDir.mkdirs()
        propFile.createNewFile()
        propFile.withWriter('utf-8') { writer ->
            writer.writeLine 'Right File'
        }
    }
}

jar {
    dependsOn('makeProp')

    if (project.hasProperty('testExclude')) {
        sourceSets {
            exclude('test.properties')
        }
    }

    from (makeProp.propFile) {
        into '/'
    }
}
apply plugin: 'java'

def props = project.file('src/main/resources/test.properties')
def temp = project.file('src/main/resources/temp.properties')

task makeProp {
    ... // Unchanged
}

jar {
    dependsOn('makeProp')

    // Move the properties file to a temp location
    props.renameTo(temp) // This returns a boolean; can perform conditional checks.

    // Exclude the temp file
    if (project.hasProperty('testExclude')) {
        sourceSets {
            exclude('temp.properties')
        }
    }

    // Insert the right prop file
    from (makeProp.propFile) {
        into '/'
    }
}
jar << {
    // Restore the temp file
    temp.renameTo(props)
}
/gradlew build
(包括两个文件)的JAR内容:

/gradlew build-PtestExclude
(两个文件都不包括)的JAR内容:


问题是gradle的排除模式适用于所有包含的路径,而不仅仅是特定的路径。因此,在上面的示例中,所有名为
test.properties
的文件都将被排除在外,无论其位置如何


您可以做的是将默认的
test.properties
放在其他地方,并根据场景复制/生成相关版本。

我做了一些非常类似的事情,这对我来说很有用。主要目标是确保任务在创建jar和处理文件之前运行。试试这个

    // create a properties file add it to folder preprocessing
    task propertiesFile << {
        // 
        description 'Dynamically creates a properties file.'

        // needed for the first pass
        def folder = project.file('src/main/resources');
        if(!folder.exists()){
            folder.mkdirs()
        }

        //write it to a propertiess file
        def props = project.file('src/main/resources/test.properties')
        props.delete()
        props << 'write this to my file!!!!'

    }

    processResources.dependsOn propertiesFile
//创建属性文件并将其添加到文件夹预处理

任务属性file我最终采用的解决方案与Pumphouse发布的类似,但我没有删除该文件,而是将其重命名为临时名称,排除该临时名称,并在完成后重命名(我需要在构建完成后保留文件内容和位置)

修改的build.gradle:

apply plugin: 'java'

task makeProp {
    def propDir = new File(buildDir, "props")
    ext.propFile = new File(propDir, "test.properties")
    outputs.file propFile

    doLast {
        propDir.mkdirs()
        propFile.createNewFile()
        propFile.withWriter('utf-8') { writer ->
            writer.writeLine 'Right File'
        }
    }
}

jar {
    dependsOn('makeProp')

    if (project.hasProperty('testExclude')) {
        sourceSets {
            exclude('test.properties')
        }
    }

    from (makeProp.propFile) {
        into '/'
    }
}
apply plugin: 'java'

def props = project.file('src/main/resources/test.properties')
def temp = project.file('src/main/resources/temp.properties')

task makeProp {
    ... // Unchanged
}

jar {
    dependsOn('makeProp')

    // Move the properties file to a temp location
    props.renameTo(temp) // This returns a boolean; can perform conditional checks.

    // Exclude the temp file
    if (project.hasProperty('testExclude')) {
        sourceSets {
            exclude('temp.properties')
        }
    }

    // Insert the right prop file
    from (makeProp.propFile) {
        into '/'
    }
}
jar << {
    // Restore the temp file
    temp.renameTo(props)
}
apply插件:“java”
def props=project.file('src/main/resources/test.properties')
def temp=project.file('src/main/resources/temp.properties')
任务makeProp{
…//不变
}
罐子{
dependsOn('makeProp')
//将属性文件移动到临时位置
props.renameTo(temp)//返回布尔值;可以执行条件检查。
//排除临时文件
if(project.hasProperty('testExclude')){
源集{
排除('临时属性')
}
}
//插入正确的道具文件
来自(makeProp.propFile){
进入“/”
}
}

我最后做了类似的事情。我已经接受了这个答案,我自己的答案。我希望有一个更直接的解决方案,而不是基本上围绕它们的排除机制工作,但我确信,该机制以我这样的构建新手无法想象的方式工作,肯定有一些合理的原因。