221

I have 2 different project build on mvn. I am trying to replace to Gradle.

Project 1 is an SDK, and project 2 is using that sdk (example).

In the time of maven it creates artifact using mvn install which adds the whole project into local repository.

I like to work in gradle like that. I like project 1 build.gradle need to post it as a gradle local repository and then example project need to use it.

In maven we do mvn install which adds a project artifact into .m2 folder but how to do in gradle so what i can add a project artefact's into the local repository.

Any way that I can do so?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
Rajmahendra
  • 3,092
  • 3
  • 31
  • 42
  • 1
    http://stackoverflow.com/questions/2572811/gradle-make-a-3rd-party-jar-available-to-local-gradle-repository – clacke May 07 '13 at 09:26

4 Answers4

201

sdk/build.gradle:

apply plugin: "maven"

group = "foo"
version = "1.0"

example/build.gradle:

repositories {
    mavenLocal()
}

dependencies {
    compile "foo:sdk:1.0"
}

$sdk> gradle install

$example> gradle build
the_storyteller
  • 2,335
  • 1
  • 26
  • 37
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 2
    apply plugin: "maven" and $sdk> gradle install this will install the sdk into .m2 right ? And mavenLocal() also gets info from .m2 and .gradle ? – Rajmahendra May 26 '11 at 10:01
  • 20
    `gradle install` publishes into the local Maven repo, and `mavenLocal()` makes sure to look there for dependencies. – Peter Niederwieser May 27 '11 at 05:31
  • 1
    Is Gradle not "phase-oriented" like Maven? IOW, if I do a `gradle publish` will it not also go through an `install` phase? – Chris F Jan 07 '20 at 21:44
  • @PeterNiederwieser Do I should consider that `apply plugin: "maven"` makes available the "install" task for Gradle, right? - in some way - it is how a "transitive" (if the term fits well here) task from Maven to Gradle, right?. Consider to update your answer. Thank You – Manuel Jordan Jul 06 '21 at 15:34
  • 9
    This is no long relevant in Gradle 7+ as the `maven` plugin has been removed. Instead you need to use the `maven-publish` plugin and then use `gradle publishToMavenLocal`. – phillipuniverse Aug 16 '21 at 18:23
136

You may be looking for:

gradle publishToMavenLocal

build.gradle:

plugins {
    // other plugins
    id 'maven-publish'
}


publishing {
    publications {
        maven(MavenPublication) {
            from components.java
        }
    }
}

See: Maven Publish Plugin

SGT Grumpy Pants
  • 4,118
  • 4
  • 42
  • 64
Miguel Reyes
  • 2,444
  • 1
  • 21
  • 11
  • 8
    It's working fine even with maven options: `gradle -Dmaven.repo.local=.m2/repository publishToMavenLocal` – sparse Jun 01 '19 at 04:24
  • 8
    how is this different from `gradle install` with the `maven` plugin? – Matthew Jun 18 '19 at 18:02
  • 4
    @Matthew the `maven` plugin has been removed in Gradle 7 – phillipuniverse Aug 16 '21 at 18:22
  • I found I had to add the publishing section, such as in https://stackoverflow.com/a/58062266/7224691, to get this to work. – Ben Sep 10 '22 at 08:57
  • This also did not work for me unless I added the publishing section. – SGT Grumpy Pants Sep 29 '22 at 18:51
  • Do we really need to add `from components.java`? Why is its use exactly? I didn't add it and it is still working for me. The only thing I added is : `plugins { .. id 'maven-publish' } publishing { publications { maven(MavenPublication) { // from components.java } } }` – dj_1993 May 20 '23 at 16:56
10

Check out Gradle's documentation on multi-project builds.

Here's an example, with some extra dependencies. Just call gradle install in the root folder, and all will be built and put to your local repo.

Folder structure:

root
+--> build.gradle
+--> settings.gradle
+--> sdk
|    +--> build.gradle
+--> example
     +--> build.gradle

root/build.gradle:

allprojects {
  apply plugin: 'java'
  apply plugin: 'maven'

  group = 'myGroup'
  version = '0.1-SNAPSHOT'
}

root/settings.gradle:

include 'sdk'
include 'example'

root/sdk/build.gradle:

dependencies {
  // just an example external dep.
  compile group:'commons-lang', name:'commons-lang', version:'2.3'
}

root/example/build.gradle:

dependencies {
  compile project(':sdk')
  compile group:'log4j', name:'log4j', version:'1.2.16'
}
csaba.sulyok
  • 1,880
  • 2
  • 13
  • 13
  • 7
    Is this ideal practice? What if he wants to create a new project, example2, which also relies on sdk? Now he has to put 2 unrelated projects under a root project just because they share a dependency? You would think this entire 'multi-project' setup would be 1 Git project as well. Again, is this bad for company / multi-developer workflows? – Anthony Chuinard Dec 09 '15 at 07:04
8

You need to publish your own library to your local repository. You can do that in the following way:

  1. Add maven-publish plugin:

    plugins {
        // your other plugins come here...
        id 'maven-publish'
    }
    
  2. Add the publishing section to your build file:

    publishing {
        publications {
            myCoolLibrary(MavenPublication) {
                from components.java
            }
        }
    }
    
  3. Run gradle build publishToMavenLocal

    Find more details in the documentation.

Stef
  • 28,728
  • 2
  • 24
  • 52
Sasha Shpota
  • 9,436
  • 14
  • 75
  • 148