Configuring mavenDeployer from Java


(Aurimas) #1

Maven plugin has nice groovy DSL to easily set up a publish task. For example you could do something like the following:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/tmp/myRepo/")
            pom.project {
                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution 'repo'
                    }
                }
            }
        }
    }
}

Our code base has a number of project that have same configuration for mavenDeployer and so I am trying to create a Gradle Plugin to automatically set this up for each project. As a groovy plugin groovy I was able to do the following:

Upload uploadTask = (Upload) project.getTasks().getByName("uploadArchives");
project.afterEvaluate {
    uploadTask.repositories {
        mavenDeployer {
            repository(url: project.uri(project.rootProject.repoOut))
        }
    };
    uploadTask.getRepositories().withType(MavenDeployer.class, new Action<MavenDeployer>() {
        @Override
        public void execute(MavenDeployer mavenDeployer) {
            mavenDeployer.getPom().project {
                name myExtension.getName()
                description myExtension.getDescription()

                licenses {
                    license {
                        name 'The Apache Software License, Version 2.0'
                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution 'repo'
                    }
                }
            }
        }
    });
}

I was curious how would one achieve this from Java as I prefer Java to be the language used for the plugin.

I got started with the following, but then I got stuck:

Upload uploadTask = (Upload) project.getTasks().getByName("uploadArchives");
project.afterEvaluate(project1 -> {
    MavenDeployer deployer =
            (MavenDeployer) uploadTask.getRepositories().getByName("mavenDeployer");

    deployer.setRepository(/* How do I set uri!? */);

    MavenPom pom = deployer.getPom();
    pom.setModel(/*... MavenModel is not in the classpath.. */);
});

The first issue seems that repository method on MavenDeployer is internal and the RemoteRepository class is not on the classpath for some reason either.

Next, it seems that there is no easy way to set up pom without using Closure which is cumbersome to create from Java.

Thoughts?


(Aurimas) #2

Any suggestions? Am I looking at this wrong or should I just file an FR on the issue tracker?


(Mark Vieira) #3

It’s added as a convention. So you should be able to get it at like so:

((HasConvention) upload).getConvention().getPlugin(MavenRepositoryHandlerConvention.class).mavenDeployer()

(Aurimas) #4

HasConvention is an internal class :frowning:

Simply getting mavenDeployer does not get much further. How about setting the URI and POM details?