Workaround Implementation of Gradle's Fully Custom Startup Script

The project uses Gradle as a packaging tool and uses the Application plug-in, but the automatically generated startup script cannot meet the needs of the project, and a custom startup script is required. My last article was by file copying, renaming, and then putting it into the package when it was packaged. Interested students can check http://buddie.iteye.com/blog/2358311

But the last implementation has two problems that cannot be solved:

1. Permission issues

When generating the script as much as possible, the executable file is added to the script file through the program, but the script is typed into the package, and after decompression, the permissions will change (now I think it can be solved, and the generated script can also be placed directly in target/ scripts directory)

2. File format problem

The development environment is Windows, and the files are in DOS format, not UNIX format. The generated script needs to be corrected by :set ff=unix on the Linux server, which is very inhumane.

 

Now I spent some time researching it, looking at the source code related to the CreateStartScripts interface in Gradle, and found a solution.

The principle is simple and crude: the script is still generated through the template, but instead of using Copy, the content of the template file is manually read, the newline character "\n" is added, the variable is replaced, and then the content is written to the target/scripts directory. and add permissions.

 

In this way, the problem of DOS format and UNIX file format is solved first, and the newline character "\r\n" is replaced by "\n". In fact, the problem of execution permissions is solved, because the scripts automatically generated by the application are placed in this directory, and after packaging, the permissions are reserved. Then I also put the script in this directory. The experimental results proved my approach.

 

The code is as follows: build.gradle

import org.apache.tools.ant.taskdefs.Chmod
startScripts << {
    File template = new File("src/script/launch.model")
    List<String> lines = template.readLines("UTF-8")
    java.lang.StringBuffer buffer = new StringBuffer()
    for (String line : lines) {
        buffer.append(line).append("\n")
    }
    String shellContent = buffer.toString()
    shellContent = shellContent.replaceAll("@gradleProjName@", applicationName)
    shellContent = shellContent.replaceAll("@gradleVersion@", pk_version)
    TextUtil.convertLineSeparators(shellContent, "\n")
    File outFile = new File(getProject().getBuildDir().getPath() + "/scripts/launch.sh")
    Writer writer = new FileWriter(outFile)
    writer.write(new String(shellContent.getBytes("UTF-8")))
    writer.flush()
    writer.close()
    Chmod chmod = new Chmod()
    chmod.setFile(outFile)
    chmod.setPerm("ugo+rx")
    chmod.setProject(AntUtil.createProject())
    chmod.execute()
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403286&siteId=291194637