Gradle插件使用DSL的工作原理是怎样的?

huangapple 未分类评论44阅读模式
英文:

How gradle plugin using DSL works?

问题

我感到困惑,请有人纠正我

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

-------使用插件DSL:--------------------

plugins {
    id "com.github.edeandrea.xjc-generation" version "1.4"
}

-------使用旧的插件应用方式:--------

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中的classpath标签告诉将此依赖项添加到应用程序类路径中

更正: mavenCentral()

英文:

I am getting confused, please someone correct me

From Gradle plugin portal it says use below approach to add a plugin

-------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"

The legacy approach is pretty much clear: as per my understanding it will find that dependency in repositories (specified maven URL) and add it to the classpath so that the application can pick the specified plugin

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 ?

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.

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 the classpath.

Updates:
For ex, the maven block in repositories-buildscript section tells to look the plugin under url "https://plugins.gradle.org/m2/ location and classpath tag in dependencies-buildscript tells to add this dependency into application classpath

Correction: mavenCentral()

答案1

得分: 0

The plugins DSL can be thought of as the declarative way of applying plugins and the legacy plugin application can be thought of as 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 ?

This is thoroughly explained in the documentation: 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.

It is mavenCentral(), not 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.

This is all explained in the documentation: Applying plugins with the plugins DSL

英文:

The plugins DSL can be thought of as the declarative way of applying plugins and the legacy plugin application can be thought of as 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 ?

This is thoroughly explained in the documentation: 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.

It is mavenCentral(), not 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.

This is all explained in the documentation: Applying plugins with the plugins DSL

答案2

得分: 0

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

plugins {
	id 'java'
	id "com.github.davidmc24.gradle.plugin.avro" version "1.0.0"
	id 'idea' // 可选(用于生成IntelliJ IDEA项目文件)
	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>
英文:

So I was facing the same issue in my gradle+Java 11 project and I solved using https://github.com/edeandrea/xjc-generation-gradle-plugin. It is not as simple as it seems to be. Usually, it is necessary to add other dependencies, which was my case. my build.gradlw looks like:

plugins {
	id &#39;java&#39;
	id &quot;com.github.davidmc24.gradle.plugin.avro&quot; version &quot;1.0.0&quot;
	id &#39;idea&#39; // optional (to generate IntelliJ IDEA project files)
	id &#39;com.github.edeandrea.xjc-generation&#39; version &#39;1.6&#39;
}

description = &quot;spring-kafka-stream&quot;
group = &#39;com.github.felipegutierrez.explore.spring&#39;
version = &#39;0.0.1&#39;
sourceCompatibility = &#39;11&#39;

ext {
	jaxbVersion = &#39;2.3.1&#39;
}

dependencies {
    ...
	implementation &quot;javax.xml.bind:jaxb-api:$jaxbVersion&quot;
	xjc &quot;javax.xml.bind:jaxb-api:$jaxbVersion&quot;
	xjc &quot;org.glassfish.jaxb:jaxb-runtime:$jaxbVersion&quot;
	xjc &quot;org.glassfish.jaxb:jaxb-xjc:$jaxbVersion&quot;
}

xjcGeneration {
	defaultAdditionalXjcOptions = [&#39;encoding&#39;: &#39;UTF-8&#39;]
	schemas {
		order {
			schemaFile = &#39;order.xsd&#39;
			javaPackageName = &#39;com.github.felipegutierrez.explore.spring.model&#39;
		}
	}
}

Besides all the xjc dependencies I had to add implementation &quot;javax.xml.bind:jaxb-api:$jaxbVersion&quot;. Then I create the file src/main/schemas/xjc/order.xsd:

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

huangapple
  • 本文由 发表于 2020年7月26日 01:43:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/63091552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定