View Javadoc
1   /*
2    * Copyright 2013-2020 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The generated stubs get committed to the SCM repo and pushed to origin.
37   *
38   * @author Marcin Grzejszczak
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  	 * Set this to "true" to bypass the whole Verifier execution.
51  	 */
52  	@Parameter(property = "spring.cloud.contract.verifier.skip", defaultValue = "false")
53  	private boolean skip;
54  
55  	/**
56  	 * Set this to "true" to bypass only JAR creation.
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  	 * The user name to be used to connect to the repo with contracts.
66  	 */
67  	@Parameter(property = "contractsRepositoryUsername")
68  	private String contractsRepositoryUsername;
69  
70  	/**
71  	 * The password to be used to connect to the repo with contracts.
72  	 */
73  	@Parameter(property = "contractsRepositoryPassword")
74  	private String contractsRepositoryPassword;
75  
76  	/**
77  	 * The URL from which a contracts should get downloaded. If not provided but
78  	 * artifactid / coordinates notation was provided then the current Maven's build
79  	 * repositories will be taken into consideration.
80  	 */
81  	@Parameter(property = "contractsRepositoryUrl")
82  	private String contractsRepositoryUrl;
83  
84  	/**
85  	 * Picks the mode in which stubs will be found and registered.
86  	 */
87  	@Parameter(property = "contractsMode", defaultValue = "CLASSPATH")
88  	private StubRunnerProperties.StubsMode contractsMode;
89  
90  	/**
91  	 * If set to {@code false} will NOT delete stubs from a temporary folder after running
92  	 * tests.
93  	 */
94  	@Parameter(property = "deleteStubsAfterTest", defaultValue = "true")
95  	private boolean deleteStubsAfterTest;
96  
97  	/**
98  	 * Map of properties that can be passed to custom
99  	 * {@link org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder}.
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 }