Given a Gradle web project that is to be published as a JAR (so that it can be a dependency of another Gradle web project, which has a different release cycle).
The maven-publish plugin is used:
apply plugin: 'war'
apply plugin: 'maven'
apply plugin: 'maven-publish'
The web project has a providedCompile dependency:
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
A jar is published using mavenJava:
publishing {
publications {
// mavenJava publishes a jar file
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
mavenLocal()
}
}
The problem is that javax.servlet-api has a runtime scope in the resulting Maven POM:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>runtime</scope>
</dependency>
Runtime scope makes no sense for the servlet-api, it is even harmful. How can the scope be set to provided in the pom.xml?
With the help of
pom.withXml(see this Gradle sample) it's possible to transform Gradle'sprovidedCompileintoprovidedscope for Maven's POM:What the
pom.withXmlsection does is going through all dependencies of typeprovidedCompilewithin the Gradle project configuration and changing the scope to be written into the Mavenpom.xmlfromruntimetoprovided.The generated
pom.xmlnow has theprovidedscope set for thejavax.servlet-api: