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.
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'
}
You can mutate the Gradle project model based on the Maven project model using ShareAction
s. 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
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']
}
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
}
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