# Multi project, make existing jar available as dependencies

**URL:** <https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935>\
**Category:** Help/Discuss\
**Created:** [August 30, 2017, 1:22pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935 "2017-08-30T13:22:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreasb70](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/andreasb70/32/6418_2.png) [@andreasb70](https://discuss.gradle.org/u/andreasb70)\
**Post date:** [August 30, 2017, 1:22pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/1 "2017-08-30T13:22:44Z")

</div>

Hello,

I am building multi project build consisting of following projects  
**war1**  
**war2**  
**jar1**

To be able to compile **jar1** , I have added the necessary dependencies pointing to mavenCentral()  
unfortunately, some of the source code in jar1 requires some libraries that are not stored in mavenCentral or other places.

To solve the problem I have added a new subproject called **localJars**  
The project loads the plug-in “maven-publish”. For each jar file stored in the project /lib, the project specifies artifactID,groupID,version.  
Using the task publish I can store all specified jars in my local mavenRepo, at this point, jar1 (or war1, war2) can include mavenLocal() as repo.

I was wondering if I could somehow avoid the need of publishing locally?  
Is it possible to have a gradle project that contains under /lib some jar files, and enrich them with artifactID,groupID,version making the available as dependencies the the other projects of the multi project build?

Thanks

---

<div class="post-metadata">

**Author:** ![onyn](https://avatars.discourse-cdn.com/v4/letter/o/db5fbb/32.png) [@onyn](https://discuss.gradle.org/u/onyn)\
**Post date:** [August 30, 2017, 1:58pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/2 "2017-08-30T13:58:22Z")

</div>

```
repositories {
    flatDir {
        dirs 'lib'
    }
}
dependencies {
    compile name: 'MyDependency'
}

```

This would add ${projectDir}/lib/MyDependency.jar file as dependency to your project.

---

<div class="post-metadata">

**Author:** ![andreasb70](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/andreasb70/32/6418_2.png) [@andreasb70](https://discuss.gradle.org/u/andreasb70)\
**Post date:** [August 30, 2017, 2:14pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/3 "2017-08-30T14:14:43Z")

</div>

Not sure I understand your suggestion.

The code you specified is for the ‘localJars’ project?  
If so, If I understand correctly, multiple jars found under lib would be zipped inside MyDependency.jar?  
After that in war1, war2 and jar1 need to specify a dependency to localJars?

in my jar1 I would rather be able to specify compile group: local, name: mylib, version 1 and then let build find the correct jar inside localJars.  
So I need a way to tag each jar under localJars/lib with group, name and version

---

<div class="post-metadata">

**Author:** ![onyn](https://avatars.discourse-cdn.com/v4/letter/o/db5fbb/32.png) [@onyn](https://discuss.gradle.org/u/onyn)\
**Post date:** [August 30, 2017, 2:56pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/4 "2017-08-30T14:56:23Z")

</div>

Specified snippet do exactly what you want - treat “lib” (or “localJars/lib” if you want) directory as dependencies repository and make jar files inside it available as project dependencies.

With this snippet you can skip publishing artifacts into the maven local repo and attach those jars as dependencies directly.

You need to assign specific name to jar file. For exampe `compile group: local, name: mylib, version: 1` would search for file `localJars/lib/mylib-1.jar`  
“group” part ignored by flatDir repository, but name and version is taken into account.

---

<div class="post-metadata">

**Author:** ![andreasb70](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/andreasb70/32/6418_2.png) [@andreasb70](https://discuss.gradle.org/u/andreasb70)\
**Post date:** [August 30, 2017, 4:32pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/5 "2017-08-30T16:32:13Z")

</div>

I see your point, but something is missing.  
let’s say that i have:

```
rootProject
myJarProject
myWarProject
localJarsProject

```

inside (or in the rootProject) localJarsProject i place all my jars under lib and make them available with:

```
repositories {
    flatDir {
        dirs 'lib'
    }
}

```

now in myJarProject or myWarProject i need to use:

`compile group: local, name: mylib, version: 1`

the file is present as localJarsProject/lib/mylib-1.jar  
somehow I need to tell myWarProject/myJarProject to look into the libs defined in localJarsProject.  
How do I do that?

---

<div class="post-metadata">

**Author:** ![onyn](https://avatars.discourse-cdn.com/v4/letter/o/db5fbb/32.png) [@onyn](https://discuss.gradle.org/u/onyn)\
**Post date:** [August 30, 2017, 4:47pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/6 "2017-08-30T16:47:13Z")

</div>

I dont know how dependency nesting works, but try this.  
In settings.gradle declare:

```
include 'localJarsProject'

```

And in myJarProject/myWarProject:

```
dependencies {
    compile project(':localJarsProject')
}

```

Useful links:  
[Multi project build](https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:project_jar_dependencies)

> <https://stackoverflow.com/questions/13595610/gradle-nested-multi-projects-with-project-compile-dependencies>

---

<div class="post-metadata">

**Author:** ![andreasb70](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/andreasb70/32/6418_2.png) [@andreasb70](https://discuss.gradle.org/u/andreasb70)\
**Post date:** [August 30, 2017, 5:10pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/7 "2017-08-30T17:10:20Z")

</div>

mmm, It’s not working.  
My multi project build is already setup and works well besides this.  
For testing I have placed my external libs inside the root project and used in myJarProject:

```
dependencies {
    compile rootProject //this points to the root project object
}

```

during build of myJarProject I can clearly see that it never looks for jars inside /lib of root project.  
And it make sense becase the statement should be something like:

```
dependencies {
    include rootProject //this points to the root project object
}

```

since there is nothing to compile  
I will study the “dependency” bit to see other ways to refer to other project’s defined repositories

---

<div class="post-metadata">

**Author:** ![Chris\_Dore](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/chris_dore/32/7592_2.png) [@Chris\_Dore](https://discuss.gradle.org/u/Chris_Dore)\
**Post date:** [August 31, 2017, 2:04pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/8 "2017-08-31T14:04:59Z")

</div>

Do you really want/need a subproject just for the local libs? If not, put your libs in something like root/libs, then in the root build.gradle:

```
subprojects { // or allprojects if root needs the repo as well
    repositories {
        flatDir {
            dir rootProject.file( 'libs' )
        }
    }
}

```

Then in any of the projects, declare the dependencies as needed.

---

<div class="post-metadata">

**Author:** ![andreasb70](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/andreasb70/32/6418_2.png) [@andreasb70](https://discuss.gradle.org/u/andreasb70)\
**Post date:** [September 4, 2017, 9:01pm UTC](https://discuss.gradle.org/t/multi-project-make-existing-jar-available-as-dependencies/23935/9 "2017-09-04T21:01:06Z")

</div>

Hi Chris,  
thanks for your solution. The idea of a sub project was only based on the fact that I like to keep my root project clean.

But your solution works perfectly and I have now moved my jars to the root project.  
Thanks
