使用Gradle构建Maven插件

13

我在考虑转向Gradle。然而,我有一些Maven插件需要在可预见的未来得到支持。

有没有一种方法可以使用Gradle构建Maven插件?

4个回答

5

这里有一些对我有用的东西:

  • 编译插件源代码后,生成项目的POM:"install.repositories.mavenInstaller.pom.writeTo( 'pom.xml' )"
  • 修补生成的POM并提供插件坐标和正确的目标目录
  • 运行"mvn org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor"

这样就会创建"build/classes/main/META-INF/maven/plugin.xml",然后由jar任务正确打包,这就是将jar文件变成Maven插件所需的全部步骤,据我所知。此外,我认为应该在插件中使用"maven-plugin-annotations"

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() }

2
非常感谢。基于此,我制作了以下版本,不需要使用replace方法:https://gist.github.com/fikovnik/ffc1fed1867bc7fa679aaf8e48f00c21 - fikovnik

0

0

我不知道是否有第三方Gradle插件可以构建Maven插件。一种可能性是调用Maven来完成部分工作(特别是元数据生成)。必要的POM可以即时创建。另一种可能性是将元数据提交到源代码控制并手动更新它(可能需要运行Maven)。最后,您可以编写一些代码在Gradle端执行元数据生成,可能会重用一些Maven代码。


-1

1
我们的重点更多地放在支持从Gradle重复使用Maven插件上,而不是支持构建它们。 - Peter Niederwieser
路线图仪表板链接已失效。 - Matthew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接