如何使用提供的范围行事发布Gradle项目JAR [英] How to maven-publish a Gradle project JAR with provided scope

查看:84
本文介绍了如何使用提供的范围行事发布Gradle项目JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个将以JAR形式发布的Gradle Web项目(以便它可以是另一个Gradle Web项目的依赖项,而该项目具有不同的发布周期).

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).

使用了maven-publish插件:

apply plugin: 'war'
apply plugin: 'maven'
apply plugin: 'maven-publish'

该Web项目具有providedCompile依赖项:

The web project has a providedCompile dependency:

providedCompile 'javax.servlet:javax.servlet-api:3.0.1'

使用mavenJava发布"jar":

publishing {
    publications {
        // mavenJava publishes a jar file
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        mavenLocal()
    }
}

问题是javax.servlet-api在生成的Maven POM中具有runtime范围:

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>

运行时范围对于servlet-api没有意义,甚至有害.如何在pom.xml中将范围设置为provided?

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?

推荐答案

pom.withXml的帮助下(请参阅此Gradle

With the help of pom.withXml (see this Gradle sample) it's possible to transform Gradle's providedCompile into provided scope for Maven's POM:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            // providedCompile -> provided scope
            pom.withXml {
                asNode().dependencies.'*'.findAll() {
                    it.scope.text() == 'runtime'  && project.configurations.providedCompile.allDependencies.find { dep ->
                        dep.name == it.artifactId.text()
                    }
                }.each() {
                    it.scope*.value = 'provided'
                }
            }
        }
    }
    repositories {
        mavenLocal()
    }
}

pom.withXml部分的工作是遍历Gradle项目配置中所有类型为providedCompile的依赖项,并将要写入Maven pom.xml的范围从runtime更改为provided.

What the pom.withXml section does is going through all dependencies of type providedCompile within the Gradle project configuration and changing the scope to be written into the Maven pom.xml from runtime to provided.

现在生成的pom.xml具有为javax.servlet-api设置的provided范围:

The generated pom.xml now has the provided scope set for the javax.servlet-api:

<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    [...]
    <dependencies>
        [...]
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

这篇关于如何使用提供的范围行事发布Gradle项目JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆