View Javadoc
1   /*
2    * Copyright (c) 2016 ingenieux Labs
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    * http://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 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   * Uploads a packed war file to Amazon S3 for further Deployment.
37   *
38   * @since 0.1.0
39   */
40  @Mojo(name = "upload-source-bundle")
41  public class UploadSourceBundleMojo extends AbstractBeanstalkMojo {
42  
43    /**
44     * S3 Bucket
45     */
46    @Parameter(property = "beanstalk.s3Bucket")
47    String s3Bucket;
48  
49    /**
50     * S3 Key
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     * <p> Should we do a multipart upload? Defaults to true </p> <p> Disable when you want to be
61     * charged slightly less :) </p>
62     */
63    @Parameter(property = "beanstalk.multipartUpload", defaultValue = "true")
64    boolean multipartUpload = false;
65  
66    /**
67     * Artifact to Deploy
68     */
69    @Parameter(property = "beanstalk.artifactFile", defaultValue = "${project.build.directory}/${project.build.finalName}.${project.packaging}", required = true)
70    File artifactFile;
71  
72    /**
73     * Silent Upload?
74     */
75    @Parameter(property = "beanstalk.silentUpload", defaultValue = "false")
76    boolean silentUpload = false;
77  
78    /**
79     * Version Label to use. Defaults to Project Version
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 }