Publishing artifacts to a local Maven repository can be useful for sharing libraries within an organization or for offline development. In this tutorial, we’ll learn how to publish artifacts to a local Maven repository using Gradle, a popular build tool for Java projects.
TL;DR
To Publish, just use this Gradle command:
gradle publishToMavenLocal
To read from LocalRepository just add this to your dependencies block in Gradle:
allprojects { repositories { mavenLocal() } }
Prerequisites
Before getting started, make sure you have the following prerequisites:
- A Java project with a Gradle build file. If you don’t have a Java project yet, you can create a new one.
- Gradle installed on your machine. If you don’t have Gradle installed, you can follow these instructions.
Setting up the local Maven repository
To publish artifacts to a local Maven repository, you’ll need to create a local Maven repository first.
Normally you would already have this configured so you can skip this part.
You can create a local Maven repository by following these steps:
- Open a terminal and navigate to the directory where you want to create the repository.
- Run the following command to create the repository:
mkdir maven-repository
- Navigate to the repository directory:
cd maven-repository
- Run the following command to initialize the repository:
mvn deploy:deploy-file -Durl=file://$(pwd) -Dfile=<path-to-artifact> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Replace <path-to-artifact>
, <group-id>
, <artifact-id>
, <version>
, and <packaging>
with the appropriate values for your artifact.
Publishing artifacts with Gradle
You can skip this part if you just want to publish in the default repository.
Now that you have a local Maven repository set up, you can use Gradle to publish artifacts to it. To do so, you’ll need to add the maven-publish
plugin to your Gradle build file and configure the publishing
block.
Here’s an example of how to do this in your build.gradle
file:
plugins {
id 'maven-publish'
}
publishing {
repositories {
maven {
url 'file:///path/to/maven-repository'
}
}
}
Replace /path/to/maven-repository
with the actual path to your local Maven repository.
Once you’ve added the maven-publish
plugin and configured the publishing
block, you can use the publishToMavenLocal
task to publish your artifacts to the local Maven repository.
task publishToMavenLocal(type: PublishToMavenLocal) {
from components.java
}
The components.java
property refers to the components that make up your Java project, such as the JAR file and its dependencies.
To publish your artifacts, run the following command in your terminal:
./gradlew publishTo
or
gradle publishToMavenLocal
To use your local dependencies from another local service:
allprojects {
repositories {
mavenLocal()
}
}
Sources:
https://proandroiddev.com/tip-work-with-third-party-projects-locally-with-gradle-961d6c9efb02
https://docs.gradle.org/current/userguide/publishing_maven.html#publishing_maven:install
Thx