Gradle processResources 损坏 .jks

blu*_*224 4 gradle

我希望这是一个简单的问题,但我没有找到答案。

我想让 build.gradle 文件通过替换某些变量来设置 Spring Boot 应用程序中的版本。这正如广告中所宣传的那样:

def tokens = [
    "version": 'project.version.toString()',
    "projectName": project.name,
    "groupId": rootProject.group,
    "artifactId": project.name
]
processResources{
    filter (ReplaceTokens, tokens: tokens)
    outputs.upToDateWhen{ false }
}
Run Code Online (Sandbox Code Playgroud)

然而,此代码还替换了 java 密钥存储中的某些内容,我也将其包含在我的资源中,这会损坏它。当我使用 ant 匹配器排除任何不是我想要替换的文件的内容时,不会复制任何其他内容。即包含“*.properties”

有没有办法只对某些文件进行令牌替换,同时仍然复制资源目录中的其余文件?我需要为非属性文件定义单独的复制任务吗?

谢谢!

Mar*_*les 5

processReousrces解决方案是在执行任务时跳过任何二进制文件。例如,我使用expand()在 gradle 脚本中计算的值替换文本文件中的标记。所以,

  1. 跳过 jks 文件
  2. 将其移至二进制资源

下面是我如何跳过目录下的文件src/main/resources/certs/。这doLast()保证了 jks 文件在完成资源处理后被复制到适当的位置。

ext {
  commit = 'git rev-parse --short HEAD'.execute().text.trim()
  branch = 'git rev-parse --abbrev-ref --symbolic HEAD'.execute().text.trim()
}

/**
 * Processes the resources, excluding the certs while building.
 */
processResources {
  // Exclude the certs files to be processed as text
  exclude "**/certs/*"

  expand(
    timestamp: new Date(),
    commit: commit,
    branch: branch,
    version: project.version
  )

  // Copy the jks file to the resources (classpath)
  doLast {
    copy {
      from "src/main/resources/certs/server.jks"
      into "$buildDir/classes/main/certs"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)