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.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 }