Skip to content

Instantly share code, notes, and snippets.

@fikovnik
Created May 8, 2016 19:58
Show Gist options
  • Save fikovnik/ffc1fed1867bc7fa679aaf8e48f00c21 to your computer and use it in GitHub Desktop.
Save fikovnik/ffc1fed1867bc7fa679aaf8e48f00c21 to your computer and use it in GitHub Desktop.
Building maven plugins by gradle
configurations {
mavenEmbedder
}
dependencies {
compile "org.apache.maven:maven-core:$mavenVersion"
compile "org.apache.maven:maven-plugin-api:$mavenVersion"
compile 'org.apache.maven.plugin-tools:maven-plugin-annotations:3.4'
mavenEmbedder "org.apache.maven:maven-embedder:$mavenVersion"
mavenEmbedder 'org.slf4j:slf4j-simple:1.7.5'
mavenEmbedder 'org.eclipse.aether:aether-connector-basic:1.0.2.v20150114'
mavenEmbedder 'org.eclipse.aether:aether-transport-wagon:1.0.2.v20150114'
mavenEmbedder 'org.apache.maven.wagon:wagon-http:2.9:shaded'
mavenEmbedder 'org.apache.maven.wagon:wagon-provider-api:2.9'
}
install.repositories.mavenInstaller.pom.with {
groupId = project.group
artifactId = project.name
version = project.version
packaging = 'maven-plugin'
}
// runs the plugin description generator
task generatePluginDescriptor(type: JavaExec, dependsOn: compileJava) {
def pomFile = file("$buildDir/pom.xml")
def pluginDescriptorFile = new File(project.compileJava.destinationDir, 'META-INF/maven/plugin.xml')
def directory = buildDir.canonicalPath
def outputDirectory = compileJava.destinationDir.canonicalPath
// FIXME: this does not seem to be working
inputs.files project.compileJava.outputs.files
outputs.file pluginDescriptorFile
classpath = configurations.mavenEmbedder
main = 'org.apache.maven.cli.MavenCli'
systemProperties['maven.multiModuleProjectDirectory'] = projectDir
args = [
'--errors',
'--batch-mode',
'--settings', '../config/repo-settings.xml',
'--file', "${buildDir}/pom.xml",
'org.apache.maven.plugins:maven-plugin-plugin:3.4:descriptor'
]
doFirst {
// we have to do it in this cryptic way, instead of
// ```
// project {
// build {
// directory = directory
// outputDirectory = outputDirectory
// }
// }
// because MavenModel already defines build and so it will
// result in No such property: _SCRIPT_CLASS_NAME_ for class: org.apache.maven.model.Model
install.repositories
.mavenInstaller
.pom
.withXml {
asNode().appendNode('build')
.with {
appendNode('directory', directory)
appendNode('outputDirectory', outputDirectory)
}
}
.writeTo(pomFile)
assert pomFile.file, "${pomFile.canonicalPath}: was not generated"
logger.info("POM is generated in ${pomFile.canonicalPath}")
}
doLast {
assert pluginDescriptorFile.file, "${pluginDescriptorFile.canonicalPath}: was not generated"
logger.info("Plugin descriptor is generated in ${pluginDescriptorFile.canonicalPath}")
}
}
project.jar.dependsOn(generatePluginDescriptor)
@tbouffard
Copy link

@nathanabercrombie the install task comes from https://docs.gradle.org/current/userguide/maven_plugin.html
just add the following on top of the build.gradle file

buildscript {
    ext {
        mavenVersion = '3.5.2'
    }
}

plugins {
    id 'maven'
}

@Hubbitus
Copy link

I've got exception like:

1) No implementation for org.apache.maven.repository.RepositorySystem was bound.
  while locating org.apache.maven.project.DefaultProjectBuildingHelper

1 error
        at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025)
        at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
        at org.eclipse.sisu.space.AbstractDeferredClass.get(AbstractDeferredClass.java:48)
        at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
        at com.google.inject.internal.InternalFactoryToInitializableAdapter.provision(InternalFactoryToInitializableAdapter.java:53)
        at com.google.inject.internal.ProviderInternalFactory$1.call(ProviderInternalFactory.java:65)
        at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:115)
        at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:133)
        at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:68)
        at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:63)
        at com.google.inject.internal.InternalFactoryToInitializableAdapter.get(InternalFactoryToInitializableAdapter.java:45)
        at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)

@Hubbitus
Copy link

And what should be in ../config/repo-settings.xml?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment