# Configure publishing multiple times

**URL:** <https://discuss.gradle.org/t/configure-publishing-multiple-times/9755>\
**Category:** Help/Discuss\
**Created:** [May 23, 2015, 12:43am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755 "2015-05-23T00:43:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sohrab\_Hosseini](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Sohrab\_Hosseini](https://discuss.gradle.org/u/Sohrab_Hosseini)\
**Post date:** [May 23, 2015, 12:43am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/1 "2015-05-23T00:43:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 23, 2015, 5:48am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/2 "2015-05-23T05:48:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sohrab\_Hosseini](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Sohrab\_Hosseini](https://discuss.gradle.org/u/Sohrab_Hosseini)\
**Post date:** [May 24, 2015, 1:10pm UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/3 "2015-05-24T13:10:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 24, 2015, 2:08pm UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/4 "2015-05-24T14:08:07Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Sohrab\_Hosseini](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Sohrab\_Hosseini](https://discuss.gradle.org/u/Sohrab_Hosseini)\
**Post date:** [May 24, 2015, 2:14pm UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/5 "2015-05-24T14:14:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 24, 2015, 2:18pm UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/6 "2015-05-24T14:18:12Z")

</div>

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 😄

---

<div class="post-metadata">

**Author:** ![Sohrab\_Hosseini](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Sohrab\_Hosseini](https://discuss.gradle.org/u/Sohrab_Hosseini)\
**Post date:** [May 25, 2015, 9:59am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/7 "2015-05-25T09:59:51Z")

</div>

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.
```

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 25, 2015, 10:53am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/8 "2015-05-25T10:53:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sohrab\_Hosseini](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Sohrab\_Hosseini](https://discuss.gradle.org/u/Sohrab_Hosseini)\
**Post date:** [May 27, 2015, 10:02am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/9 "2015-05-27T10:02:19Z")

</div>

> [@Sohrab\_Hosseini](#):
>
> project.repositories {  
> maven {  
> url “${project.properties.mavenRepoUrl}/repo”  
> }  
> }

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

---

<div class="post-metadata">

**Author:** ![Sohrab\_Hosseini](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@Sohrab\_Hosseini](https://discuss.gradle.org/u/Sohrab_Hosseini)\
**Post date:** [May 28, 2015, 5:29am UTC](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/10 "2015-05-28T05:29:49Z")

</div>

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

> [@Strange error in Publishing when using properties in Repositories](https://discuss.gradle.org/t/strange-error-in-publishing-when-using-properties-in-repositories/9836):
>
> I am consistently getting a strange error in publishing block when using properties in repositories block. The details are available here: [Configure publishing multiple times](https://discuss.gradle.org/t/configure-publishing-multiple-times/9755/7) I am not even entirely sure why this is happening but the behaviour seems like a bug (unless I have made an obvious mistake somewhere).
