1

We have several git repos in our environment, some are web front, some are service layer, and some are back end. Some of our modules are libraries that are dependencies of other modules and some are not dependencies but contain web services etc.

I need to be able to setup Gradle to optionally link to the source for a module if the developer has that module included in the IntelliJ project. For example, a UI developer doesn't need all of the Java libraries to do their job but another developer might.

It is possible to add the maven plugin for Gradle and do an install on the dependent modules but that is far from ideal. Doing so requires us to manually do an install on each module to put the compiled jar into the .m2 directory so the dependent modules can then use that jar. After every install/refresh you have to go into IntelliJ and re-add the source path for each library as it isn't automatic. This method also doesn't automatically do the install when I rebuild my project either.

I know it is possible as we did it at a company I worked for in the past. I wasn't involved much with the build management so I didn't learn how it was done.

Gremash
  • 8,158
  • 6
  • 30
  • 44

1 Answers1

1

I found a great solution to the problem here: https://stackoverflow.com/a/23574904/3088642.

I am trying to make the code work in plugins and will post the code here if I succeed.

Here is my full top-level build.gradle file:

// Make sure we've parsed sub-project dependencies
evaluationDependsOnChildren()
// Map of all projects by name
def subProjectsByName = subprojects.collectEntries { p -> [p.name, p] }

// Replace artifact dependencies with subproject dependencies, if possible
subprojects.each { p ->
    def changes = [] // defer so we don't get ConcurrentModificationExceptions
    p.configurations.each { c ->
        c.dependencies.each { d ->
            def sub = subProjectsByName[d.name]
            if (sub != null) {
                changes.add({
                    c.dependencies.remove(d)
                    p.dependencies.add(c.name, sub)
                })
            }
        }
    }
    for (change in changes) {
        change()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

And my settings.gradle file:

// find all sub-projects and include them
rootDir.eachFileRecurse {
    if (it.name == "build.gradle") {
        def projDir = it.parentFile
        if (projDir != rootDir) {
            include projDir.name
            project(":${projDir.name}").projectDir = projDir
        }
    }
}
Community
  • 1
  • 1
Gremash
  • 8,158
  • 6
  • 30
  • 44