Multi project, make existing jar available as dependencies

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

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

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

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

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.

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?

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

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

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.

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