Publish Java artifact to Maven Local with Gradle
Asked Answered
H

5

98

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
Hanafee answered 29/1, 2016 at 21:49 Comment(8)
Try gradle publishToMavenLocalIllegality
in early days 'gradle install' worked just fine...Teucer
@Teucer that is with the now deprecated maven plugin. maven-publish changes a few things around.Illegality
@Illegality thanks, I'll take a lookTeucer
You could add task install(dependsOn: publishToMavenLocal) if you have particularly strong habits.Advocaat
Thank you everybody, I solved the problem running the task publishToMavenLocal as @Rage mentioned, but using the dependency way as EricWendelin mentioned, it is a nice way to do, So I have implemented the way he advice. Thank you guys. =)Hanafee
Why not use apply plugin: 'maven' and gradle install?Conjunct
@Illegality Are there any features that Gradle ISN'T planning on deprecating? That's what I'd like to know... :PLethia
I
152

The correct task to publish artifacts to local maven is

gradle publishToMavenLocal
Illegality answered 1/2, 2016 at 16:27 Comment(3)
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\Arthromere
@VinayakDornala See stackoverflow.com/questions/35460534/…Invalidity
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=<whatever>. IE gradle publishToMavenLocal -Pversion=SNAPVenturous
G
11

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]

Grous answered 15/1, 2021 at 14:43 Comment(0)
S
7

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
Scrawly answered 5/11, 2022 at 11:23 Comment(1)
Only with this step works: publishing { publications { mavenJava(MavenPublication) { groupId = 'your-group' artifactId = 'your-artifact' version = "0.0.1" from components.java } } ... Thank you!Lawford
K
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.

Kirkcudbright answered 18/2, 2022 at 16:29 Comment(0)
K
2

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

Kolva answered 29/6, 2020 at 22:27 Comment(2)
do you have to install a plugin to get "install"Huffman
No. You could do what @Eric Wendelin suggested in one of the comments to the question.Downtrodden

© 2022 - 2024 — McMap. All rights reserved.