且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将Gradle模块包含到Maven项目中

更新时间:2022-10-14 23:12:58

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


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

 < plugin> 
< groupId> org.fortasoft< / groupId>
< artifactId> gradle-maven-plugin< / artifactId>
< version> 1.0.8< / version>
<配置>
<任务>
<任务> doSomething< /任务>
< /任务>
< / configuration>
<执行次数>
<执行>
<阶段>编译< /阶段>
<目标>
< 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.