Rade Milovic
Rade Milovic

Reputation: 1015

Gradle's publishToMavenLocal task is not executed correctly

I'm trying to create a gradle based multi-module project. There is also an project that contains different gradle scripts that enables pluggable build configurations. One of those scripts is for publishing artifacts to maven repository. This is the content of that script:

apply plugin: 'maven-publish'

configure(subprojects.findAll()) {
    if (it.name.endsWith('web')) {
        publishing {
            publications {
                mavenWeb(MavenPublication) {
                    from components.web
                }
            }
        }
    } else {
        publishing {
            publications {
                mavenJava(MavenPublication) {
                    from components.java
                }
            }
        }
    }
}

build.dependsOn publishToMavenLocal

This script is included in the build gradle file of other project.

apply from: '../../food-orders-online-main/artifact-publish.gradle'

When I run build task it always shows that publishToMavenLocal task is up to date and I cannot find the artifacts in the local repository. Am I doing something wrong?

Upvotes: 19

Views: 42633

Answers (2)

heading
heading

Reputation: 721

By adapting answer from here, it works for me.

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

Upvotes: 61

PiersyP
PiersyP

Reputation: 5233

I think this could be a manifestation of a bug with gradle, that modules can lose the publishMavenJavaPublicationToMavenLocal task when they are depended upon in a certain way.

If gradle determines that there is no publishMavenJavaPublicationToMavenLocal task for a module then the publishToMavenLocal task will always report that it is up to date.

The specific case I have found occurs in a multimodule setup, with multiple levels of nested modules. It can be summarised as follows, where shared:domain loses its publishMavenJavaPublicationToMavenLocal when depended upon by A

root
root gradle build file
->A
  own gradle build file with dependency on shared:domain
-> shared
    gradle build file for shared modules
     -> shared:domain
     -> shared:B

I have created a small example project demonstrating this behaviour available here - https://github.com/piersy/Gradle2MavenPublishBug

I have also logged a bug with gradle here - http://forums.gradle.org/gradle/topics/the-publishmavenjavapublicationtomavenlocal-task-disappears-from-a-project-when-that-project-is-depended-upon-in-a-specific-way

For now the workarounds I have found are to

  1. Remove A's dependency on shared:domain
  2. Make A a submodule with its configuration specified in its parent module build file
  3. Give shared:domain its own gradle build file

Upvotes: 7

Related Questions