Multi project jar generation

In the spirit of code reuse and only writing specific code once, I’m trying to create a setup where I have a huge multi project with lots of sub projects which might depend on each other and create a bunch of jars to be reused in different (new) projects. These subprojects have simple functions like sending emails or parsing files. For this purpose I would like to generate jars which contain all the local dependencies (read: dependencies of other subprojects) in the jars. If I can just add other jars inside the jar and reuse that I would be able to use that but I have not found how to depend on jars inside jars with gradle.

For illustrative purposes below is a simple example of how the project looks

I have a multi project gradle project (Project1)

Project1 looks like this:

Project1

  • SubProject1
  • SubProject2
  • SubProject3

in which SubProject2 depends on SubProject1

I would like to create a jar file containing the class files of both these projects but not SubProject3 (since it has no dependencies) in the SubProject2 build dir. Preferably with a single task for all of these sub projects (so if a SubProject4 would depend on 3, it would also create such a combined jar)

How would I go about doing this?

Why do you want Subproject2 to contain all the class files from Subproject1?

Is it not sufficient enough to declare Subproject1 as a dependency of Subproject2?

dependencies {
    compile project(':subproject1')
}

This would create the jar of subproject1, and then use that on the classpath when compile subproject2.

There certainly is a way to do what you want, I’m more curious than anything.

Because I want to use SubProject2 in a totally different Root gradle project without having to include both subproject1.jar and subproject2.jar

I’m looking for a best practice solution for sharing my code helper classes between projects and having to write it only once. while jenkins checks these helper classes (and dependent projects) on every new commit to see if something breaks.

Goals:

  • Never have to copy code (compiled code from a local repo would be fine)
  • Never have broken code in production (Ok, we will never reach this, but at least try to get as close as possible with existing technologies)

A more practical example:

HelperClasses

  • JDBCDatabase
  • MSSQLDatabase (uses JDBCDatabase and MSSQL jdbc driver jar)
  • PostGreSQLDatabase (uses JDBCDatabase and postgres jdbc driver jar)

Application1 uses MSSQLDatabase, I don’t want to have to include JDBCDatabase.jar, neither do I want it to have any code/jars for a PostGreSQL database

WebApplication1 uses PostGreSQLDatabase, I don’t want to have to includ JDBCDatabase.jar, or have it include MSSQL

You mentioned in your other question about using Artifactory or something equivalent. That’s probably your best bet.

Have each of the HelperClasses projects compile to individual jars.

When uploading to Artifactory (I have more experience with Nexus, so this might not be the case), Gradle will define a Maven POM file that has the dependencies defined.

Then in Application1 you define the single dependency of MSSQLDatabase.jar, and gradle will pull down all the transitive dependencies as well.

How this would look:

JDBCDatabase compiles to a .jar (JDBCDatabase.jar), and gets uploaded to your repo.
MSSQLDatabase compiles to a .jar and has a dependency on JDBCDatabase. When that gets compiled, Gradle will pull down that dependency from your repo. When that gets uploaded to your repo, the POM file that gets generated references JDBCDatabase.jar.

Application1 will define the MSSQLDatabase dependency, then when it compiles, it’ll pull down both MSSQLDatabase and JDBCDatabase from your repo.

Does that make sense?

You can configure Jenkins to build the latest stuff and upload to your repo.

Yes it does make sense! Thanks for the explanation! This sounds like it’s the solution I’ve been looking for!