Iām working on a gradle script where one of the task needs to do the following:
-> Execute a pre-built jar file, which expects couple of arguments/parameters. The jar file depends on some other files from the same directory where the jar file is located.
At the moment, I donāt need to compile/build anything from a java source code. I already have a jar file that needs to be executed in a task along with few other tasks.
Iām going through gradle documentation and searching online too, but most info seems to be revolving around compiling and building from java source code. I believe in my case, I just need to use the āapplicationā plugin but I donāt think Iām doing it properly.
Hereās what my build.gradle looks like right now:
The folder structure is pretty simple. at the root, I have build.gradle file, whose content is posted above. I have a directory named ālibā where Iāve stored the jar file and other files it requires. The 3rd line is where Iām (trying to) pass parameter that is required by the jar file for execution.
Iāve tried few other options, but not getting anywhere. Would someone be able to point me to a direction what exact Iām missing here? Even if I have the source code to re-build it using Gradle (which I donāt think is necessary), will it compile the source code every time the build script is run, even though the source code for that java program is not changing.
Youāre close. Itās OK to use javaexec {}, but usually itās better to use the JavaExec task type. Itās similar to Exec.
I donāt think you need any plug-ins for this unless youāre already building something else. I think youāre having trouble now because you need to set the working directory.
The configurations/dependencies blocks might seem extra, but Iād only use an explicit file as a classpath if I was sure the jar would contain everything I needed to run (i.e., no other runtime dependencies). If your jar has any other dependencies, you can just add them to the scheduleRuntime configuration as a regular dependency (e.g., ālog4j:log4j:1.2.17ā).
Thank you Sterling for your reply. Youāre right that in this case I donāt need to use any plugin.
I do like your approach and Iām trying to use it but keep getting an error during the build. Itās complaining about the quotes in following line:
scheduleRuntime files("lib/JarFileName.jar")
The error Iām getting is āUnexpected tokenā and then it points to the first quote. I donāt see any typo in my code and based on Gradleās documentation link below, it seems I have it correct. Not sure why Gradle is complaining about this line.
The jar file Iām working with does have other dependencies, which are all located in the same ālibā directory where jar itself is located. Do I need to use filetree instead of files in the dependencies?
Nope. I commented out everything in the script except for the configurations and dependencies block. It was still complaining about āunexpected tokenā on that dependency. I changed it to use fileTree method as shown below and Iām able to run the script now.