16

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,

Community
  • 1
  • 1
panza
  • 1,341
  • 7
  • 38
  • 68

3 Answers3

22

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.

wax_lyrical
  • 922
  • 1
  • 10
  • 22
  • IDEA have "Import as Gradle project" option in context menu on *.gradle files, so project reopen is not required. – cybersoft Jan 11 '21 at 10:55
6

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.

Haim Raman
  • 11,508
  • 6
  • 44
  • 70
0

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
itro
  • 7,006
  • 27
  • 78
  • 121