Skip to content

A Gradle plugin to dynamically generate Gradle scripts (and more) from Maven pom.xml's

Notifications You must be signed in to change notification settings

uklance/gradle-maven-transform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gradle-maven-transform Build Status

What does this Gradle plugin do?

This plugin allows you to transform Maven pom.xml files during your build. A typical use case for this is when you are trying to migrate your build from Maven to Gradle and you want both builds to work during the migration. You can generate Gradle scripts which contain the Maven dependencies from the pom.xml in a format that can be applied in a Gradle build. Once you are happy with the Gradle build you can copy paste the generated Gradle scripts into your Gradle build and ditch Maven for ever!

How does it work?

The gradle-maven-transform plugin uses the Maven ModelBuilder API to read pom.xml files and generate an Effective POM for each which are then passed to the Transformers specified in the Gradle build. The Effective POM is a "denormalized" view of the model with property substitutions applied and any versions inherited from parent POMs or BOMs are explicitly declared in each Model.

Is there an example?

Sure, there's a sample multi-module Maven build here. The pom.xml files from the Maven build are transformed by the example-transform project. The generated Gradle scripts are applied in the build.gradle here. So it's possible to build maven-root with both Maven and Gradle.

What Transformers are available?

There's out-of-the-box support for Freemarker transformations which should suit most use cases. Users can implement their own custom transformations by implementing Transformer

Transform context

Each Transformer is passed a Map<String, Object> context. There will be objects in the context map by default but custom context objects can be added and the defaults can be overridden.

Projects Transform

Confguring a projectsTransform { ... } will generate a single file for all the pom.xml files configured. Typically you would use this transformation to perform aggregate functionality. Eg: to generate a script common to all projects which will be applied in the root build.gradle of a multi-module build. By default the transform context map will contain the following:

Name Type
projectsContext ProjectsContext
dependencyAggregator DependencyAggregator

Project Transform

Configuring a projectTransform { ... } will generate a separate file for each of the pom.xml files configured. An example use case for this is to generate a gradle script per project, each containing the dependencies for a single project. Each generated script would be applied by a single Gradle project.

Name Type
projectContext ProjectContext
projectsContext ProjectsContext
dependencyAggregator DependencyAggregator

Sample usage

plugins {
   id "com.lazan.maven-transform" version "0.3"
}
mavenTransform {
   pomXmls 'path1/pom.xml', 'path2/pom.xml', 'path3/pom.xml'
   outputDirectory "$buildDir/mavenTransform"
   transformClasspath files('src/main/freemarker')
   
   projectsTransform {
      // produce a single output file for all pom.xmls
      outputPath 'aggregate.gradle'
      freemarkerTransform 'aggregate.ftl'
      context 'customContext', { projectsContext ->
         return new MyCustomProjectsContext(projectsContext)
      }
   }

   projectTransform {
      // produce an output file for each pom.xml
      outputPath { context -> "${context.artifactId}.gradle" }
      freemarkerTransform 'project-dependencies.ftl'
      context 'customContext', { projectContext ->
         return new MyCustomProjectContext(projectContext)
      }
   }
}

About

A Gradle plugin to dynamically generate Gradle scripts (and more) from Maven pom.xml's

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages