Configure publishing multiple times

This has been driving me nuts. So wondering if anyone can spot why it’s happening.

Here is an abridged version of my binary plugins:

class MyBasePlugin implements Plugin<Project> {
     void apply(Project project) {
          project.apply plugin: MavenPublishPlugin
          project.publishing {
               repositories { ... }
          }
     }
}

class LibraryPlugin implements Plugin<Project> {	
     public void apply(Project project) {
          project.apply plugin: MyBasePlugin
          project.publishing {
               publications { ... }
          }
     }
}

When I apply LibraryPlugin in a build.gradle file, build fails on project.publishing line in MyBasePlugin (never actually execute the second publishing in LibraryPlugin).

> Failed to apply plugin [id 'example.library']
   > Failed to apply plugin [class 'com.example.MyBasePlugin']
      > Cannot configure the 'publishing' extension after it has been accessed.

There are no other publishing before this and even if there were, I have seen invoking project.publishing { } multiple times across plugins work before.

This just doesn’t make sense to me. Can anyone see why this would happen? Or is there a hook I can add some logging to so I can see who is referencing it first?

1 Like

Try something like project.getExtensions().configure(PublishingExtension, new ClosureBackedAction({ repositories.add(repo) }))

The issue doesn’t go away. I am still configuring the same extension even when I look it up by class, instead of name.

I cannot reproduce your problem with Gradle 2.4 and the information you provided.

class MyBasePlugin implements Plugin<Project> {
	void apply(Project project) {
		project.apply plugin: MavenPublishPlugin
		project.publishing {
			repositories { maven{ name 'aRepo' } }
		}
	}
}

class LibraryPlugin implements Plugin<Project> {
	public void apply(Project project) {
		project.apply plugin: MyBasePlugin
		project.publishing {
			publications {
				aPub(MavenPublication){
				}
			}
		}
	}
}
apply plugin: LibraryPlugin

When executing the above build.gradle with gradlew tasks, I correclty get the task publishAPubPublicationToMavenLocal

That’s the frustrating part. I cannot reproduce it neither when I chuck everything in the same build.gradle file. It is only happening when they are spread out into their own classes in a binary plugin that I apply.

There is literally nothing between when MavenPublishPlugin is applied and the first instance of project.publishing {} so it is beyond me what is accessing it in that interval.

This is why I was asking if there is a way for me to find the culprit, for example through some debug logging. I think I may have to attach a debugger to this to find out what the heck is going on.

Are you sure you didn’t simplify too much your scenario ?
Maybe there is a problem with the way you publish your plugins ?
Beside these two points, there’s not much I can think of, except a Core Dev intervention :smile:

Here is the minimum build.gradle file that replicates the issue. Am I doing something really silly here?

apply plugin: LibraryPlugin

class MyBasePlugin implements Plugin<Project> {

	@Override
	void apply(Project project) {
		project.apply plugin: 'maven-publish'
		
		project.repositories {
			maven {
				url "${project.properties.mavenRepoUrl}/repo"
			}
		}
		
		project.publishing {
			repositories {
				maven {
					url "${project.properties.mavenRepoUrl}/libs-release-local"
					credentials {
							username project.properties.mavenRepoUsername
							password project.properties.mavenRepoPassword
					}
				}
			}
		}
	}
}

class LibraryPlugin implements Plugin<Project> {
	
	final static String LIBRARY_REPOSITORY = 'libs-release-local'
	
	@Override
	public void apply(Project project) {
		project.apply plugin: MyBasePlugin
		project.apply plugin: 'groovy'
		
		project.dependencies {
			compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.3.7', classifier: 'indy'
		}
		
		project.publishing {
			publications {
				mavenLibrary(MavenPublication) {
					from project.components.java
				}
			}
		}
	}
	
}

The error I get when I run gradle tasks:

* What went wrong:
A problem occurred evaluating root project 'gradle'.
> Failed to apply plugin [class 'LibraryPlugin']
   > Failed to apply plugin [class 'MyBasePlugin']
      > Cannot configure the 'publishing' extension after it has been accessed.

yes, there aren’t many differences indeed …
I’m out of ideas…
maybe you can add the --debug --stacktrace output to really pinpoint the problem.

The difference between my working example and yours, concerning the project.publishing.repositories section, is that you define an url and some credentials, where I only define the maven repo name.

For some insane reason, when I change that to url 'repo' in repositories, it starts working again. I am so confused!

Turns out it has to do with usage of project.properties which forces the project to be evaluated. Need to use project.property() instead.