有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java gradle插件如何使用DSL工作?

我弄糊涂了,请有人纠正我

从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 repo中发布才能访问它,但是build.gradle文件如何知道在哪里可以找到它,这意味着我们需要添加repositories {}块并在其中指定mavencentral()以使其工作,还是需要添加任何其他内容

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

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

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

更正:mavenCentral()


共 (2) 个答案

  1. # 1 楼答案

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

    But in case of new approach (i.e. using DSL) it doesn't say anything, what I understand is the plugin must be published in official Gradle repo in order to access it, but how the build.gradle files know where to find it, meaning do we need to add repositories {} block and specify mavencentral() in it to make it work or need to add anything else ?

    文档中对此进行了详细解释:Plugin Marker Artifacts

    Also pluginMangement block present in settings.gradle controls the new plugin approach, I have tried defining the repositories {} block and added mavencentral() but it doesn't work.

    它是mavenCentral(),而不是mavencentral()

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

    In short, how plugin added using DSL works, which block tells from where to find that plugin and then which line tells to add it in classpath.

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

  2. # 2 楼答案

    因此,我在gradle+Java11项目中遇到了同样的问题,我使用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>