1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package br.com.ingenieux.mojo.beanstalk.bundle;
18
19 import com.amazonaws.services.s3.model.PutObjectRequest;
20 import com.amazonaws.services.s3.model.PutObjectResult;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.plugin.MojoFailureException;
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 java.io.File;
29
30 import br.com.ingenieux.mojo.aws.util.BeanstalkerS3Client;
31 import br.com.ingenieux.mojo.beanstalk.AbstractBeanstalkMojo;
32
33 import static java.lang.String.format;
34
35
36
37
38
39
40 @Mojo(name = "upload-source-bundle")
41 public class UploadSourceBundleMojo extends AbstractBeanstalkMojo {
42
43
44
45
46 @Parameter(property = "beanstalk.s3Bucket")
47 String s3Bucket;
48
49
50
51
52 @Parameter(
53 property = "beanstalk.s3Key",
54 defaultValue = "${project.artifactId}/${project.build.finalName}-${beanstalk.versionLabel}.${project.packaging}",
55 required = true
56 )
57 String s3Key;
58
59
60
61
62
63 @Parameter(property = "beanstalk.multipartUpload", defaultValue = "true")
64 boolean multipartUpload = false;
65
66
67
68
69 @Parameter(property = "beanstalk.artifactFile", defaultValue = "${project.build.directory}/${project.build.finalName}.${project.packaging}", required = true)
70 File artifactFile;
71
72
73
74
75 @Parameter(property = "beanstalk.silentUpload", defaultValue = "false")
76 boolean silentUpload = false;
77
78
79
80
81 @Parameter(property = "beanstalk.versionLabel", required = true)
82 String versionLabel;
83
84 @Parameter(defaultValue = "${project}")
85 MavenProject project;
86
87 protected Object executeInternal() throws Exception {
88 String path = artifactFile.getPath();
89
90 if (!(path.endsWith(".war") || path.endsWith(".jar") || path.endsWith(".zip"))) {
91 getLog().warn("Not a war/jar/zip file. Skipping");
92
93 return null;
94 }
95
96 if (!artifactFile.exists()) {
97 throw new MojoFailureException("Artifact File does not exist! (file=" + path + ")");
98 }
99
100 BeanstalkerS3Client client = new BeanstalkerS3Client(getAWSCredentials(), getClientConfiguration(), getRegion());
101
102 client.setMultipartUpload(multipartUpload);
103 client.setSilentUpload(silentUpload);
104
105 if (StringUtils.isBlank(s3Bucket)) {
106 getLog().info("S3 Bucket not defined.");
107 s3Bucket = getService().createStorageLocation().getS3Bucket();
108
109 getLog().info("Using defaults, like: " + s3Bucket);
110
111 project.getProperties().put("beanstalk.s3Bucket", s3Bucket);
112 }
113
114 getLog().info("Target Path: s3://" + s3Bucket + "/" + s3Key);
115 getLog().info("Uploading artifact file: " + path);
116
117 PutObjectResult result = client.putObject(new PutObjectRequest(s3Bucket, s3Key, artifactFile));
118
119 getLog().info("Artifact Uploaded");
120
121 project.getProperties().put("beanstalk.s3Key", s3Key);
122
123 project.getProperties().put("beanstalk.lastUploadedS3Object", format("s3://%s/%s", s3Bucket, s3Key));
124
125 return result;
126 }
127 }