【问题标题】:How gradle plugin using DSL works?使用 DSL 的 gradle 插件如何工作?
【发布时间】:2020-11-15 09:08:43
【问题描述】:

我有点糊涂了,求大神指正

从 Gradle 插件门户中,它说使用以下方法添加插件

-------Using the plugins DSL:--------------------
plugins {
    id "com.github.edeandrea.xjc-generation" version "1.4"
}

-------Using legacy plugin application:--------
buildscript {
   repositories {
      maven {
        url "https://plugins.gradle.org/m2/"
      }
   }
   dependencies {
      classpath "gradle.plugin.com.github.edeandrea:xjc-generation-gradle-plugin:1.4"
   }
}

apply plugin: "com.github.edeandrea.xjc-generation"

遗留方法非常清楚:根据我的理解,它会在存储库(指定的 maven URL)中找到该依赖项并将其添加到类路径中,以便应用程序可以选择指定的插件

但在新方法(即使用 DSL)的情况下,它什么也没说,我的理解是插件必须在 Gradle 官方仓库中发布才能访问它,但是 build.gradle 文件如何知道在哪里找到它,这意味着我们需要添加repositories {} 块并在其中指定mavencentral() 以使其工作还是需要添加其他任何东西?

settings.gradle 中的 pluginMangement 块也控制新的插件方法,我尝试定义 repositories {} 块并添加了 mavencentral() 但它不起作用。

简而言之,使用 DSL 添加的插件是如何工作的,哪个块告诉从哪里找到该插件,然后哪一行告诉将其添加到类路径中。

更新: 例如,repositories-buildscript 部分中的 maven 块告诉在 url "https://plugins.gradle.org/m2/ 位置下查看插件,并且 dependencies-buildscript 中的类路径标记告诉将此依赖项添加到应用程序类路径中

更正: mavenCentral()

【问题讨论】:

    标签: java gradle plugins build.gradle gradle-plugin


    【解决方案1】:

    插件 DSL 可以被认为是 declarative 应用插件的方式,而遗留插件应用程序可以被认为是 imperative

    但是在新方法的情况下(即使用 DSL)它什么也没说,我的理解是插件必须在官方 Gradle 存储库中发布才能访问它,但是 build.gradle 文件如何知道在哪里找到它,这意味着我们需要添加 repositories {} 块并在其中指定 mavencentral() 以使其工作还是需要添加其他任何东西?

    这在文档中有详细解释:Plugin Marker Artifacts

    settings.gradle 中的 pluginMangement 块也控制新的插件方法,我尝试定义存储库 {} 块并添加了 mavencentral() 但它不起作用。

    mavenCentral(),不是mavencentral()

    pluginManagement {
        repositories {
            mavenCentral()
        }
    }
    
    rootProject.name = "example"
    

    简而言之,使用 DSL 添加的插件是如何工作的,哪个块告诉从哪里找到该插件,然后哪一行告诉将其添加到类路径中。

    这在文档中都有解释:Applying plugins with the plugins DSL

    【讨论】:

    • 尝试使用 mavenCentral() 和其他几个存储库,如 gradlePluginPortal() 和 jcenter(),但在使用 Plugin DSL 尝试时,我仍然在存储库下收到插件未找到错误。
    【解决方案2】:

    所以我在我的 gradle+Java 11 项目中遇到了同样的问题,我使用 https://github.com/edeandrea/xjc-generation-gradle-plugin 解决了这个问题。它并不像看起来那么简单。通常,有必要添加其他依赖项,这是我的情况。我的build.gradlw 看起来像:

    plugins {
        id 'java'
        id "com.github.davidmc24.gradle.plugin.avro" version "1.0.0"
        id 'idea' // optional (to generate IntelliJ IDEA project files)
        id 'com.github.edeandrea.xjc-generation' version '1.6'
    }
    
    description = "spring-kafka-stream"
    group = 'com.github.felipegutierrez.explore.spring'
    version = '0.0.1'
    sourceCompatibility = '11'
    
    ext {
        jaxbVersion = '2.3.1'
    }
    
    dependencies {
        ...
        implementation "javax.xml.bind:jaxb-api:$jaxbVersion"
        xjc "javax.xml.bind:jaxb-api:$jaxbVersion"
        xjc "org.glassfish.jaxb:jaxb-runtime:$jaxbVersion"
        xjc "org.glassfish.jaxb:jaxb-xjc:$jaxbVersion"
    }
    
    xjcGeneration {
        defaultAdditionalXjcOptions = ['encoding': 'UTF-8']
        schemas {
            order {
                schemaFile = 'order.xsd'
                javaPackageName = 'com.github.felipegutierrez.explore.spring.model'
            }
        }
    }
    

    除了所有 xjc 依赖项之外,我还必须添加 implementation "javax.xml.bind:jaxb-api:$jaxbVersion"。然后我创建文件src/main/schemas/xjc/order.xsd:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="order">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="order-by" type="xs:string"/>
                    <xs:element name="ship-to">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="name" type="xs:string"/>
                                <xs:element name="address" type="xs:string"/>
                                <xs:element name="city" type="xs:string"/>
                                <xs:element name="country" type="xs:string"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="item" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="title" type="xs:string"/>
                                <xs:element name="note" type="xs:string" minOccurs="0"/>
                                <xs:element name="quantity" type="xs:positiveInteger"/>
                                <xs:element name="price" type="xs:decimal"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="order-id" type="xs:string" use="required"/>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      相关资源
      最近更新 更多