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.util.List;
20  
21  import org.apache.maven.model.Dependency;
22  import org.apache.maven.model.Plugin;
23  import org.apache.maven.project.MavenProject;
24  import org.codehaus.plexus.archiver.jar.Manifest;
25  import org.codehaus.plexus.archiver.jar.ManifestException;
26  
27  final class ManifestCreator {
28  
29  	private ManifestCreator() {
30  		throw new IllegalStateException("Can't instantiate a utility class");
31  	}
32  
33  	public static Manifest createManifest(MavenProject project) throws ManifestException {
34  		Manifest manifest = new Manifest();
35  		Plugin verifierMavenPlugin = findMavenPlugin(project.getBuildPlugins());
36  		if (verifierMavenPlugin != null) {
37  			manifest.addConfiguredAttribute(new Manifest.Attribute("Spring-Cloud-Contract-Maven-Plugin-Version",
38  					verifierMavenPlugin.getVersion()));
39  		}
40  		if (verifierMavenPlugin != null && !verifierMavenPlugin.getDependencies().isEmpty()) {
41  			Dependency verifierDependency = findVerifierDependency(verifierMavenPlugin.getDependencies());
42  			if (verifierDependency != null) {
43  				String verifierVersion = verifierDependency.getVersion();
44  				manifest.addConfiguredAttribute(
45  						new Manifest.Attribute("Spring-Cloud-Contract-Verifier-Version", verifierVersion));
46  			}
47  		}
48  		return manifest;
49  	}
50  
51  	private static Plugin findMavenPlugin(List<Plugin> plugins) {
52  		for (Plugin plugin : plugins) {
53  			if ("spring-cloud-contract-maven-plugin".equals(plugin.getArtifactId())) {
54  				return plugin;
55  			}
56  		}
57  		return null;
58  	}
59  
60  	private static Dependency findVerifierDependency(List<Dependency> deps) {
61  		for (Dependency dep : deps) {
62  			if ("spring-cloud-contract-verifier".equals(dep.getArtifactId())) {
63  				return dep;
64  			}
65  		}
66  		return null;
67  	}
68  
69  }