【问题标题】:Replace File in Gradle Build在 Gradle 构建中替换文件
【发布时间】:2015-10-31 09:32:06
【问题描述】:

我正在尝试用 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内容:

Wrong File with extra text to make it obvious which one is being put into the jar based on size

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 '/'
    }
}

./gradlew build 的 JAR 内容(包括两个文件):

Archive:  TestProject.jar
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  08-07-15 14:27   META-INF/
       25  08-07-15 14:27   META-INF/MANIFEST.MF
        0  08-07-15 13:50   run/
      499  08-07-15 13:50   run/HelloWorldTest.class
        0  08-07-15 13:50   entry/
     1413  08-07-15 13:50   entry/EntryPoint.class
       95  08-07-15 14:27   test.properties
       11  08-07-15 14:03   test.properties
 --------                   -------
     2043                   8 files

./gradlew build -PtestExclude 的 JAR 内容(不包括任何文件):

Archive:  TestProject.jar
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  08-07-15 14:29   META-INF/
       25  08-07-15 14:29   META-INF/MANIFEST.MF
        0  08-07-15 13:50   run/
      499  08-07-15 13:50   run/HelloWorldTest.class
        0  08-07-15 13:50   entry/
     1413  08-07-15 13:50   entry/EntryPoint.class
 --------                   -------
     1937                   6 files

【问题讨论】:

    标签: java groovy gradle build


    【解决方案1】:

    我做了一些非常相似的事情,这对我有用。主要目标是确保任务在创建 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
    

    【讨论】:

    • 我最终做了类似的事情。我已经接受了这个作为答案,我自己也可以遵循。我希望有一个比必须基本上解决他们的排除机制更直接的解决方案,但我敢肯定,必须有一些合理的理由让机制以像我这样的构建新手无法想到的方式工作的。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      修改 build.gradle:

      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)
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-18
        • 2014-08-05
        • 2017-01-19
        • 1970-01-01
        • 1970-01-01
        • 2020-09-09
        • 2014-12-16
        • 1970-01-01
        • 2012-02-20
        相关资源
        最近更新 更多