How to create custom pom.xml without dependencies by Gradle
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.
