Ability to convert Gradle Project to Maven in IntelliJ and keep both build files
What worked for me:
- Adding the maven-publish plugin at the top of my build.gradle
plugins {
id 'maven-publish'
}
- 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
}
}
}
- running
./gradlew publishToMavenLocal
- Go to the build/publications/maven/pom-default.xml and copy it as pom.xml
- 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.
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
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.