Skip to content

Migrate from Maven to Gradle by sharing dependencies and more

License

Notifications You must be signed in to change notification settings

uklance/gradle-maven-share

Repository files navigation

gradle-maven-share Build Status Coverage Status Download

The gradle-maven-share plugin helps with migrating your build from Maven to Gradle. It allows you to have a Maven and Gradle build working in parallel by importing Maven dependencies into a Gradle build. It's not only dependencies that can be shared, there's hooks to share any configuration from your Maven pom.xml files with Gradle via custom ShareActions

It's recommended to use this plugin as a 'stepping stone' to migrate from Maven to Gradle with the end goal of removing Maven entirely.

Custom pom file

By default, pom.xml will be used in the project directory but this can be configured

apply plugin: 'com.lazan.gradlemavenshare'
apply plugin: 'java'

mavenShare {
	pomFile 'custom-pom.xml'
}

Custom ShareAction

You can mutate the Gradle project model based on the Maven project model using ShareActions. These can be applied before or after the dependencies are shared

import com.lazan.gradlemavenshare.*
def shareAction = { ResolvedPom pom, Project proj, ProjectResolver resolver ->
	println "Sharing $pom.artifactId with $proj.name"
} as ShareAction

subprojects {
	apply plugin: 'java'
	apply plugin: 'com.lazan.gradlemavenshare'
	
	mavenShare {
		doFirst shareAction
		doLast shareAction
	}
}

See ShareAction, ResolvedPom and ProjectResolver

Excluding maven dependencies

You may not wish to share all maven dependencies with gradle, dependencies can be excluded via any maven dependency attributes (groupId, artifactId, version, classifier, type)

apply plugin: 'java'
apply plugin: 'com.lazan.gradlemavenshare'

mavenShare {
	exclude [groupId: 'com.foo', artifactId: 'bar']
	exclude [classifier: 'tests']
	exclude [type: 'test-jar']
}

Custom ConfigurationResolver

By default, Gradle will use the maven dependency's scope to decide which Gradle Configuration to add the dependecy to

Maven Scope Gradle Configuration
test testCompile
compile compile
provided compileOnly
runtime runtime

For custom behaviour you can configure a ConfigurationResolver

import com.lazan.gradlemavenshare.*

apply plugin: 'java'
apply plugin: 'com.lazan.gradlemavenshare'

mavenShare {
	configurationResolver =  { Project project, org.apache.maven.model.Dependency dependency ->
		String scope = dependency.scope ?: 'compile'
		String configName = "foo${scope}"
		return project.configurations.maybeCreate(configName)
	} as ConfigurationResolver
}

See ConfigurationResolver

Custom DependencyResolver

import com.lazan.gradlemavenshare.*
import org.apache.maven.model.Dependency

subprojects {
	apply plugin: 'java'
	apply plugin: 'com.lazan.gradlemavenshare'

	configurations {
		testOutput
	}

	dependencies {
		testOutput sourceSets.test.output
	}

	mavenShare {
		def testJarResolver = { Project proj, Dependency dep, ProjectResolver resolver ->
			if (resolver.isProject(dep)) {
				// local project dependency
				String projectPath = resolver.getProject(dep).path
				return proj.dependencies.project([path: projectPath, configuration: 'testOutput'])
			}
			// external dependency
			return [group: dep.groupId, name: dep.artifactId, version: dep.version, classifier: 'tests']
		} as DependencyResolver
		
		resolve([groupId: 'com.foo', type: 'test-jar'], testJarResolver)
	}
}

See DependencyResolver and ProjectResolver

About

Migrate from Maven to Gradle by sharing dependencies and more

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published