How do I share a sub-project with multiple root projects?

I have a library that is a dependency on multiple other projects. How do I declare that library as a dependency on them? For instance, I this library has common interfaces, etc, and it’s required for other libraries and web projects. I’d prefer not to have duplicate copies of the same library project, because if I make a change to any classes I need those changes visible too all projects, not just the immediate root project. Maybe I’m over thinking this, but the root project and sub-project structure doesn’t seem to take that into consideration.

1 Like

Are you talking about a single multi-project build, or multiple independent builds?

multiple independent builds.

Keep in mind that I’m in the process of converting my old Ant projects to Gradle, and this is a hurdle I’m facing now. Below is what I had for my Ant projects:

Library #1 Library #2 depends on Library #1 Web Project #1 depends on Library #2 and Library #1 Swing Deskptop Application depends on Library #2 and Library #1

The first thing I did was create a single Gradle project for Library #1 and copied over the classes from my original Ant project, adding the external dependencies required. Next, I create another single Grandle project for Library #2, but I can’t add Library #1 as a dependency because it’s not a sub-project. I could make it a sub-project, but I will run into the same problem when I get to the web project and deskptop app, which both require Library #1 and #2, but cannot be a sub-project of each other.

I’m confused. :slight_smile:

In a nutshell, you can either have one multi-project build and specify the necessary project dependencies, or have four independent builds and exchange artifacts via a Maven or Ivy repository.

The logical project hierarchy in a multi-project build has nothing to do with how the subprojects depend on each other in terms of their code. In your case, the natural solution would be to have four subprojects (lib1, lib2, web, desktop).

To learn more about multi-project builds, check out the “multi-project builds” chapter in the Gradle User Guide and the samples in the full Gradle distribution.

Great, thank you.

The only odd thing is that three artifacts are generated for my web app: *.jar, *.war, *source.jar. Is that normal?

Quick question before I mark this tread as solved. How do I configure the EE container I want to use for testing? I’ve been using Glassfish, where my old Ant project would deploy to a running GF instance and then launch and external browser. I’ve been looking online on how to do the same, but it’s confusing. From what I can tell, I should use Embedded GF 4 (it’s supposed to be in Maven Central), and supply a domain.xml in the project to configure it. Of course, I don’t know how to accomplish any of that, esp with editing build.gradle.

It’s normal to have a Jar generated for a War project (because the ‘war’ plugin builds on top of the ‘java’ plugin). If you don’t need the Jar, you can do ‘jar.enabled = false’. A sources Jar will only be generated if you add a task for that.

As for interacting with the EE container, I recommend that you look into the following plugins:

Thanks for all your help, Peter. Much appreciated. BTW, I’m jumping fron Ant right to Gradle, so I don’t have the Maven knowledge. Picking it up as I go. :slight_smile: