Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ability to convert Gradle Project to Maven in IntelliJ and keep both build files

I have been happily using IntelliJ + Gradle, but I was recently asked to move to Maven as a build tool. Is there an easy way to convert a Gradle project to Maven in IntelliJ or perhaps keep both configuration files (build.gradle and pom.xml)? As per Gradle build.gradle to Maven pom.xml, I have used the Gradle Maven's plugin and generated the pom.xml file in build/poms, but I would like to keep both build.gradle and pom.xml at the same level providing the ability to build the project using whichever is preferred. Is that possible?

Thanks,

like image 851
paranzana Avatar asked Mar 30 '16 22:03

paranzana


People also ask

Can I convert a Gradle project to Maven?

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

Can we have both Maven and Gradle together?

Short answer: yes. There's no conflict between having two independent build scripts for the same project, one in Maven and one in Gradle.

How do I change a project from Gradle to Maven in IntelliJ?

In the Project tool window, right-click your project and select Add Framework Support. In the dialog that opens, select Maven from the options on the left and click OK. IntelliJ IDEA adds a default POM to the project and generates the standard Maven layout in Project tool window.

Can I use Gradle dependency in Maven?

Gradle understands different repository types, such as Maven and Ivy, and supports various ways of accessing the repository via HTTP or other protocols. You can also have repositories on the local file system. This works for both Maven and Ivy repositories.


3 Answers

I can't offer you a solution that lets you keep all your Gradle project settings intact, but if you find the following useful, please mark it accordingly.

To move between the two build tools, you can do the following:

If you have a Maven project, which you want to convert, you need to go into the project folder, and on the cmd line do "gradle init" (obviously make sure Gradle is on your path). Close and reopen the project, and you will be prompted to enable Gradle on the project.

To your exact question, about converting Gradle back to Maven. You can simply delete the .idea folder, leaving all the Gradle scripts in place. Then inside Intellij, select open project. Navigate to your pom.xml and select this. Next through the following screens. When Intellij prompts you to enable the project as Gradle, decline. The result is new project that will still have your Gradle scripts included, but now builds with Maven. You will need to have a valid pom.xml in place that maven can use.

like image 115
wax_lyrical Avatar answered Sep 16 '22 11:09

wax_lyrical


To create pom, let's add the Maven plugin to your build.gradle file:

apply plugin: 'maven'

The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

The plugin automatically adds the install task as well. So to convert, let's run the following:

gradle install
like image 32
itro Avatar answered Sep 18 '22 11:09

itro


What worked for me:

  1. Adding the maven-publish plugin at the top of my build.gradle
plugins {
    id 'maven-publish'
}
  1. Adding a publishing task with bare basic maven information
publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'com.my.groupid'
            artifactId = 'my-artifact'
            version = '1.0-SNAPSHOT'
            from components.java
        }
    }
}
  1. running
./gradlew publishToMavenLocal
  1. Go to the build/publications/maven/pom-default.xml and copy it as pom.xml
  2. The above creates just a simple maven pom with dependencies and required some tweaks to be useful

Note: I tried this on a very simple project.

like image 3
Haim Raman Avatar answered Jan 01 '70 00:01

Haim Raman