Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle processResources corrupts .jks

Tags:

gradle

I have what I hope is a simple question that I've had no luck finding an answer to.

I want to have the build.gradle file set the version in my Spring Boot application by replacing certain variables. This works as advertised:

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

This code, however, also replaces something in the java key store which I also include in my resources, which corrupts it. When I use ant matchers to exclude anything that's not the file I want to replace, nothing else gets copied. i.e. include '*.properties'

Is there a way to only do the token replacement on certain files while still copying the rest of the files in the resource directory? Do I need to define a separate copy task for non-property files?

Thanks!

like image 800
blur0224 Avatar asked Oct 20 '25 17:10

blur0224


1 Answers

The solution is to skip any binary files while the task processReousrces is executed. For instance, I use expand() to replace tokens in text files with values computed in the gradle script. So,

  1. Skip the jks file
  2. Move it to the binary resources

Here's how I can skip the files under the directory src/main/resources/certs/. The doLast() guarantees that the jks file is copied to the appropriate location when it finishes with the resources.

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"
    }
  }
}
like image 58
Marcello de Sales Avatar answered Oct 23 '25 08:10

Marcello de Sales



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!