【问题标题】:Build Maven plugins using Gradle使用 Gradle 构建 Maven 插件
【发布时间】:2012-06-26 14:18:07
【问题描述】:

我正在考虑迁移到 Gradle。不过,我确实有一些 Maven 插件需要在可预见的未来得到支持。

有没有办法使用 Gradle 构建 Maven 插件?

【问题讨论】:

    标签: maven maven-plugin gradle


    【解决方案1】:

    Here's something 对我有用:

    • 编译插件源后生成项目的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 插件AFAIK 所需的全部内容。另外,我认为,"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】:

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

    【讨论】:

      【解决方案3】:

      我发现了这个可以用来创建 Maven 插件的 Gradle 插件 Maven Plugin Builder Gradle Plugin 但我还没有让它工作

      【讨论】:

        【解决方案4】:

        目前不支持该功能,但它在 development roadmap 上。每隔一段时间检查一下roadmap dashboard,看看状态是否发生了变化。

        【讨论】:

        • 我们的重点更多是支持 Gradle 中 Maven 插件的重用,而不是支持构建它们。
        • 路线图仪表板链接不再有效
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-12
        • 1970-01-01
        • 2014-05-16
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多