Plugins config block in a custom gradle plugin

Hi there,

I am using the release plugin from townsfolk: Release Plugin https://github.com/researchgate/gradle-release

Within it’s documentation it states to use:
plugins { id 'net.researchgate.release' version '2.0.2' }

if using gradle 2.1 or later. This works fine in the build.gradle script.

The problem is I want to use this from within a binary plugin. The plugin is to replace Maven and I need to setup the release configuration/tasks. I have the normal apply plugin: 'XXX@ code for other common plugins e.g. Java, War, PMD etc. I just can’t figure out how to set the above from within the plugin.

Any help appreciated.

MARK

CIAO

You can use the “old” syntax still. That’s what I would do in your plugin. So use:

project.apply(plugin: 'net.researchgate.release')

And make sure jcenter() is added to your buildscript’s repositories. This has to be done in the build script and not the plugin.

Thanks for you quick response.

I tried what you said e.g.

class EdinaPlugin implements Plugin<Project> {

  @Override
  void apply(Project project) {
      project.apply(plugin: 'net.researchgate.release')
  }
}

I added the following to my plugins build.gradle file:

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'net.researchgate:gradle-release:2.0.2'
  }
}

When I try to install the project to my local maven repository it fails the tests. The tests do pass without the changes to the plugin. The error message from the failed tests is the same always, failure to apply my custom plugin.

From your original reply it appears I should really put the buildscript block in the project using my plugin, but I don’t even get that far, that is why I put it in the plugin’s build.gradle.

I find it difficult to debug problems with the plugin because it always fails to apply the plugin. What would be the best way to debug this?

Can you try running with --stacktrace?

The tests fail in your plugin’s project? Do you have a compile/runtime dependency on ‘net.researchgate:gradle-release:2.0.2’?

I have ran again with --stacktrace and --info, looking at the stacktrace created, the release plugin is not being applied as it cannot find it see the following stacktrace except.

Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'net.researchgate.release']
	at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.applyPlugin(DefaultObjectConfigurationAction.java:117)
	at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.access$200(DefaultObjectConfigurationAction.java:36)
	at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction$3.run(DefaultObjectConfigurationAction.java:80)
	at org.gradle.api.internal.plugins.DefaultObjectConfigurationAction.execute(DefaultObjectConfigurationAction.java:131)
	at org.gradle.api.internal.project.AbstractPluginAware.apply(AbstractPluginAware.java:37)
	at org.gradle.api.Project$apply.call(Unknown Source)
	at edina.shared.gradle.EdinaPlugin.apply(EdinaPlugin.groovy:31)
	at edina.shared.gradle.EdinaPlugin.apply(EdinaPlugin.groovy)

Below is my actual plugin (I’ve removed code not pertinent to the current problem e.g. applying static code analysis plugins.

Plugin Source

package edina.shared.gradle

import org.gradle.api.Plugin
import org.gradle.api.Project


class EdinaPlugin implements Plugin<Project> {

  @Override
  void apply(Project project) {
    project.buildscript {
      repositories {
        jcenter()
      }
      dependencies {
        classpath 'net.researchgate:gradle-release:2.0.2'
      }
    }

    project.apply(plugin: 'net.researchgate.release')
  }
}

build.gradle

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'net.researchgate:gradle-release:2.0.2'
  }
}

// plugins {
//   id 'net.researchgate.release' version '2.0.2'
// }

group='edina.shared'
description="""Edina specific plugin to set defaults for all projects."""


configurations {
    deployerJars
}
 
repositories {
  jcenter()
  mavenLocal()
}



// Apply necessary plugins.
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'pmd'
apply plugin: 'net.researchgate.release'

dependencies {
  compile gradleApi()
  compile localGroovy()

  deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
}
 
release {
  // Prevent releases from master branch only.
  // This can be configured to specify releases from a particular branch only.
  requireBranch = ''
}

uploadArchives {
  repositories.mavenDeployer {
      configuration = configurations.deployerJars
      repository(url: edinaRepository) {
          authentication(userName: mavenUsername, password: mavenPassword)
      }
  }
}

// Ensure maven publish task is ran after java test task.
uploadArchives.dependsOn(test)
install.dependsOn(test)

createReleaseTag.dependsOn uploadArchives

To test your plugin, you need to include ‘net.researchgate:gradle-release:2.0.2’ on the classpath for the tests.

Try:

dependencies {
   compile 'net.researchgate:gradle-release:2.0.2'
}

I’ve added the compile time dependency to a number of places but each has the same result i.e. cannot find ‘net.researchgate:gradle-release:2.0.2’ plugin. I’ve pushed my code to my github page for anyone interest in helping me solve this problem.

Ah, it’s trying to apply the gradle-release plugin but an exception occurs within it. It’s looking for an SCM directory:

Caused by: org.gradle.api.GradleException: Unsupported SCM system, no .svn, .bzr, .git, or .hg found in [some projectDir] or its parent directories.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
	at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
	at net.researchgate.release.ReleasePlugin.applyScmPlugin(ReleasePlugin.groovy:259)
	at net.researchgate.release.ReleasePlugin.apply(ReleasePlugin.groovy:35)
	at net.researchgate.release.ReleasePlugin.apply(ReleasePlugin.groovy)

Try putting this in your setup method for your EdinaPluginTest.

project.file(".git").mkdirs()

.hg, .svn would work too, I think. I don’t know enough about the plugin if it makes sense to treat that as a fatal error or just degrade the functionality. You could raise an issue on their GitHub.

That’s solved it, thanks very much for your help, can’t believe I missed that when I looked at the stacktrace myself, still, live and learn.

I’ll raise an issue on their github issue tracker as well as I doubt they intended this.