Publish Java artifact to Maven Local with Gradle

MavenGradlePublishArtifactMaven Publish

Maven Problem Overview


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.

Maven Solutions


Solution 1 - Maven

The correct task to publish artifacts to local maven is

gradle publishToMavenLocal

Solution 2 - Maven

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]

Solution 3 - Maven

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

Solution 4 - Maven

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

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

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErikson MurrugarraView Question on Stackoverflow
Solution 1 - MavenRaGeView Answer on Stackoverflow
Solution 2 - MavenyoAlex5View Answer on Stackoverflow
Solution 3 - MavenIvan RodriguesView Answer on Stackoverflow
Solution 4 - MavenMahozadView Answer on Stackoverflow