Expanding on Alex Lipov's answer, and this goes beyond the original question in detail, but for anyone that asked themselves the question of the title:
So you want to use Gradle and its neat dependency management (via Maven), maybe publish your project A to some local repository, and then in another project B, pull it in.
Here a flatDir is certainly not what you want, because any dependencies from project A are not going to be known to project B.
What you want is a local maven repository. This does not require setting up a local server of any kind. It really is just another local directory, where your project A stores not just the jar, but also some extra files, e.g. for its dependencies.
Pointing Gradle to a local directory can be done like this:
// build.gradle.kts syntax:
repositories {
maven(uri("C:\\your\\path\\.m2\\repository"))
}
You may be tempted to use mavenLocal()
, since this is the default local maven repository (under your user profile, in .m2), but if want your files to reside within your directory of choice, mavenLocal is not the way.
You may also be tempted to use the maven.repo.local
system property to change your default local maven repository to your directory of choice, but this will dump contents of any other local projects you may have into your directory of choice.
Now, if you pull dependencies from any maven directory, you cannot simply dump your jars there, like you could with flatDir. So here's how to do it using Gradle.
How to put files into a local maven repository
First you need the plugin to enable maven publications
plugins {
`maven-publish`
}
Then in the publishing
extension in your build file, you can specify your repository again, similar to before
publishing {
repositories {
maven(uri("C:\\your\\path\\.m2\\repository")) {
name = "MyPath"
}
}
Unlike before, we also added a name to the repository. This is because the maven plugin will now create tasks for it, e.g. publishAllPublicationsToMyPathRepository
.
If you already have a maven publication for your project, and were only looking for a way to do so locally, you are done here.
If not, then next, still in the publishing
scope, here is an example
publications {
create<MavenPublication>(project.name) {
groupId = group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
}
}
}
This will create tasks named publish<project.name>PublicationTo...
, that will put your project A into the local directory, in the correct format.
If you examine the directory structure created, you will find groupId, artifactId and version, then the jar along with metadata files.
Should you use this
As others have pointed out, local repositories break the build anywhere principle.
However, if the alternative was to use flatDir, then this is probably preferable, and more along the lines of how Gradle should be used.
Maven repositories are expected to be immutable. You should generally not publish one version, then make changes to code and publish the same version again.
If you are using a snapshot version (e.g. your version string ending in -SNAPSHOT
), the maven plugin will automatically create a unique publication every time you run publish. This may create a lot of files, so beware of this.
When using mavenLocal
for publishing, the publications will have a maven-metadata-local.xml
instead of maven-metadata.xml
. They will not contain checksum files, and they will not create unique publications, even for snapshot versions. I tried using mavenLocal { url = uri("...") }
, but this also does also not result in true local publishing (Gradle 7.2).
If you really want the local publishing mode, a copy task grabbing it from the standard maven local directory may be the only way.