I am thinking about moving to Gradle. I do, however, have some Maven plugins that will need to be supported for the foreseeable future.
Is there a way to build Maven plugins using Gradle?
You can't use a Maven plugin as-is in Gradle; you'll have to port it to a Gradle plugin. How difficult this is depends on how many Maven APIs the plugin is using. Another strategy might be to call into Maven via Gradle's Exec task.
Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.
While creating a custom plugin, you need to write an implementation of plugin. Gradle instantiates the plugin and calls the plugin instance using Plugin. apply() method. The following example contains a greeting plugin, which adds a hello task to the project.
Here's something that worked for me:
"install.repositories.mavenInstaller.pom.writeTo( 'pom.xml' )"
"mvn org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor"
This way "build/classes/main/META-INF/maven/plugin.xml"
is created and then packed properly by jar
task, which is all that's needed for a jar file to become a Maven plugin, AFAIK. Also, I believe, "maven-plugin-annotations" should be used in a plugin.
task pluginDescriptor( type: Exec ) {
commandLine 'mvn', '-e', '-B', 'org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor'
doFirst {
final File pom = project.file( 'pom.xml' )
install.repositories.mavenInstaller.pom.writeTo( pom )
assert pom.file, "[$pom.canonicalPath] was not created"
pom.text = pom.text.
replace( '<groupId>unknown</groupId>', "<groupId>${project.group}</groupId>" ).
replace( '<artifactId>empty-project</artifactId>', "<artifactId>${project.name}</artifactId>" ).
replace( '<version>0</version>', """
|<version>${version}</version>
| <packaging>maven-plugin</packaging>
| <build>
| <directory>\${project.basedir}/build</directory>
| <outputDirectory>\${project.build.directory}/classes/main</outputDirectory>
| </build>
|""".stripMargin().trim())
}
doLast {
final pluginDescriptor = new File(( File ) project.compileGroovy.destinationDir, 'META-INF/maven/plugin.xml' )
assert pluginDescriptor.file, "[$pluginDescriptor.canonicalPath] was not created"
println "Plugin descriptor file:$pluginDescriptor.canonicalPath is created successfully"
}
}
project.compileGroovy.doLast{ pluginDescriptor.execute() }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With