Share constant between build.gradle and plugin

Hi! I’m writing a gradle plugin that adds a dependency to a project defined in the same repository, and I’m trying to figure out a way to share a constant between the dependency and the plugin code.

plugin/build.gradle:

class MyPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.configurations.create("conf")
        project.dependencies.add("conf", "org.my.project:dependency:XXX")
    }
}

dependency/build.gradle:

apply plugin: 'maven-publish'

publishing {
    publications {
         maven(MavenPublication) {
            version = 'XXX'
            ...
         }
    }
}

Is there a way I can define the version XXX somewhere in a constant and refer to it in both MyPlugin.apply and plugin/build.gradle so that I don’t have to update both every time? I tried defining a system property in settings.gradle but that is not visible in MyPlugin.apply.

You could use extra properties and only need to be a bit careful when to access them. Besides that, you could entirely move the configuration of the publication into your plugin and only need to manage the version within your plugin then. The following is one example I did for Ivy a few days ago:

/**
 * Add a publication for our artifacts.
 */
private void configPub()
{
	Project					project		= this.project;
	PublishingExtension		publishing	= this.getPublishing();
	PublicationContainer	pubCont		= publishing.getPublications();

	pubCont.create(	"coreWithJavadocAndDeps",
					IvyPublication.class,
					new Action<IvyPublication>()
	{
		@Override
		public void execute(IvyPublication pub)
		{
			JavadocToJar		task		= JavadocToJar.findSelf(project);
			Map<String, Object>	artifact	= ImmutableMap.of(	"source",	task,
																"type",		"javadoc");

			pub.from(project.getComponents().findByName("java"));
			pub.artifact(artifact);
			CustomIvyPublish.this.configPubArtifacts(pub);
		}
	});
}