2

I'm trying to make an executable program file to distribute of my java program. Normally I would make this file by running:

$ echo '#!/usr/bin/java -jar' > program
$ cat my-program.jar >> program
$ chmod +x program

As far as I can tell Gradle is supposed to be able to make distributions of software. I'm not sure if this extends to making Operating System releases or if it's just files to be used by package maintainers. as covered here

I would like to be able to have Gradle prepare distribution files for making packages. Something like this:


distributions
└── project_1.1-1
    └── Folder
        ├── control
        └── usr
            └── local
                └── bin
                    └── program

So far I have tried this in build.gradle:

distributions {
    write '#!/usr/bin/java -jar'
    into program
    Jar {
        from sourceSets.main.output
        dependsOn configurations.runtimeClasspath
        from {
            configurations.runtimeClasspath.findAll {
                it.name.endsWith('jar') }.collect { zipTree(it) }
            }
        }
    } into program
}
9716278
  • 2,044
  • 2
  • 14
  • 30
  • Your `echo` commands create a text file with two lines, not a binary. This is not the same as what you are attempting to do in your `build.gradle` file. – Code-Apprentice Jun 25 '19 at 21:50

2 Answers2

1

You should probably rather create a runnable distribution using the gradle application plugin, e.g. see this questions:

Else you can write a file similar to this question: create version.txt file in project dir via build.gradle task

task versionTxt()  {
    doLast {
        new File("$projectDir/version.txt").text = """
#!/usr/bin/java -jar
#...
"""
    }
}

and change permissions like in this change file permission by gradle

tkruse
  • 10,222
  • 7
  • 53
  • 80
0

To duplicate your echo commands, you only need to write the name of the JAR file as the second line in program. Note that you are not creating a binary file here. Rather you are creating a bash script that automatically runs the java interpreter on your JAR file.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268