2

I have two different java projects, projectA and projectB. I'm at a stage where projectB depends on projectA. How do I pull in the jars created by building projectA and all of its dependencies?

In essence, I would like to mirror the effect of adding a projectA to the build path of projectB in eclipse,effectively pulling in jars on the classpath projectA.

John
  • 619
  • 2
  • 9
  • 24

1 Answers1

2
  1. If the two projects are closely related you can make Project A and Project B part of the same Multi-project build, then:

    //ProjectB build.gradle
    dependencies {
      compile project(':ProjectA')
    }
    
  2. If they're not closely related you can publish the output of Project A to a repository e.g. your local Maven, Artificatory, Bintray etc. Then in Project B you'll get it like any other dependency. E.g.

    //ProjectB build.gradle
    repositories {
      mavenLocal()
      //Or something like this:
      //maven { url "http://urltoyourartifactory.com/repo/" }
    }
    
    dependencies {
      compile 'com.yourname:project-a:1.0.0'
    }
    
Heinrich Filter
  • 5,760
  • 1
  • 33
  • 34
  • Do I get project-a jar and all of it's dependencies by doing this or am I just getting project-a jar? – John May 29 '15 at 06:21
  • If Project A is setup correctly, all it's dependencies will be included by default. To manage which transitive dependencies are added from Project A into Project B, have a look at the [Dependency Management](https://docs.gradle.org/current/userguide/dependency_management.html) chapter – Heinrich Filter May 29 '15 at 06:46
  • I have this setup but when I store a file in `core/src/main/resources/item.txt` then my web project can't use Spring's `ResourceLoader.getResource("classpath:item.txt")` and vice versa. If the file is stored in `web/src/main/resoures/item.txt` then my core project can't use Spring's `ResourceLoader.getResource("classpath:item.txt")` command. The error is always `java.io.FileNotFoundException`. My `build.gradle` for web is such that is says `compile project(':core')`. Any ideas? – fIwJlxSzApHEZIl Nov 02 '17 at 21:38
  • @anon sounds like you may need to read the content from the classpath as an InputStream and not as a file, see https://stackoverflow.com/questions/14876836/file-inside-jar-is-not-visible-for-spring – Heinrich Filter Nov 03 '17 at 08:36
  • @HeinrichFilter thanks for the input but that didn't solve the problem. No method of importing the file through Spring's `ResourceLoader`, `ClassLoader`, as a file, or as an input stream helped the issue. What I had to do was 'import' the core's project resources folder at build time for the 'web' project in the build.gradle file. Answer is here: https://stackoverflow.com/a/43686346/584947 – fIwJlxSzApHEZIl Nov 04 '17 at 15:05