Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding classpath entries using Gradle's Application plugin

Tags:

I'm using Gradle's Application plugin to generate the install for a standalone java application. I have a configuration file I need to put on the classpath but I can't seem to get it to generate the classpath correctly in the sh/bat files. This configuration file needs to be located outside of the jar.

The conf file is in the directory /src/dist/conf/ so when I run installApp it installs it under a conf directory like this $APP_HOME/conf.

I tried adding this directory to the claspath like this:

startScripts.classpath.add(files('$APP_HOME/conf')) 

but when I look at the classpath in the sh/bat files it adds an entry that looks like this:

$APP_HOME/lib/conf 

How do I tell gradle to drop the lib section for this entry?

like image 226
Josh Avatar asked May 09 '12 14:05

Josh


People also ask

What is classpath in Gradle?

Gradle dependencies are grouped into sets called configurations. Different configurations are used for building classpath for the major two tasks — compile classpath is used for compilation and runtime classpath is used for running the application.

What does Gradlew installDist do?

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

What does Gradle application Plugin do?

The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.

Which are the two types of plugins in Gradle?

Applying Plugins There are two types of plugins one is script plugin and second is binary plugin.


2 Answers

Another workaround for this issue (GRADLE-2333) is proposed by Alexandr Fadeev here.

Here is (a bit modified) Alexandr's solution that solved the problem for me on Gradle-1.6:

startScripts {   classpath += files('src/dist/XxxAPlaceHolderForAConfigxxX')   doLast {     def windowsScriptFile = file getWindowsScript()     def unixScriptFile    = file getUnixScript()     windowsScriptFile.text = windowsScriptFile.text.replace('%APP_HOME%\\lib\\XxxAPlaceHolderForAConfigxxX', '%APP_HOME%\\config')     unixScriptFile.text  = unixScriptFile.text.replace('$APP_HOME/lib/XxxAPlaceHolderForAConfigxxX', '$APP_HOME/config')   } } 

It's a bit uglier than Josh's solution, but it allows you to preserve the exact directory layout (/src/dist/conf and $APP_HOME/conf) mentioned in the original question.

like image 176
nehaev Avatar answered Sep 17 '22 16:09

nehaev


The easiest way to get a file onto the class path is to put it into src/main/resources. src/dist is for adding files to the distribution (i.e. zip file), not to the Jar/class path.

like image 20
Peter Niederwieser Avatar answered Sep 21 '22 16:09

Peter Niederwieser