Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Gradle module to Maven project

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?

like image 818
Anton Dzodzikov Avatar asked Nov 01 '16 01:11

Anton Dzodzikov


2 Answers

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.

like image 75
Stanislav Avatar answered Nov 09 '22 21:11

Stanislav


You might find my gradle-maven-share plugin useful. It's designed to help migrating from maven to gradle by sharing the maven config with gradle.

like image 4
lance-java Avatar answered Nov 09 '22 20:11

lance-java