Chapter 47. Build Setup Plugin

The build setup plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.

The Gradle build setup plugin prepares the current project for Gradle. Typically it will create the relevant build.gradle, settings.gradle files. If a pom.xml file is found in the project directory the Maven build is converted to a Gradle build. At the moment only the conversion from Maven 3 is supported.

The plugin is currently incubating which means it is already useful but not everything might work perfectly. The API, plugin and task names may change before the final release. Please let us know your feedback or report any issues.

This plugin provides a lifecycle task named setupBuild. If a Gradle build with the task name setupBuild is triggered in a directory without an existing build file in the project directory, the build setup plugin is auto applied. This means that you can setup a Gradle project build without needing a stub build.gradle file declaring the build-setup plugin. If a pom.xml file is found in the project directory, a task of type ConvertMaven2Gradle is added to the setupBuild lifecycle. Furthermore a Wrapper task is part of the setupBuild lifecycle that generates all files needed to run the build using the Gradle Wrapper. Details about the Gradle Wrapper can be found in the according chapter Chapter 61, The Gradle Wrapper

The build-setup plugin adds the following tasks to the project:

Table 47.1. Build Setup plugin - tasks

Task name Depends on Type Description
buildSetup maven2Gradle, generateBuildFile, generateSettingsFile, setupWrapper lifecycle task (no actions) Generates a Gradle project.
maven2Gradle - ConvertMaven2Gradle Converts a Maven project to a Gradle project by obtaining the effective POM. This task is only added if a pom.xml file can be found in the project directory.
generateBuildFile - GenerateBuildFile Generates an build.gradle file. This task is only added if no pom.xml file can be found in the project directory.
generateSettingsFile - GenerateSettingsFile Generates a settings.gradle file. This task is only added if no pom.xml file can be found in the project directory.
setupWrapper - Wrapper Generates Gradle wrapper files.

The ConvertMaven2Gradle task works by obtaining the effective POM of the current project by executing Maven internally. Then it reads the dependencies and other information to generate build.gradle scripts. This task is inspired by the maven2gradle tool founded and maintained by recognized leaders of Gradle community; created by Baruch Sadogursky with contributions from Antony Stubbs, Matthew McCullough and others.

47.1. Maven conversion - features

  • Uses effective POM and effective settings (support for POM inheritance, dependency management, properties)
  • Supports both single module and multimodule projects. Generates settings.gradle for multimodule projects (*).
  • Supports custom module names (that differ from directory names)
  • Generates general metadata - id, description and version
  • Applies maven, java and war plugins (as needed)
  • Supports packaging war projects as jars if needed
  • Generates dependencies (both external and inter-module)
  • Generates download repositories (inc. local Maven repository)
  • Adjusts java compiler settings
  • Supports packaging of sources and tests
  • Supports testng runner
  • Generates global exclusions from Maven enforcer plugin settings

(*) - Note: Your project will be considered multi-module only if your reactor is also a parent of at least one of your modules. Why so? Reactor project is built last, when Parent project is built first. The reactor has to be built first, because effective-pom Mojo generates needed output only if it finds modules in first project it encounters. Making reactor also a parent achieves this.

47.2. Usage

To convert a Maven project follow the steps:

  • Make sure your Maven project builds and uses maven3.
  • Make sure no build.gradle file is available in your your Maven project folder.
  • Run gradle setupBuild in your project directory. You should see that the maven2Gradle task is executed as a dependency on setupBuild.