How to include class files from one project in another


(Mark Lamb) #1

So I have a multi project setup that looks something like this

Root Project
–> common
–> project1
–> project2
–> 3rd_party_api

So common obviously contains a bunch of code shared across the other projects. Projects 1 and 2 are fine because they are wars and contain the common jar file as a dependency without any issues.

The problem I have is with my 3rd_party_api project. This is quite a small jar file that we will be delivering to other teams so that they can integrate with our code. Most of the java code required is contained in this project folder however there are 3 or 4 classes that are in the common project and need to be included in this library. Because it has to be standalone I need to wrap those classes in the jar file.

I have tried various iterations of srcDirs and source but I can’t for the life of me figure out an easy way to do this.

As I’ve said I’ve looked at different approaches but my latest attempt looked a bit like this:

project(':api') {
  defaultTasks 'jar'
  apply plugin: 'java'
  sourceSets {
  	main {
  	  java {
	    srcDir 'src/main/java'
        srcDir fileTree(dir: '../common/src/main/java').matching { include 'com/my/classes/**' }
	  }
	}
   dependencies { 
     compile project(':common')
  }
}

This compiles successfully but the extra classes from common are not included in the jar.
Any help would be greatly appreciated.