Best way to export dependencies when building IDEA project

EDIT: The problem was due to some bad configuration that was causing all of our runtime and compile dependencies to be converted to Provided dependencies in IDEA. When I fixed this issue, all of the runtime and compile dependencies were exported as expected.

I have a project with about a dozen modules, each containing dozens of dependencies. When building on the command line, everything is fine. However, when I build the IntelliJ IDEA project (gradle idea), IDEA does not treat the dependencies as transitive by default, and I need to manually check the export box for several dependencies so that, e.g. project B, which depends on A, has access to library C via its dependency on project A.

I’ve tried marking the dependencies as transitive in build.gradle, but that doesn’t cause them to be exported in the generated .iml files. Using the code will export all dependencies:

idea.module.iml {
    whenMerged { module ->
        module.dependencies*.exported = true
    }
}

Maybe this is the best solution? I’m not entirely sure of the ramifications of exporting all dependencies.

Anyway, through trial and error I’ve found a way to export only the dependencies I want, but it’s hacky and probably brittle:

// Retuns true if dep is a single-module dependency whose classes url contains
// libname.
def dependencyFor(dep, libname) {
    if (!(dep instanceof org.gradle.plugins.ide.idea.model.SingleEntryModuleLibrary)) return false
    dep.classes.url.head().contains(libname)
}

// Export these libraries so the project can compile in IDEA. For some reason
// Gradle's IDEA plugin doesn't automatically export them.
idea.module.iml.whenMerged { module ->
    module.dependencies.each { dep ->
        ['akka-actor', 'jackson-core-asl', 'jackson-mapper-asl',
         'rxjava', 'scala-logging'].each { lib ->
            if (dependencyFor(dep, lib)) dep.exported = true
        }
    }
}

Obviously I’m not terribly fond of this solution. I do have a couple of dependencies that are automatically exported, and I have no idea why. Is there a better way to make Gradle export specific dependencies when generating .iml files? Searching the Internet, I find people saying that I don’t need to worry about this and Gradle should automatically export what it needs to export, but that’s not happening. Is this a bug?

This topic was automatically closed 1 minute after the last reply. New replies are no longer allowed.