1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.cloud.contract.maven.verifier;
18
19 import java.io.File;
20 import java.util.Collections;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.maven.execution.MavenSession;
25 import org.apache.maven.model.Resource;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.project.MavenProject;
28 import org.apache.maven.shared.filtering.MavenFilteringException;
29 import org.apache.maven.shared.filtering.MavenResourcesExecution;
30 import org.apache.maven.shared.filtering.MavenResourcesFiltering;
31
32 import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties;
33
34 class CopyContracts {
35
36 private static final Log log = LogFactory.getLog(CopyContracts.class);
37
38 private final MavenProject project;
39
40 private final MavenSession mavenSession;
41
42 private final MavenResourcesFiltering mavenResourcesFiltering;
43
44 private final ContractVerifierConfigProperties config;
45
46 CopyContracts(MavenProject project, MavenSession mavenSession, MavenResourcesFiltering mavenResourcesFiltering,
47 ContractVerifierConfigProperties config) {
48 this.project = project;
49 this.mavenSession = mavenSession;
50 this.mavenResourcesFiltering = mavenResourcesFiltering;
51 this.config = config;
52 }
53
54 public void copy(File contractsDirectory, File outputDirectory) throws MojoExecutionException {
55 log.info("Copying Spring Cloud Contract Verifier contracts to [" + outputDirectory + "]"
56 + ". Only files matching [" + this.config.getIncludedContracts() + "] pattern will end up in "
57 + "the final JAR with stubs.");
58 Resource resource = new Resource();
59 String includedRootFolderAntPattern = this.config.getIncludedRootFolderAntPattern() + "*.*";
60 String slashSeparatedGroupIdAntPattern = slashSeparatedGroupIdAntPattern(includedRootFolderAntPattern);
61 String dotSeparatedGroupIdAntPattern = dotSeparatedGroupIdAntPattern(includedRootFolderAntPattern);
62
63 resource.addInclude(slashSeparatedGroupIdAntPattern);
64 if (!slashSeparatedGroupIdAntPattern.equals(dotSeparatedGroupIdAntPattern)) {
65
66 resource.addInclude(dotSeparatedGroupIdAntPattern);
67 }
68 if (this.config.isExcludeBuildFolders()) {
69 resource.addExclude("**/target/**");
70 resource.addExclude("**/.mvn/**");
71 resource.addExclude("**/build/**");
72 resource.addExclude("**/.gradle/**");
73 }
74 resource.setDirectory(contractsDirectory.getAbsolutePath());
75 MavenResourcesExecution execution = new MavenResourcesExecution();
76 execution.setResources(Collections.singletonList(resource));
77 execution.setOutputDirectory(outputDirectory);
78 execution.setMavenProject(this.project);
79 execution.setEncoding("UTF-8");
80 execution.setMavenSession(this.mavenSession);
81 execution.setInjectProjectBuildFilters(false);
82 execution.setOverwrite(true);
83 execution.setIncludeEmptyDirs(false);
84 execution.setFilterFilenames(false);
85 execution.setFilters(Collections.emptyList());
86 try {
87 this.mavenResourcesFiltering.filterResources(execution);
88 }
89 catch (MavenFilteringException e) {
90 throw new MojoExecutionException(e.getMessage(), e);
91 }
92 }
93
94 private String slashSeparatedGroupIdAntPattern(String includedRootFolderAntPattern) {
95 if (includedRootFolderAntPattern.contains(slashSeparatedGroupId())) {
96 return includedRootFolderAntPattern;
97 }
98 else if (includedRootFolderAntPattern.contains(dotSeparatedGroupId())) {
99 return includedRootFolderAntPattern.replace(dotSeparatedGroupId(), slashSeparatedGroupId());
100 }
101 return includedRootFolderAntPattern;
102 }
103
104 private String dotSeparatedGroupIdAntPattern(String includedRootFolderAntPattern) {
105 if (includedRootFolderAntPattern.contains(dotSeparatedGroupId())) {
106 return includedRootFolderAntPattern;
107 }
108 else if (includedRootFolderAntPattern.contains(slashSeparatedGroupId())) {
109 return includedRootFolderAntPattern.replace(slashSeparatedGroupId(), dotSeparatedGroupId());
110 }
111 return includedRootFolderAntPattern;
112 }
113
114 private String slashSeparatedGroupId() {
115 return this.project.getGroupId().replace(".", File.separator);
116 }
117
118 private String dotSeparatedGroupId() {
119 return this.project.getGroupId();
120 }
121
122 }