简体   繁体   English

将 Gradle 模块包含到 Maven 项目中

[英]Include Gradle module to Maven project

We have large project that uses Maven as a build system.我们有使用 Maven 作为构建系统的大型项目。 We decided that in future projects we will use Gradle as more convenient tool, but we want to use Gradle for our legacy project too.我们决定在未来的项目中我们将使用 Gradle 作为更方便的工具,但我们也希望在我们的遗留项目中使用 Gradle。

I think that migrating from Maven to Gradle at one time will be very painful, because we have TONS of code in POM files (we have really heavy build logic).我认为一次从 Maven 迁移到 Gradle 会非常痛苦,因为我们在 POM 文件中有大量代码(我们有非常繁重的构建逻辑)。

I know, that Gradle have automigration tool (gradle init) but it doesn't work properly (I think this tool is for small Maven projects with no specific build logic).我知道,Gradle 有自动迁移工具(gradle init),但它不能正常工作(我认为这个工具适用于没有特定构建逻辑的小型 Maven 项目)。

So, here is a question: can I include Gradle module to Maven project to migrate with small steps?所以,这里有一个问题:我可以将 Gradle 模块包含到 Maven 项目中以小步迁移吗? Maybe there is Maven plugin, that allows to treat build.gradle as pom.xml file?也许有 Maven 插件,允许将build.gradle视为pom.xml文件?

There is a gradle-maven-plugin which allows you to run Gradle tasks from within Maven.有一个gradle-maven-plugin允许您从 Maven 中运行 Gradle 任务。 From the plugin description:从插件说明:

To use the plugin, simply declare the plugin and bind it to the maven lifecycle phase of your choice:要使用插件,只需声明插件并将其绑定到您选择的 maven 生命周期阶段:

 <plugin> <groupId>org.fortasoft</groupId> <artifactId>gradle-maven-plugin</artifactId> <version>1.0.8</version> <configuration> <tasks> <!-- this would effectively call "gradle doSomething" --> <task>doSomething</task> </tasks> </configuration> <executions> <execution> <!-- You can bind this to any phase you like --> <phase>compile</phase> <goals> <!-- goal must be "invoke" --> <goal>invoke</goal> </goals> </execution> </executions> </plugin>

Now when you run maven, gradle will be invoked and execute the "doSomething" task defined in build.gradle.现在,当您运行 maven 时,将调用 gradle 并执行 build.gradle 中定义的“doSomething”任务。

Obviously you can change the task(s) to suit your needs.显然,您可以更改任务以满足您的需要。

In this example, the gradle invocation will happen during the maven "compile" phase, but this can be easily changed by changing the element value.在这个例子中,gradle 调用将在 maven“编译”阶段发生,但这可以通过更改元素值轻松更改。

Not sure, how to include artifacts of Gradle build to your dependencies, may be you'll need to post this artifact to local maven repository, to make it available for your maven project.不确定如何将 Gradle 构建的工件包含到您的依赖项中,可能您需要将此工件发布到本地 Maven 存储库,以使其可用于您的 Maven 项目。

You might find my gradle-maven-share plugin useful.你可能会发现我的gradle-maven-share插件很有用。 It's designed to help migrating from maven to gradle by sharing the maven config with gradle.它旨在通过与 gradle 共享 maven 配置来帮助从 maven 迁移到 gradle。

I probably would use:我可能会使用:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>test</id>
      <phase>test</phase>
      <configuration>
        <executable>./gradlew</executable>
        <arguments>
          <argument>test</argument>
          <argument>-i</argument>
        </arguments>
      </configuration>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>
    <execution>
      <id>install</id>
      <phase>install</phase>
      <configuration>
        <executable>./gradlew</executable>
        <arguments>
          <argument>install</argument>
        </arguments>
      </configuration>
      <goals>
        <goal>exec</goal>
      </goals>
    </execution>
  </executions>
</plugin>

See reasons at: https://stackoverflow.com/a/62971663/8650621原因见: https : //stackoverflow.com/a/62971663/8650621

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM