如何强制Gradle覆盖自定义WAR任务中的资源文件?



我创建了一个war task,用src/release/resources:

覆盖src/main/resources
task warRelease(type: War) {
    webInf {
        from 'src/release/resources'
        into 'classes'
    }
    duplicatesStrategy = DuplicatesStrategy.WARN
}

意外存在两个数据库。war文件中的属性

$ jar tf build/libs/project.war | grep database
WEB-INF/classes/database.properties
WEB-INF/classes/database.properties

根据这个,

从Gradle 0.9.1开始,Copy任务总是覆盖文件。另一个目前还不支持策略

gradle - v

Gradle 2.14.1
------------------------------------------------------------
Build time:   2016-07-18 06:38:37 UTC
Revision:     d9e2113d9fb05a5caabba61798bdb8dfdca83719
Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_102 (Oracle Corporation 25.102-b14)
OS:           Linux 4.7.2-1-ARCH amd64

War任务只扩展了AbstractCopy任务。这个链接直接谈到了CopyTask。通常使用setDuplicatesStrategy API让任务排除重复文件。诀窍是次要文件总是被排除在外,并且(我认为)您不能为WAR任务覆盖它。

默认情况下,src/main/resources文件始终包含在WAR任务中。我还没有找到一种方法来改变默认包含的顺序。下一个最佳选择是直接从WAR中排除被替换的文件:

task warRelease(type: War) {
   classifier 'release'
   webInf {
      from 'src/main/release'
      into 'classes/'
   }
   rootSpec.filesMatching(/database.properties/) { details ->
      if (details.file.path =~ "build/resources/main/"){
         details.exclude()
      }
   }
}

您可能想知道为什么被排除的文件来自路径build/resources/main/。这是因为processResources任务将复制所有的sourceSets的资源文件到这个位置之前,他们被包括到WAR。

您指定的build.gradle文件确实会像人们怀疑的那样向控制台写入警告消息(re: duplicate)。

为了使WAR文件优先选择src/release/resources/database.properties而不是src/main/resources/database.properties,请考虑如下:

apply plugin: 'war'
task warRelease(type: War) {
    webInf {
        exclude 'src/main/resources/database.properties'
        from 'src/release/resources'
        into 'classes'
    }
}

相关内容

  • 没有找到相关文章

最新更新