Added excludeBuildFolders property

without this change when we work with the repo with common contracts then in the logs we can see that a lot of unnecessary files are processed. Those files are related to the fact that `target` / `build` folders are created and reside in the path in which we're searching for contracts. Due to this we have duplicates in terms of converting files.
with this change we add the `excludeBuildFolders` property that is by default turned off. If you enable it in the `pom.xml` that is used by the consumers to install stubs locally then the target folder gets ignored.

fixes #167
This commit is contained in:
Marcin Grzejszczak
2016-12-28 12:27:56 +01:00
parent 60648f6203
commit 0fd91976f3
14 changed files with 211 additions and 4 deletions

View File

@@ -58,12 +58,20 @@ class RecursiveFilesConverter {
properties.excludedFiles as Set, [] as Set, properties.includedContracts)
ListMultimap<Path, ContractMetadata> contracts = scanner.findContracts()
if (log.isDebugEnabled()) {
log.debug("Found the following contracts $contracts")
log.debug("Found the following contracts ${contracts}")
log.debug("Exclude build folder: [${properties.isExcludeBuildFolders()}]")
}
contracts.asMap().entrySet().each { entry ->
entry.value.each { ContractMetadata contract ->
File sourceFile = contract.path.toFile()
try {
String path = sourceFile.path
if (properties.isExcludeBuildFolders() && (matchesPath(path, "target") || matchesPath(path, "build"))) {
if (log.isDebugEnabled()) {
log.debug("Exclude build folder is set. Path [${path}] contains [target] or [build] in its path")
}
return
}
if (!singleFileConverter.canHandleFileName(sourceFile.name)) {
return
}
@@ -81,6 +89,10 @@ class RecursiveFilesConverter {
}
}
private boolean matchesPath(String path, String folder) {
return path.matches("^.*${File.separator}${folder}${File.separator}.*\$")
}
private Path createAndReturnTargetDirectory(File sourceFile) {
Path relativePath = Paths.get(properties.contractsDslDir.toURI()).relativize(sourceFile.parentFile.toPath())
Path absoluteTargetPath = outMappingsDir.toPath().resolve(relativePath)

View File

@@ -134,6 +134,13 @@ class ContractVerifierExtension {
*/
Map<String, String> baseClassMappings = [:]
/**
* If set to true then the {@code target} or {@code build} folders are getting
* excluded from any operations. This is used out of the box when working with
* common repo with contracts.
*/
boolean excludeBuildFolders
void contractDependency(@DelegatesTo(Dependency) Closure closure) {
closure.delegate = contractDependency
closure.call()

View File

@@ -32,6 +32,9 @@ class ContractsCopyTask extends ConventionTask {
project.copy {
from(file)
include(antPattern)
if (props.isExcludeBuildFolders()) {
exclude "**/target/**", "**/build/**"
}
into(outputContractsFolder)
}
}

View File

@@ -27,7 +27,8 @@ class ExtensionToProperties {
stubsSuffix: extension.stubsSuffix,
assertJsonSize: extension.assertJsonSize,
packageWithBaseClasses: extension.packageWithBaseClasses,
baseClassMappings: extension.baseClassMappings
baseClassMappings: extension.baseClassMappings,
excludeBuildFolders: extension.excludeBuildFolders
)
}
}

View File

@@ -88,6 +88,7 @@ class SpringCloudContractVerifierGradlePlugin implements Plugin<Project> {
}
}
//TODO: Deprecate this since starting with 1.1.x
private void addProjectDependencies(Project project) {
project.dependencies.add("testCompile", "com.github.tomakehurst:wiremock:2.1.7")
project.dependencies.add("testCompile", "com.toomuchcoding.jsonassert:jsonassert:0.4.7")

View File

@@ -109,6 +109,13 @@ public class ConvertMojo extends AbstractMojo {
@Parameter(property = "contractsWorkOffline", defaultValue = "false")
private boolean contractsWorkOffline;
/**
* If {@code true} then any file laying in a path that contains {@code build} or {@code target}
* will get excluded in further processing.
*/
@Parameter(property = "excludeBuildFolders", defaultValue = "false")
private boolean excludeBuildFolders;
@Component(role = MavenResourcesFiltering.class, hint = "default")
private MavenResourcesFiltering mavenResourcesFiltering;
@@ -129,6 +136,7 @@ public class ConvertMojo extends AbstractMojo {
}
// download contracts, unzip them and pass as output directory
ContractVerifierConfigProperties config = new ContractVerifierConfigProperties();
config.setExcludeBuildFolders(this.excludeBuildFolders);
File contractsDirectory = new MavenContractsDownloader(this.project, this.contractDependency,
this.contractsPath, this.contractsRepositoryUrl, this.contractsWorkOffline, getLog(),
this.aetherStubDownloaderFactory, this.repoSession).downloadAndUnpackContractsIfRequired(config, this.contractsDirectory);

View File

@@ -54,6 +54,10 @@ class CopyContracts {
+ "the final JAR with stubs.");
Resource resource = new Resource();
resource.addInclude(this.config.getIncludedRootFolderAntPattern() + "*.*");
if (this.config.isExcludeBuildFolders()) {
resource.addExclude("**/target/**");
resource.addExclude("**/build/**");
}
resource.setDirectory(contractsDirectory.getAbsolutePath());
MavenResourcesExecution execution = new MavenResourcesExecution();
execution.setResources(Collections.singletonList(resource));

View File

@@ -213,4 +213,17 @@ public class PluginUnitTest {
File test = new File(basedir, path);
then(FileUtils.readFileToString(test)).contains("extends TestBase").contains("import com.example.TestBase");
}
@Test
public void shouldGenerateStubsForCommonRepoWithTargetFolder() throws Exception {
File basedir = this.resources.getBasedir("common-repo");
this.maven.executeMojo(basedir, "convert");
assertFilesNotPresent(basedir, "target/generated-test-sources/contracts/");
// there will be no stubs cause all files are copied to `target` folder
assertFilesNotPresent(basedir, "target/stubs/mappings/");
assertFilesPresent(basedir, "target/stubs/contracts/consumer1/Messaging.groovy");
assertFilesPresent(basedir, "target/stubs/contracts/pom.xml");
}
}

View File

@@ -0,0 +1,37 @@
/**
*
* Copyright 2013-2016 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
*
* http://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.
*/
org.springframework.cloud.contract.spec.Contract.make {
label 'some_label'
input {
messageFrom('jms:input')
messageBody([
bookName: 'foo'
])
messageHeaders {
header('sample', 'header')
}
}
outputMessage {
sentTo('jms:output')
body([
bookName: 'foo'
])
headers {
header('BOOK-NAME', 'foo')
}
}
}

View File

@@ -0,0 +1,34 @@
/**
*
* Copyright 2013-2016 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
*
* http://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.
*/
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'POST'
url('/users') {
}
headers {
header 'Content-Type': 'application/json'
}
body '''{ "login" : "john", "name": "John The Contract" }'''
}
response {
status 200
headers {
header 'Location': '/users/john'
}
}
}

View File

@@ -0,0 +1,34 @@
/**
*
* Copyright 2013-2016 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
*
* http://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.
*/
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'POST'
url('/users') {
}
headers {
header 'Content-Type': 'application/json'
}
}
response {
status 200
headers {
header 'Location': '/users/john'
}
body '''{ "list" : [ "login", "john", "name", "John The Contract" ] }'''
}
}

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013-2016 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
http://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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.cloud.verifier.sample</groupId>
<artifactId>common-repo</artifactId>
<version>0.1</version>
<properties>
<skipTests>true</skipTests>
<excludeBuildFolders>true</excludeBuildFolders>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<configuration>
<!-- By default it would search under src/test/resources/ -->
<contractsDirectory>${project.basedir}</contractsDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -110,8 +110,8 @@ class ContractVerifierConfigProperties {
String includedContracts = ".*"
/**
* A ant pattern to match files. Relates to contracts, stubs etc. You can append
* any kind of files you wish e.g {@code $includedRootFolderAntPattern/*.groovy}
* A ant pattern to match files. Gets updated when using repo with common contracts
* to reflect the path to proper folder with contracts.
*/
String includedRootFolderAntPattern = "**/"
@@ -137,4 +137,11 @@ class ContractVerifierConfigProperties {
*/
Map<String, String> baseClassMappings
/**
* If set to true then the {@code target} or {@code build} folders are getting
* excluded from any operations. This is used out of the box when working with
* common repo with contracts.
*/
boolean excludeBuildFolders
}