smeeb
smeeb

Reputation: 29557

Publishing locally with Gradle not working

I have the following directory structure:

myapp/
    src/main/resources/
        <lots of code>
    build.gradle

With the following build.gradle:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

group = 'net.myuser'

repositories {
    jcenter()
}

dependencies {
    compile(
        <dependencies here>
    )
}

jar {
    baseName = 'myapp'
}

task writePom << {
    pom {
        project {
            groupId group
            artifactId 'myapp'
            version version

            inceptionYear '2015'
            licenses {
                license {
                    name 'myapp'
                    distribution 'Blah blah blah'
                }
            }
        }
    }.writeTo("build/libs/pom.xml")
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

artifacts {
    archives(file("${buildDir}/libs/myapp-${version}.jar")) {
        name "myapp"
        classifier ""
    }
}

When I do:

./gradlew clean build writePom install -Pversion=0.1.0

I get two problems:

  1. Inside the generated build/libs/pom.xml the groupId is showing as null; and
  2. I cannot find a net.myuser directory under ~/.gradle/caches/modules-2, which tells me install is not working

So I ask: What do I need to change so that groupId isn't null, and how do I get install publishing all of the following:

Upvotes: 0

Views: 4517

Answers (1)

Thomas K.
Thomas K.

Reputation: 6780

As per comment, use groupId project.group instead of groupId group in order to set the <groupId> properly.


Regarding the install task, please have a look at Gradle Maven Plugin documentation:

Installs the associated artifacts to the local Maven cache, including Maven metadata generation.

By default, the local Maven cache is located in ~/.m2/repository, thus you are looking at the wrong location. The install task does not tamper with ~/.gradle/caches/modules-2 which is (as the name already implies) only a cache for resolved dependencies.


By using the Maven plugin, Gradle already creates a POM file for you. So please check if you really need a custom writePom task.

Also, the main artifact is installed automatically, so this might be redundant:

artifacts {
    archives(file("${buildDir}/libs/myapp-${version}.jar")) {
        name "myapp"
        classifier ""
    }
}

Upvotes: 1

Related Questions