1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package br.com.ingenieux.mojo.beanstalk.version;
18
19 import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionRequest;
20 import com.amazonaws.services.elasticbeanstalk.model.CreateApplicationVersionResult;
21 import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsRequest;
22 import com.amazonaws.services.elasticbeanstalk.model.DescribeApplicationVersionsResult;
23 import com.amazonaws.services.elasticbeanstalk.model.S3Location;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29
30 import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
31
32
33
34
35
36
37
38
39
40 @Mojo(name = "create-application-version")
41 public class CreateApplicationVersionMojo extends AbstractBeanstalkMojo {
42
43
44
45
46 @Parameter(property = "beanstalk.applicationName", defaultValue = "${project.artifactId}", required = true)
47 String applicationName;
48
49
50
51
52
53
54 @Parameter(property = "beanstalk.versionDescription", defaultValue = "Update from beanstalk-maven-plugin")
55 String versionDescription;
56
57
58
59
60 @Parameter(property = "beanstalk.autoCreateApplication", defaultValue = "true")
61 boolean autoCreateApplication;
62
63
64
65
66 @Parameter(property = "beanstalk.s3Bucket", defaultValue = "${project.artifactId}", required = true)
67 String s3Bucket;
68
69
70
71
72 @Parameter(
73 property = "beanstalk.s3Key",
74 defaultValue = "${project.artifactId}/${project.build.finalName}-${beanstalk.versionLabel}.${project.packaging}",
75 required = true
76 )
77 String s3Key;
78
79
80
81
82 @Parameter(property = "beanstalk.versionLabel", defaultValue = "${project.version}", required = true)
83 String versionLabel;
84
85
86
87
88 @Parameter(property = "beanstalk.skipExisting", defaultValue = "true")
89 boolean skipExisting;
90
91 protected Object executeInternal() throws MojoExecutionException {
92 if (skipExisting) {
93 if (versionLabelExists()) {
94 getLog().info("VersionLabel " + versionLabel + " already exists. Skipping creation of new application-version");
95
96 return null;
97 }
98 }
99
100 CreateApplicationVersionRequest request = new CreateApplicationVersionRequest();
101
102 request.setApplicationName(applicationName);
103 request.setDescription(versionDescription);
104 request.setAutoCreateApplication(autoCreateApplication);
105
106 if (StringUtils.isNotBlank(s3Bucket) && StringUtils.isNotBlank(s3Key)) {
107 request.setSourceBundle(new S3Location(s3Bucket, s3Key));
108 }
109
110 request.setDescription(versionDescription);
111
112 request.setVersionLabel(versionLabel);
113
114 CreateApplicationVersionResult result = getService().createApplicationVersion(request);
115
116 return result.getApplicationVersion();
117 }
118
119 private boolean versionLabelExists() {
120
121
122
123 DescribeApplicationVersionsRequest davRequest =
124 new DescribeApplicationVersionsRequest().withApplicationName(applicationName).withVersionLabels(versionLabel);
125
126
127
128
129 DescribeApplicationVersionsResult result = getService().describeApplicationVersions(davRequest);
130
131
132
133
134 return !result.getApplicationVersions().isEmpty();
135 }
136 }