Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy additional files in Gradle Application Plugin

Tags:

java

gradle

I have a small Java/Gradle project. I'm using the Application plugin to create a zip distribution (using the distZip task). Using the standard configuration I get the following directories in my zip file:

/bin - The scripts to start the application go in here
/lib - Contains my project code in a JAR file and all dependency JAR files.

The trouble is that I would like a third directory: /conf where I can put my configuration files (instead of having them packaged inside my application JAR file.

I imagine that this is a pretty common requirement because things like log4j.xml and hibernate.properties would be better placed outside the JAR file. I just can't figure out how I can customise the behavior of the Application plugin to do this however.

like image 969
James Blewitt Avatar asked Apr 21 '11 10:04

James Blewitt


People also ask

How do I add plugins to Gradle?

To add a Gradle plugin with dependencies, you can use code similar to the following: Plugin DSL GA versions. Plugin DSL non GA versions. Legacy Plugin Application.

What does Gradlew installDist do?

You can run gradle installDist to assemble the uncompressed distribution into $buildDir/install/${project.name} .

What is subproject in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency. Example 2.


3 Answers

I revisited this problem several months later and I finally have an elegant solution. The following code should be added to the gradle file:

distZip {     into(project.name) {         from '.'         include 'conf/*'     } } 

This adds an additional include to the distZip task. This copies the "conf" directory (including contents) into the Zip distribution.

The generated zip file contains a single directory which is the same as the project name. This is why the "into" part is required.

like image 143
James Blewitt Avatar answered Sep 22 '22 00:09

James Blewitt


Actually, create a dist dir under the src dir in your project. Anything in this dir is copied by the application plugin (under applicationDistribution) when installApp or distZip is run.

Or edit applicationDistribution to do other things, if a simple copy is not enough.

like image 23
Graham Avatar answered Sep 22 '22 00:09

Graham


For me, a simple

applicationDistribution.from("src/main/config/") {
    into "config"
}

did the job. Of course you need to have your properties loaded correctly from within code. Especially if you move them from src/main/resources where they have been usable via classpath, into the new location. I circumvented this by adding a command line parameter which points to the configuration file.

like image 40
ferdy Avatar answered Sep 19 '22 00:09

ferdy