将Gradle模块包含到Maven项目中 [英] Include Gradle module to Maven project

查看:1604
本文介绍了将Gradle模块包含到Maven项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用Maven作为构建系统的大型项目。我们决定,在未来的项目中,我们将使用Gradle作为更方便的工具,但我们也希望将Gradle用于我们的遗留项目。



我认为从Maven迁移到Gradle有一段时间会非常痛苦,因为我们在POM文件中有很多代码(我们有非常繁重的构建逻辑)。



我知道,Gradle有自动迁移工具gradle init),但它不能正常工作(我认为这个工具适用于没有特定的构建逻辑的小型Maven项目)。

所以,这里有一个问题:can我包含Gradle模块到Maven项目以小步骤迁移?也许有Maven插件,它允许将 build.gradle 作为 pom.xml 文件?

解决方案

有一个 gradle-maven-plugin ,它允许你从Maven内部运行Gradle任务。从插件描述中可以看到:


要使用插件,只需声明插件并将其绑定到您的maven
生命周期阶段选择:

 < plugin> 
< groupId> org.fortasoft< / groupId>
< artifactId> gradle-maven-plugin< / artifactId>
< version> 1.0.8< / version>
<配置>
<任务>
<! - 这会有效地调用gradle doSomething - >
<任务> doSomething< /任务>
< /任务>
< / configuration>
<执行次数>
<执行>
<! - 您可以将其绑定到您喜欢的任何阶段 - >
<阶段>编译< /阶段>
<目标>
<! - 目标必须是invoke - >
< goal> invoke< / goal>
< /目标>
< /执行>
< /执行次数>
< / plugin>

现在,当您运行maven时,gradle将被调用并执行
doSomething任务在build.gradle中定义。



显然,您可以根据自己的需要更改任务。

在这个例子中,Gradle调用将在maven
compile阶段发生,但是可以通过更改
元素的值来轻松更改。


不确定,如何将Gradle构建的构件包含到您的依赖项中,可能需要将此构件发布到本地Maven存储库,以使其可用于您的Maven项目。

We have large project that uses Maven as a build system. 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.

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).

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).

So, here is a question: can I include Gradle module to Maven project to migrate with small steps? Maybe there is Maven plugin, that allows to treat build.gradle as pom.xml file?

解决方案

There is a gradle-maven-plugin which allows you to run Gradle tasks from within Maven. From the plugin description:

To use the plugin, simply declare the plugin and bind it to the maven lifecycle phase of your choice:

<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.

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.

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项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆