【问题标题】:Can i generate a maven plugin descriptor (plugin.xml) with gradle?我可以用 gradle 生成一个 Maven 插件描述符(plugin.xml)吗?
【发布时间】:2017-06-08 08:11:15
【问题描述】:

我的任务是将具有基于 java 的 maven 插件的 maven 项目迁移到 gradle。该插件使用 maven-plugin-plugin 并具有目标描述符和 helpmojo。

<build>
    <plugins>
      <plugin>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.4</version>
        <configuration>
        </configuration>
        <executions>
          <execution>
            <id>default-descriptor</id>
            <goals>
              <goal>descriptor</goal>
            </goals>
            <phase>process-classes</phase>
          </execution>
          <execution>
            <id>help-descriptor</id>
            <goals>
              <goal>helpmojo</goal>
            </goals>
            <phase>process-classes</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

有什么方法可以在 gradle 中执行这些目标,还是我需要在 gradle 中重写我的基于 java 的 maven 插件?

【问题讨论】:

  • Gradle 中没有内置支持,我不知道有任何第三方插件这样做。
  • 不知道为什么要使用 Gradle 构建 Maven 插件项目。

标签: java maven gradle


【解决方案1】:

您可以通过在 build.gradle 上运行此任务代码来使用 gradle 创建此文件。此代码生成一个 pom.xml 临时文件,使用临时 pom 运行 maven 并生成 plguin.xml 文件。最后(在 doLast 上)任务将文件复制到 META-INF/maven/ 目录。

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"
  }
}

【讨论】:

  • 嘿。我试过你的答案,它给了我一个“索引超出范围”的错误。感谢您的帮助,但我想我会在 gradle 中重写 maven 插件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 2013-09-30
  • 2018-07-04
  • 2015-06-12
  • 1970-01-01
相关资源
最近更新 更多