Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish Java artifact to Maven Local with Gradle

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.
like image 273
Erikson Murrugarra Avatar asked Jan 29 '16 21:01

Erikson Murrugarra


People also ask

Can Gradle create Maven artifact?

Publish Maven artifacts using Gradle command-line tool To publish artifacts of a Gradle project, you should configure artifact properties, reference a repository, and specify authentication credentials. This configuration is performed in the build. gradle file.

Does Gradle use Maven local repository?

Bookmark this question. Show activity on this post.

How does Gradle publish work?

Gradle automatically generates publishing tasks for all possible combinations of publication and repository, allowing you to publish any artifact to any repository.


Video Answer


3 Answers

The correct task to publish artifacts to local maven is

gradle publishToMavenLocal
like image 57
RaGe Avatar answered Oct 07 '22 07:10

RaGe


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]

like image 10
yoAlex5 Avatar answered Oct 07 '22 06:10

yoAlex5


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

like image 2
Ivan Rodrigues Avatar answered Oct 07 '22 06:10

Ivan Rodrigues