Augusto
Augusto

Reputation: 1336

Gradle Distribution create File

I'm using gradle to build a desktop app and I'm trying to create a distribution for my app. I've used the application plugin (which inherit the distribution plugin) and when I call the distZip task, I would like to create a file into that zip with a configuration file with values passed as arguments from the gradle command. For ex: when I call

gradle build distZip --project-prop myproperty=myvalue

I want to generate a properties file inside the zip distribution with that content. I've tried the following approach with no success:

applicationDistribution.into(".") {
    new File("server.properties").write("myproperty=$myproperty")
}

When I do this, it creates a file in the root of the project instead of the root of the distribution zip. Does anyone know how to create this file inside the distribution zip?

Upvotes: 0

Views: 2568

Answers (1)

Opal
Opal

Reputation: 84784

It will be:

apply plugin: 'distribution'

distZip {
   def p = File.createTempFile('aaa', 'bbb')
   p.write "my.property: $project.myProperty\n"
   p.deleteOnExit()

    from(p) {
      rename {
         'server.properties'
      }
   }
}

and invocation:

gradle clean distZip -Pmyproperty=bolo

You need to configure the task that prepares the distribution - in this particular case distZip - it's task of type Zip (Copy).

Be careful with referencing properties with project.myproperty. If no myproperty is found an exception will be thrown.

Upvotes: 0

Related Questions