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.HashMap;
21 import java.util.Map;
22
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugins.annotations.Mojo;
25 import org.apache.maven.plugins.annotations.Parameter;
26 import org.apache.maven.project.MavenProject;
27
28 import org.springframework.cloud.contract.stubrunner.ContractProjectUpdater;
29 import org.springframework.cloud.contract.stubrunner.ScmStubDownloaderBuilder;
30 import org.springframework.cloud.contract.stubrunner.StubRunnerOptions;
31 import org.springframework.cloud.contract.stubrunner.StubRunnerOptionsBuilder;
32 import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
33 import org.springframework.util.StringUtils;
34
35
36
37
38
39
40 @Mojo(name = "pushStubsToScm")
41 public class PushStubsToScmMojo extends AbstractMojo {
42
43 @Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
44 private File projectBuildDirectory;
45
46 @Parameter(property = "stubsDirectory", defaultValue = "${project.build.directory}/stubs")
47 private File outputDirectory;
48
49
50
51
52 @Parameter(property = "spring.cloud.contract.verifier.skip", defaultValue = "false")
53 private boolean skip;
54
55
56
57
58 @Parameter(property = "spring.cloud.contract.verifier.publish-stubs-to-scm.skip", defaultValue = "false")
59 private boolean taskSkip;
60
61 @Parameter(defaultValue = "${project}", readonly = true)
62 private MavenProject project;
63
64
65
66
67 @Parameter(property = "contractsRepositoryUsername")
68 private String contractsRepositoryUsername;
69
70
71
72
73 @Parameter(property = "contractsRepositoryPassword")
74 private String contractsRepositoryPassword;
75
76
77
78
79
80
81 @Parameter(property = "contractsRepositoryUrl")
82 private String contractsRepositoryUrl;
83
84
85
86
87 @Parameter(property = "contractsMode", defaultValue = "CLASSPATH")
88 private StubRunnerProperties.StubsMode contractsMode;
89
90
91
92
93
94 @Parameter(property = "deleteStubsAfterTest", defaultValue = "true")
95 private boolean deleteStubsAfterTest;
96
97
98
99
100
101 @Parameter(property = "contractsProperties")
102 private Map<String, String> contractsProperties = new HashMap<>();
103
104 @Override
105 public void execute() {
106 if (this.skip || this.taskSkip) {
107 getLog().info("Skipping Spring Cloud Contract Verifier execution: spring.cloud.contract.verifier.skip="
108 + this.skip + ", spring.cloud.contract.verifier.publish-stubs-to-scm.skip=" + this.taskSkip);
109 return;
110 }
111 if (!StringUtils.hasText(this.contractsRepositoryUrl)
112 || !ScmStubDownloaderBuilder.isProtocolAccepted(this.contractsRepositoryUrl)) {
113 getLog().info("Skipping pushing stubs to scm since your "
114 + "[contractsRepositoryUrl] property doesn't match any of the accepted protocols");
115 return;
116 }
117 String projectName = this.project.getGroupId() + ":" + this.project.getArtifactId() + ":"
118 + this.project.getVersion();
119 getLog().info("Pushing Stubs to SCM for project [" + projectName + "]");
120 new ContractProjectUpdater(buildOptions()).updateContractProject(projectName, this.outputDirectory.toPath());
121 }
122
123 StubRunnerOptions buildOptions() {
124 StubRunnerOptionsBuilder builder = new StubRunnerOptionsBuilder()
125 .withOptions(StubRunnerOptions.fromSystemProps()).withStubRepositoryRoot(this.contractsRepositoryUrl)
126 .withStubsMode(this.contractsMode).withUsername(this.contractsRepositoryUsername)
127 .withPassword(this.contractsRepositoryPassword).withDeleteStubsAfterTest(this.deleteStubsAfterTest)
128 .withProperties(this.contractsProperties);
129 return builder.build();
130 }
131
132 }