How to create custom pom.xml without dependencies by Gradle

innossh
innossh
Feb 25, 2017 · 1 min read

If you want to create custom pom.xml by Gradle, you can use maven plugin and define pom object in build.gradle, as below.

...
project(‘dummy-project-01’) {
apply plugin: ‘maven’
task writeNewPom {
pom {
project {
inceptionYear ‘2008’
licenses {
license {
name ‘The Apache Software License, Version 2.0’
url ‘http://www.apache.org/licenses/LICENSE-2.0.txt’
distribution ‘repo’
}
}
}
}.writeTo(“$buildDir/newpom.xml”)
}
dependencies {
...
}
}
...

It’s also on the official document. https://docs.gradle.org/current/userguide/maven_plugin.html#sec:maven_convention_methods
You are free to add pom elements (such as repositories or profiles) manually, and the dependencies will be added automatically by Gradle.

It’s very simple, but I had a question. How can I create the custom pom.xml without dependencies?

The answer is here.

...
project(‘dummy-project-02’) {
apply plugin: ‘maven’
task writeNewPom << {
pom {}.whenConfigured {
pom -> pom.dependencies.clear()
}.writeTo(“$buildDir/resources/main/META-INF/maven/$project.group/$project.name/pom.xml”)
}
processResources.dependsOn writeNewPom
dependencies {
...
}
}
...

pom.dependencies is a List, so you can make it empty by the method clear(). By the way, since processResources dependes on the task, you can create the custom pom.xml every time gradle build is executed. Also you can specify the output path by the method writeTo . So the path $buildDir/resources/main/META-INF/maven/$project.group/$project.name/pom.xml means that the custom pom.xml will be in the jar file.

http://innossh.hatenablog.com/entry/2017/02/06/193954 (ja)

innossh

Written by

innossh

I’m a software engineer in Japan. My Japanese tech blog is here http://innossh.hatenablog.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade