2017-06-08 91 views
0

我有任務將具有基於java的maven插件的maven項目遷移到gradle。該插件使用maven-plugin-plugin並具有目標描述符和helpmojo。我可以使用Gradle生成Maven插件描述符(plugin.xml)嗎?

<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插件?

+0

在Gradle中沒有支持構建,我不知道有任何第三方插件正在做這件事。 –

+0

不知道爲什麼你會使用Gradle來構建一個Maven插件項目。 – Tome

回答

0

您可以通過在build.gradle上運行此任務代碼來使用gradle創建此文件。這段代碼生成一個pom.xml臨時文件,運行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" 
    } 
} 
+0

嘿。我試了你的答案,它給了我一個「索引超出範圍」的錯誤。感謝您的幫助,但我想我會用gradle重寫maven插件 – ionut

相關問題