Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Maven plugins using Gradle

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?

like image 827
Axel Fontaine Avatar asked Jun 26 '12 14:06

Axel Fontaine


People also ask

Can Maven plugins be used in 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.

Can Gradle build Maven project?

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.

How do I add plugins to build Gradle?

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.


1 Answers

Here's something that worked for me:

  • After compiling plugin's sources generate project's POM: "install.repositories.mavenInstaller.pom.writeTo( 'pom.xml' )"
  • Patch POM generated and provide plugin's coordinates and correct destination directories
  • Run "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() }
like image 75
Evgeny Goldin Avatar answered Oct 05 '22 23:10

Evgeny Goldin