Gradle modify file content, rename

When using Gradle to package, you need to put the handwritten service startup script into the compressed package, which originally only needs simple code.

applicationDistribution.from('src/script/runServer.sh'){
    into "bin"
}

 But in the actual project, the project name can be configured, and the version can be configured. The deployed directory on the server has the project name and version information. For example, the actual project directory is /data0/projName-1.2.3/. In order to make the script work normally, my idea is to maintain a script template. The template uses two variables to represent the project name and version information, and then when packaging, replace the script template with the project name and version information in the Gradle configuration file. variable to generate a script file. Finally, the generated script file is typed into the package.

This gets to the point - use Gradle to modify the contents of the file and rename it.

My implementation is as follows

 

#!/bin/bash

projName="@gradleProjName@"
projVersion="@gradleVersion@"
projFolder = $ {projName} - $ {projVersion}

 runServer.model

 

 

applicationName='projName'
app_version='1.2.3'

import org.apache.tools.ant.filters.ReplaceTokens
task genShell(type: Copy) {
    from 'src/script/'
    include 'runServer.model'
    into 'src/script/'
    rename ('runServer.model','runServer.sh')
    filter (ReplaceTokens, tokens: [
            gradleProjName: applicationName,
            gradleVersion: app_version
    ])
}

 build.gradle


 

Note: Using Gradle version 2.14.1

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681097&siteId=291194637