91

I am facing a problem when trying to install a generated jar into my local Maven Repository. The message error just show me 'task 'publish' is not found'

I am using this Gradle Script:

buildscript {
    ext {
        springBootVersion = '1.3.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'maven-publish'

jar {
    baseName = 'mongofoundry'
    version = '1.0.0'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7


repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-data-mongodb')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

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


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

Do you have some idea Why I am reading that error message? Thanks.

UPDATED

Running the command as @RaGe mentioned, solved the problem:

gradle publishToMavenLocal
krock
  • 28,904
  • 13
  • 79
  • 85
Erikson Murrugarra
  • 1,379
  • 3
  • 13
  • 22

5 Answers5

144

The correct task to publish artifacts to local maven is

gradle publishToMavenLocal
RaGe
  • 22,696
  • 11
  • 72
  • 104
  • 2
    This actually doesn't work the way I want. The artifact should have been placed in C:\Users\XXXX\.gradle\caches\modules-2\files-2.1\cdb-webservices-spring-boot-starter but it gets placed in C:\Users\XXXX\.m2\repository\cdb-webservices-spring-boot-starter\cdb-webservices-spring-boot-starter\ – Vinayak Dornala Mar 15 '19 at 18:02
  • 2
    @VinayakDornala See https://stackoverflow.com/questions/35460534/gradle-is-it-possible-to-publish-to-gradles-own-local-cache – Gab Mar 25 '19 at 20:33
  • 6
    I just wanted to add that I sometimes find it helpful to customize the version number when publishing directly from the command line -- you can do this by adding `-Pversion=`. IE `gradle publishToMavenLocal -Pversion=SNAP` – Sammaron Mar 25 '20 at 19:32
10

Check Maven locally

For developing and testing it is useful to check library locally

gradle settings for apply plugin: 'com.android.library' not apply plugin: 'java-library'(where you can use it by default)

apply plugin: 'maven-publish'

//simple settings
project.afterEvaluate {
    publishing {
        publications {
            library(MavenPublication) {
                //setGroupId groupId
                setGroupId "com.company"
                //setArtifactId artifactId
                setArtifactId "HelloWorld"
                version "1.1"

                artifact bundleDebugAar

/* add a dependency into generated .pom file
                pom.withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', 'com.company')
                    dependencyNode.appendNode('artifactId', 'HelloWorld-core')
                    dependencyNode.appendNode('version', '1.1')

                }
*/
            }
        }
    }
}

to run it using command line or find this command in Gradle tab

./gradlew publishToMavenLocal

Location

artefact will be added into .m2 folder

//Unix
~/.m2

//Windows
C:\Users\<username>\.m2

//For example
/Users/alex/.m2/repository/<library_path>/<version>/<name>.<extension>

build folder

<project_path>/build/outputs/<extension>

other repositories location

~/.gradle/caches/modules-2/files-2.1/<group_id>/<artifact_id>/<version>/<id>

//For example
/Users/alex/.gradle/caches/modules-2/files-2.1/com.company/HelloWorld/1.1/c84ac8bc425dcae087c8abbc9ecdc27fafbb664a

To use it add mavenLocal(). It is important to place it as a first item for prioritise it, which is useful for internal dependencies

buildscript {
    repositories {
        mavenLocal()
    }

allprojects {
    repositories {
        mavenLocal()
    }
}

and

dependencies {
    implementation 'com.company:HelloWorld:+'
}

*Also remember if you use a kind of shared.gradle file (via apply from) you should set path which is relevant to project.gradle (not shared.gradle)

[iOS CocoaPod local]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
5

This is how I did it with Kotlin DSL (build.gradle.kts) for my Android library:

plugins {
    id("maven-publish")
    // OR simply
    // `maven-publish`

    // ... (other plugins)
}

publishing {
    repositories {
        // Local repository which we can first publish in it to check artifacts
        maven {
            name = "LocalTestRepo"
            url = uri("file://${buildDir}/local-repository")
        }
    }
    publications {
        // ...
    }
}

You can create all the publications with the following command:

./gradlew publishAllPublicationsToLocalTestRepoRepository

Or just a single publication with this command:

./gradlew publishReleasePublicationToLocalTestRepoRepository

See Gradle documentations: Maven Publish Plugin for more information.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
4

Here is an alternative skeleton for Gradle 7.5.1 with Java 17

build.gradle

plugins {
    id 'org.gradle.java'
    id 'org.gradle.maven-publish'
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
    withJavadocJar()
    withSourcesJar()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'your-group'
            artifactId = 'your-artifact'
            version = "0.0.1"
            from components.java
        }
    }
    repositories {
        mavenLocal()
    }
}

Publishing

You can see more details on the publishing steps with --info

./gradlew --info publishToMavenLocal

Output Directory

Linux/macOS

/Users/<username>/.m2/repository/your-group/your-artifact/0.0.1

Windows

C:\Users\<username>\.m2\repository\your-group\your-artifact\0.0.1
rbento
  • 9,919
  • 3
  • 61
  • 61
  • Only with this step works: publishing { publications { mavenJava(MavenPublication) { groupId = 'your-group' artifactId = 'your-artifact' version = "0.0.1" from components.java } } ... Thank you! – Pavlo Chechehov Nov 24 '22 at 16:56
2

Add maven plugin to your project and then: gradle clean install

Volodymyr Masliy
  • 413
  • 4
  • 14
Ivan Rodrigues
  • 441
  • 4
  • 20