How do I add external directories/jars as a module dependencies using gradle?
Answered
In my multi-module gradle project, I am loading some external jar files (as plugins). I am currently adding external jar directory to idea module like this:

I don't want to add those jars as a compile dependencies as it was not required during compilation. Just to run the application from intellij run profile I need those jars to be in classpath. How do I set these dependencies using gradle idea module?
Please sign in to leave a comment.
If you have a Gradle project, you should use build.gradle to manage dependencies:
https://docs.gradle.org/current/userguide/introduction_dependency_management.html
That is ok. I am doing that for project dependency management. The problem is if I add
compile fileTree(dir: '/lib', include: '*.jar')
or
runtime fileTree(dir: '/lib', include: '*.jar')
intellij is not picking it up when I run the application from IDE. But if I add the dir as a module dependency (like the picture shown in the question), then intellij is picking it up during run. Those jars are not required during compilation, they are plugins, so required only in runtime classpath.
So I was looking for how to customize the idea task in gradle so that the lib dir is attached to the classpath during application run.
You need to set a dependency scope, looks like you need a "Runtime" scope:
https://www.jetbrains.com/help/idea/dependencies.html
Is it possible to do using gradle script? I already tried as below
runtime fileTree(dir: '/lib', include: '*.jar')
But did not work.
This should work. Did you refresh the Gradle Project? Do you see these jars added to the module dependencies with Runtime scope? If not, then your Gradle configuration is incorrect and you'll need to provide a sample project to illustrate the issue so that we can help you to find the problem with the configuration.