Adding a plugin with dependencies of a specific maven repository

Hi,

I have to make a gradle plugin to use our own builder. This builder have some “common” dependencies (from jcenter) and some other from our own m2 public repository (https://m2.convertigo.com).

I tested my plugin by publishing to maven local. In an another project, I have to declare 3 repositories in order to get it work:

buildscript {
    repositories {
        jcenter()
        maven {
            url "https://m2.convertigo.com"
        }
        mavenLocal()
    }
    dependencies {
        classpath 'org.samples.greeting:greeting-plugin-example:0.1'
    }
}

apply plugin: 'org.samples.greeting'

Next, I read the doc about publishing to the plugin-portal and I though we can publish / remove plugin directly and I didn’t think there is an “approuval” step and no button to remove it.

Can you remove my test please : com.github.nicolas-albert.greeting ?

I have to know if once published, extra dependencies will be pulled from “https://m2.convertigo.com” or the user have to add this repository itself ? (like the method without plugin-portal)

Also, do I publish the plugin with my personnal account for my compagny or do I have to create “corporate” account to do this (if someone else have to publish a release) ?

Thanks !

Hi Nicolas,

I’ve removed your test plugin as requested. It doesn’t look like there’s a way to delete a new, unapproved plugin yet.

The build script you posted here will get dependencies only for building the plugin itself and does not declare that consumers of the plugin should have that repository declared. The user would have to add the repository themselves. However, there’s a way to add repositories to a Project in the Plugin<Project> class.

We recommend creating a corporate account for publishing so that access is shared. Currently there’s not a mechanism to allow multiple owners for a given plugin.

Cheers,
Eric

Thanks Eric,

Do you have an url or a github repo with the good pratice about adding a repository with the Plugin<Project> ?

I suppose this is something like that but I didn’t find a complet sample:

public class GreetingPlugin implements Plugin<Project> {
    public void apply(Project project) {
        project.getBuildscript().getRepositories().add(??);

I’ll create an account for my company.

Thanks again !

Nicolas

Nearly correct.

You want project.repositories.add(...) in your apply method.

Cheers,
Eric

I don’t know what to put in place of ..., how can I transform DSL

maven {
    url "https://m2.convertigo.com"
}

to a java object ?

I found the DefaultMavenArtifactRepository class but the constructors aren’t simple to complete >.<

new DefaultMavenArtifactRepository(fileResolver, transportFactory, locallyAvailableResourceFinder, instantiatorFactory, artifactFileStore, pomParser, metadataParser, authenticationContainer, moduleIdentifierFactory, resourcesFileStore, fileResourceRepository, featurePreviews, metadataFactory, isolatableFactory, objectFactory)
new DefaultMavenArtifactRepository(describer, fileResolver, transportFactory, locallyAvailableResourceFinder, instantiatorFactory, artifactFileStore, pomParser, metadataParser, authenticationContainer, moduleIdentifierFactory, resourcesFileStore, fileResourceRepository, featurePreviews, metadataFactory, isolatableFactory, objectFactory)

I think I have missed something.

I think I have the right syntax but it’s still fail if I remove my repo from the testing app buildscript section.

public class GreetingPlugin implements Plugin<Project> {
    public void apply(Project project) {
        RepositoryHandler repo = project.getRepositories();
        repo.maven((maven) -> {
	        maven.setUrl(URI.create("https://m2.convertigo.com"));
        });
        repo.jcenter();

The gradle of the testing app search for my plugin dependencies in my mavenLocal repo and ignore repositories declared in the apply.

I also tested this without success:

repo.add(repo.maven((maven) -> {
    maven.setUrl(URI.create("https://m2.convertigo.com"));
}));

Thanks for your support.

Here a sample of my gradle for the plugin:

plugins {
    id 'java-gradle-plugin'
    id 'maven-publish'
    id 'com.gradle.plugin-publish' version '0.10.0'
}

apply plugin: 'java'

group = 'org.samples.greeting'
version = '0.1'

repositories {
    jcenter()
    maven {
        url "https://m2.convertigo.com"
    }
}

dependencies {
    compile 'commons-lang:commons-lang:[2.6,)'
    compile 'com.convertigo.lib:convertigo-engine-api:7.6.0-SNAPSHOT'
}

gradlePlugin {

It compile, publish locally, wonderful.

But when I use it, the apply isn’t called because the dependencies are unknow for my maven local.

So I change compile to compileOnly and then the apply is called. I can add repositories but I cannot add dependencies : Cannot change dependencies of configuration ':classpath' after it has been resolved.

I still not know how a plugin can use dependencies of a custom repository and I didn’t find sample or documentation about this.

Some help will be appreciate.

So lets summarize:
You want to create a public Gradle Plugin which will add your custom repo so that a user can define a dependencies from that repo, right?

If so, then you code above “works”.
As @eriwen mentioned above you only have to add the repo via project.repositories.add (or like you do with repo.maven(...).

Your problem is that your plugin define a dependency which is inside the custom repo.
So you have to imagine that Gradle do the following:

  • Apply your plugin
  • Wants to resolve its dependencies -> Error
  • Call apply after resolving of the dependencies was successfully.

How could you resolve this?
Well, when your plugin really needs the convertigo-engine-api as dependency than your clients have to declare your custom repo. Otherwise it is “not really possible” - At least from what I know about Gradle :smile:.

Another thing is when you want to add a dependency in your Plugin for your “users”.
In other words, when you want to provide a Plugin which will add some dependencies for the current project.
Then you can simply call the code above (to add the repo) and then add the dependency via project.dependencies.add().
Then Gradle should know how to resolve the dependency from the custom repo…

I tried many many things 1 month ago without success and I went forward.
In all case, we provide a gradle file in the template of our new projects and it use the “old” syntax, but does the job:

buildscript {
	repositories {
		mavenLocal()
		maven {
			url "https://m2.convertigo.com"
		}
		jcenter()
	}
	dependencies {
		classpath 'com.convertigo:gradle-plugin:((CURRENT_VERSION))'
	}
}

apply plugin: 'convertigo'

Thanks for your answer.