Gradle's publishToMavenLocal task is not executed correctly
Asked Answered
S

2

18

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?

Spicy answered 26/10, 2014 at 20:31 Comment(3)
Perhaps check the --info and --debug logs.Stratification
:food-orders-online-admin-business:publishToMavenLocal (Thread[main,5,main]) sta rted. :food-orders-online-admin-business:publishToMavenLocal Skipping task ':food-orders-online-admin-business:publishToMavenLocal' as it has no actions. :food-orders-online-admin-business:publishToMavenLocal UP-TO-DATE :food-orders-online-admin-business:publishToMavenLocal (Thread[main,5,main]) com pleted. Took 0.007 secs.Spicy
I already know that task is skipped. My question is why. I think that subprojects.findAll() doesn't return anything. Then how can I access all sub-projects? I can do it in every project separately, but it's repetitive and stupid if I can do this.Spicy
C
7

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
Cherub answered 6/1, 2015 at 12:31 Comment(8)
Thank you for such detailed and well explained answer. I think I will go with a third workaround in my case. I also hope that Gradle team will solve this issue soon.Spicy
Given how long it took me to track this bug down, I was very happy to be able to share the workarounds with someone.Cherub
Maybe you should raise it in the issue tracker rather than on the forum.Merl
Hi Mirko, I would love to, but as far as I can see the only way to raise an issue is through the forum, if you know otherwise please provide me the link where I can sign up to add bugs to the issue tracker. ThanksCherub
@Karl The text on the page you linked linked says "If you are looking to report a problem with Gradle, please use discuss.gradle.org to create a "bug" post" and the link in my post above is to such a bug report, that I filed almost 2 years ago.Cherub
@Cherub I oversaw that. I'm right, though, no one will look into a forum for old issues. Why make things smart/let contributors use your issue tracker if you can do it this way?!?Autonomous
@Karl I don't know! I have never tried to sign up for the tracker since the advice given by gradle is explicitly not to use it. We actually stopped using gradle about a year ago because of this and other issues. Please feel free to copy this my post into the tracker.Cherub
I can't do that neither. They seriously don't allow write access for registered users. The fact that they advise it doesn't make it a smart idea.Autonomous
S
61

By adapting answer from here, it works for me.

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        mavenLocal()
    }
}
Sweepstakes answered 23/3, 2017 at 13:22 Comment(4)
Ref: docs.gradle.org/current/userguide/publishing_maven.htmlBeirut
Is the mavenLocal() bit required? The link provided by @Beirut states: "You do not need to have mavenLocal() in your publishing.repositories section."Nocturn
@M.Justin the repositories.mavenLocal part is not required. My setup doesn't have it and my artefacts still get published to my local .m2.Myology
this made my day. It worked for me. There where many other ways suggested over various posts but only this worked for me. Thanks :)Saccharose
C
7

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
Cherub answered 6/1, 2015 at 12:31 Comment(8)
Thank you for such detailed and well explained answer. I think I will go with a third workaround in my case. I also hope that Gradle team will solve this issue soon.Spicy
Given how long it took me to track this bug down, I was very happy to be able to share the workarounds with someone.Cherub
Maybe you should raise it in the issue tracker rather than on the forum.Merl
Hi Mirko, I would love to, but as far as I can see the only way to raise an issue is through the forum, if you know otherwise please provide me the link where I can sign up to add bugs to the issue tracker. ThanksCherub
@Karl The text on the page you linked linked says "If you are looking to report a problem with Gradle, please use discuss.gradle.org to create a "bug" post" and the link in my post above is to such a bug report, that I filed almost 2 years ago.Cherub
@Cherub I oversaw that. I'm right, though, no one will look into a forum for old issues. Why make things smart/let contributors use your issue tracker if you can do it this way?!?Autonomous
@Karl I don't know! I have never tried to sign up for the tracker since the advice given by gradle is explicitly not to use it. We actually stopped using gradle about a year ago because of this and other issues. Please feel free to copy this my post into the tracker.Cherub
I can't do that neither. They seriously don't allow write access for registered users. The fact that they advise it doesn't make it a smart idea.Autonomous

© 2022 - 2024 — McMap. All rights reserved.