diff --git a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunner.java b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunner.java index d1d0cb9293..0285672bcb 100644 --- a/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunner.java +++ b/spring-cloud-contract-stub-runner/src/main/java/org/springframework/cloud/contract/stubrunner/StubRunner.java @@ -17,6 +17,7 @@ package org.springframework.cloud.contract.stubrunner; import java.io.File; +import java.io.FilenameFilter; import java.io.IOException; import java.net.URL; import java.nio.file.Files; @@ -77,14 +78,12 @@ public class StubRunner implements StubRunning { this.stubsConfiguration.artifactId + "_" + stubs.getPort(this.stubsConfiguration.toColonSeparatedDependencyNotation())); try { - if (outputMappings.exists()) { - outputMappings.delete(); - } outputMappings.getParentFile().mkdirs(); + clearOldFiles(outputMappings.getParentFile(), this.stubsConfiguration.artifactId); outputMappings.createNewFile(); Files.write(Paths.get(outputMappings.toURI()), registeredMappings.getBytes()); if (log.isDebugEnabled()) { - log.debug("Stored the mappings for artifactid [" + this.stubsConfiguration.artifactId + " at [" + outputMappings + "] location"); + log.debug("Stored the mappings for artifactid [" + this.stubsConfiguration.artifactId + "] at [" + outputMappings + "] location"); } } catch (IOException e) { @@ -96,6 +95,27 @@ public class StubRunner implements StubRunning { return stubs; } + private void clearOldFiles(File outputFolder, final String filename) { + File[] files = outputFolder.listFiles(new FilenameFilter() { + @Override public boolean accept(final File dir, final String name) { + return name.startsWith(filename); + } + }); + if (files == null) { + if(log.isDebugEnabled()) { + log.debug("Failed to retrieve any mappings"); + } + return; + } + for (final File file : files) { + if (!file.delete()) { + if(log.isDebugEnabled()) { + log.debug("Exception occurred while trying to remove [" + file.getAbsolutePath() + "]"); + } + } + } + } + @Override public URL findStubUrl(String groupId, String artifactId) { return this.localStubRunner.findStubUrl(groupId, artifactId); diff --git a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleSpec.groovy b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleSpec.groovy index 90a3ce5cde..ac16ed37f3 100644 --- a/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleSpec.groovy +++ b/spring-cloud-contract-stub-runner/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit/StubRunnerRuleSpec.groovy @@ -39,7 +39,7 @@ class StubRunnerRuleSpec extends Specification { .repoRoot(StubRunnerRuleSpec.getResource("/m2repo/repository").toURI().toString()) .downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance") .downloadStub("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer") - .withMappingsOutputFolder("target/outputmappingsforule") + .withMappingsOutputFolder("target/outputmappingsforrule") def 'should start WireMock servers'() { @@ -61,7 +61,7 @@ class StubRunnerRuleSpec extends Specification { when: def url = rule.findStubUrl('fraudDetectionServer') then: - new File("target/outputmappingsforule", "fraudDetectionServer_${url.port}").exists() + new File("target/outputmappingsforrule", "fraudDetectionServer_${url.port}").exists() } // end::classrule[] }