This article covers shortly how to convert Maven POM file to Gradle build file. This will be useful in case we have a Maven project and want to quickly convert it to Gradle project.
1. Prerequisites
We need to have Gradle installed on our environment first. You can refer to the following tutorials for Gradle installation:
Install Gradle on Ubuntu 16.04 LTS (Xenial Xerus)
2. Convert Maven POM file to Gradle Build file
Gradle provides us a plugin called Build Init Plugin which can be used to bootstrap the process of creating a new Gradle build. It supports creating brand new projects of different types as well as converting existing builds (e.g. An Apache Maven build) to be Gradle builds.
If we have a Maven project, we can convert Maven POM file to Gradle build file by executing below command:
1 |
gradle init |
or
1 |
gradle init --type pom |
3. Example
Let’s say we have an Maven project with the pom.xml file as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.howtoprogram</groupId> <artifactId>junit5-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>junit5-tutorial</name> <url>https://howtoprogram.xyz</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.jupiter.version>5.0.0-M2</junit.jupiter.version> <junit.vintage.version>4.12.0-M2</junit.vintage.version> <junit.platform.version>1.0.0-M2</junit.platform.version> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>${junit.platform.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.jupiter.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit.vintage.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
To convert Maven POM file to Gradle build file:
1 |
gradle init |
Here is the content of the build.gradle file which has been generated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
apply plugin: 'java' apply plugin: 'maven' group = 'com.howtoprogram' version = '0.0.1-SNAPSHOT' description = """junit5-tutorial""" sourceCompatibility = 1.5 targetCompatibility = 1.5 repositories { maven { url "http://repo.maven.apache.org/maven2" } } dependencies { testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version:'1.0.0-M2' testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version:'5.0.0-M2' testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version:'4.12.0-M2' } |
Beside this file, other files and folders are also generated: gradle wrapper file, settings.gradle, etc
4. Related References
Specify The Build File in Gradle
Refresh or Redownload Dependencies in Gradle