Updated docs with the new plugins api for Gradle

fixes gh-1217
This commit is contained in:
Marcin Grzejszczak
2019-09-26 12:18:58 +02:00
parent 6d5846463a
commit 48ecf30f65
17 changed files with 296 additions and 81 deletions

View File

@@ -35,8 +35,75 @@ To add a Gradle plugin with dependencies, you can use code similar to the follow
====
[source,groovy,indent=0,subs="verbatim,attributes",role="primary"]
.Plugin DSL
.Plugin DSL GA versions
----
// build.gradle
plugins {
id "groovy"
// this will work only for GA versions of Spring Cloud Contract
id "org.springframework.cloud.contract" version "${GAVerifierVersion}"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${GAVerifierVersion}"
}
}
dependencies {
testCompile "org.codehaus.groovy:groovy-all:${groovyVersion}"
// example with adding Spock core and Spock Spring
testCompile "org.spockframework:spock-core:${spockVersion}"
testCompile "org.spockframework:spock-spring:${spockVersion}"
testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}
----
[source,groovy,indent=0,subs="verbatim,attributes",role="secondary"]
.Plugin DSL non GA versions
----
// settings.gradle
pluginManagement {
plugins {
id "org.springframework.cloud.contract" version "${verifierVersion}"
}
repositories {
// to pick from local .m2
mavenLocal()
// for snapshots
maven { url "https://repo.spring.io/snapshot" }
// for milestones
maven { url "https://repo.spring.io/milestone" }
// for GA versions
gradlePluginPortal()
}
}
// build.gradle
plugins {
id "groovy"
id "org.springframework.cloud.contract"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${verifierVersion}"
}
}
dependencies {
testCompile "org.codehaus.groovy:groovy-all:${groovyVersion}"
// example with adding Spock core and Spock Spring
testCompile "org.spockframework:spock-core:${spockVersion}"
testCompile "org.spockframework:spock-spring:${spockVersion}"
testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}
----
[source,groovy,indent=0,subs="verbatim,attributes",role="secondary"]
.Legacy Plugin Application
----
// build.gradle
buildscript {
repositories {
mavenCentral()
@@ -44,6 +111,8 @@ buildscript {
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springboot_version}"
classpath "org.springframework.cloud:spring-cloud-contract-gradle-plugin:${verifier_version}"
// here you can also pass additional dependencies such as Pact or Kotlin spec e.g.:
// classpath "org.springframework.cloud:spring-cloud-contract-spec-kotlin:${verifier_version}"
}
}
@@ -56,39 +125,6 @@ dependencyManagement {
}
}
dependencies {
testCompile "org.codehaus.groovy:groovy-all:${groovyVersion}"
// example with adding Spock core and Spock Spring
testCompile "org.spockframework:spock-core:${spockVersion}"
testCompile "org.spockframework:spock-spring:${spockVersion}"
testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}
----
[source,groovy,indent=0,subs="verbatim,attributes",role="secondary"]
.Legacy Plugin Application
----
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springboot_version}"
}
}
apply plugin: 'groovy'
plugins {
id "org.springframework.cloud.contract" version "${GAVerifierVersion}"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:${verifier_version}"
}
}
dependencies {
testCompile "org.codehaus.groovy:groovy-all:${groovyVersion}"
// example with adding Spock core and Spock Spring
@@ -99,8 +135,6 @@ dependencies {
----
====
IMPORTANT: The Plugin DSL option will only work for GA (General Availability) releases (i.e. `2.2.0.RELEASE` etc.).
[[gradle-and-rest-assured]]
== Gradle and Rest Assured 2.0

View File

@@ -1,4 +1,25 @@
// tag::repos[]
/*
We need to use the [buildscript {}] section when we have to modify
the classpath for the plugins. If that's not the case this section
can be skipped.
If you don't need to modify the classpath (e.g. add a Pact dependency),
then you can just set the [pluginManagement {}] section in [settings.gradle] file.
// settings.gradle
pluginManagement {
repositories {
// for snapshots
maven {url "https://repo.spring.io/snapshot"}
// for milestones
maven {url "https://repo.spring.io/milestone"}
// for GA versions
gradlePluginPortal()
}
}
*/
buildscript {
repositories {
mavenCentral()

View File

@@ -1,17 +1,10 @@
buildscript {
repositories {
mavenCentral()
mavenLocal()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/release" }
maven { url 'https://repo.spring.io/plugins-snapshot' }
maven { url "https://repo.spring.io/plugins-release-local" }
maven { url "https://repo.spring.io/plugins-staging-local/" }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${findProperty('bootVersion') ?: bootVersion}"
}
plugins {
id "groovy"
id "org.springframework.boot"
id "io.spring.dependency-management"
id "maven-publish"
id "maven"
id "org.springframework.cloud.contract"
}
group = 'com.example'
@@ -25,12 +18,6 @@ repositories {
maven { url "https://repo.spring.io/release" }
}
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'
apply plugin: 'maven'
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$BOM_VERSION"
@@ -47,6 +34,10 @@ dependencies {
testCompile 'org.springframework.cloud:spring-cloud-contract-wiremock'
}
contracts {
failOnNoContracts = false
}
test {
systemProperty 'spring.profiles.active', 'gradle'
testLogging {

View File

@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -1,5 +1,21 @@
#!/usr/bin/env sh
#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
@@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
@@ -109,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

View File

@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

View File

@@ -1 +1,26 @@
pluginManagement {
plugins {
id 'org.springframework.boot'
id "org.springframework.cloud.contract" version "${verifierVersion}"
id "io.spring.dependency-management" version "1.0.8.RELEASE"
}
repositories {
// to pick from local .m2
mavenLocal()
// for snapshots
maven { url "https://repo.spring.io/libs-snapshot-local" }
// for milestones
maven { url "https://repo.spring.io/libs-milestone-local" }
// for GA versions
gradlePluginPortal()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'org.springframework.boot') {
useModule("org.springframework.boot:spring-boot-gradle-plugin:${bootVersion}")
}
}
}
}
rootProject.name = 'http-server-webclient-gradle'

View File

@@ -22,6 +22,7 @@
<module>spring-cloud-contract-pact</module>
<module>spring-cloud-contract-maven-plugin</module>
<module>spring-cloud-contract-gradle-plugin</module>
<module>spring-cloud-contract-gradle-portal-plugin</module>
</modules>
<build>

View File

@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -1,5 +1,21 @@
#!/usr/bin/env sh
#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
##
## Gradle start up script for UN*X
@@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m"'
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
@@ -109,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

View File

@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m"
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.contract.verifier.plugin
import java.time.Instant
import groovy.transform.CompileStatic
import groovy.transform.Immutable
@@ -40,6 +41,7 @@ import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.WorkResult
import org.springframework.cloud.contract.stubrunner.ContractDownloader
import org.springframework.cloud.contract.stubrunner.StubConfiguration
import org.springframework.cloud.contract.stubrunner.StubDownloader
@@ -48,8 +50,6 @@ import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.springframework.cloud.contract.verifier.converter.ToYamlConverter
import org.springframework.util.StringUtils
import java.time.Instant
// TODO: Convert to incremental task: https://docs.gradle.org/current/userguide/custom_tasks.html#incremental_tasks
/**
* Task that copies the contracts in order for the jar task to
@@ -77,6 +77,7 @@ class ContractsCopyTask extends DefaultTask {
Provider<Boolean> excludeBuildFolders
@Input
Provider<Boolean> failOnNoContracts
String contractsDirectoryPath
Provider<Directory> contractsDirectory
// All fields inside `@Nested` one are properly marked as an `@Input` to work with incremental build:
@Nested
@@ -95,32 +96,42 @@ class ContractsCopyTask extends DefaultTask {
@Input
@Optional
Property<String> contractsPath
@Input
@Optional
Instant getForceDownloadOfTheLatestContracts() {
// If we have `dynamic` version (`+` or `SNAPSHOT`) - we should mark this task as out of date for every run:
if (shouldDownloadContracts() && getStubConfiguration().isVersionChanging()) {
return Instant.now() // This will trigger re-download of contracts
} else {
}
else {
return null // This will not trigger re-download of contracts
}
}
@Optional
@InputDirectory
Provider<Directory> getContractsDirectory() {
if (shouldDownloadContracts()) {
contractsDirectoryPath = contractsDirectory.get().asFile.absolutePath
if (shouldDownloadContracts() || contractFolderMissing()) {
return null
} else {
}
else {
return contractsDirectory
}
}
private boolean contractFolderMissing() {
contractsDirectory.isPresent() && !contractsDirectory.get().asFile.exists()
}
@Internal
boolean shouldDownloadContracts() {
return StringUtils.hasText(contractDependency.getArtifactId().getOrNull()) ||
StringUtils.hasText(contractDependency.getStringNotation().getOrNull()) ||
StringUtils.hasText(contractRepository.repositoryUrl.getOrNull())
}
@Internal
StubConfiguration getStubConfiguration() {
return GradleContractsDownloaderHelper.stubConfiguration(contractDependency)
@@ -141,14 +152,22 @@ class ContractsCopyTask extends DefaultTask {
if (downloadedData) {
contractsDirectory = downloadedData.downloadedContracts
antPattern = "${downloadedData.inclusionProperties.includedRootFolderAntPattern}*.*"
} else {
logger.info("Contracts got downloaded to [" + contractsDirectory + "]")
}
else if (config.contractsDirectory != null && config.contractsDirectory.isPresent()) {
contractsDirectory = config.contractsDirectory.get().asFile
antPattern = "**/"
}
else {
contractsDirectory = null
}
logger.info("For project [{}] will use contracts provided in the folder [{}]", project.name, contractsDirectory)
final String contractsRepository = config.contractRepository.repositoryUrl.isPresent() ? config.contractRepository.repositoryUrl : ""
throwExceptionWhenFailOnNoContracts(contractsDirectory, contractsRepository)
if (contractsDirectory == null) {
logger.info("Contracts directory not set and contracts weren't downloaded. There's nothing to copy")
return
}
final String slashSeparatedGroupId = project.group.toString().replace(".", File.separator)
final String slashSeparatedAntPattern = antPattern.replace(slashSeparatedGroupId, project.group.toString())
final File output = config.copiedContractsFolder.get().asFile
@@ -225,7 +244,8 @@ class ContractsCopyTask extends DefaultTask {
downloadedContracts: contractsSubDirIfPresent(downloadedContracts, logger),
inclusionProperties: inclusionProperties
)
} else {
}
else {
return null
}
}
@@ -251,10 +271,9 @@ class ContractsCopyTask extends DefaultTask {
}
return
}
if (config.failOnNoContracts.get() && (!file.exists() || file.listFiles().length == 0)) {
throw new GradleException("Contracts could not be found: ["
+ file.getAbsolutePath()
+ "] .\nPlease make sure that the contracts were defined, or set the [failOnNoContracts] flag to [false]")
if (config.failOnNoContracts.get() && (file == null || !file.exists() || file.listFiles().length == 0)) {
String path = file != null ? file.getAbsolutePath() : config.contractsDirectoryPath
throw new GradleException("Contracts could not be found: [" + path + "]\nPlease make sure that the contracts were defined, or set the [failOnNoContracts] flag to [false]")
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.plugin
import org.gradle.api.Project

View File

@@ -1,11 +1,28 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.verifier.plugin
import org.gradle.api.internal.provider.DefaultPropertyState
import org.gradle.api.internal.provider.DefaultProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.springframework.cloud.contract.stubrunner.StubConfiguration
import spock.lang.Specification
import org.springframework.cloud.contract.stubrunner.StubConfiguration
/**
* @author Marcin Grzejszczak
* @author Anatoliy Balakirev
@@ -81,6 +98,6 @@ class GradleContractsDownloaderHelperSpec extends Specification {
// Have to use this internal property impl here. Is there some better way?
static <T> Property<T> prop(Class<T> aClass) {
return new DefaultPropertyState(aClass)
return new DefaultProperty(aClass)
}
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-tools</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<groupId>org.springframework.cloud.contract</groupId>
<artifactId>org.springframework.cloud.contract.gradle.plugin</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Contract Gradle Portal Plugin</name>
<description>Spring Cloud Contract Gradle Portal Plugin</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-gradle-plugin</artifactId>
</dependency>
</dependencies>
</project>