View Javadoc
1   package org.andromda.maven.plugin.andromdapp;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.List;
6   import org.apache.commons.io.FileUtils;
7   import org.apache.maven.model.Build;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugin.MojoFailureException;
10  import org.codehaus.plexus.util.DirectoryScanner;
11  
12  /**
13   * Provides the deployment of applications to a given directory.
14   *
15   * @goal deploy
16   * @phase package
17   * @author Chad Brandon
18   */
19  public class DeployMojo
20      extends AppManagementMojo
21  {
22      /**
23       * Indicates whether or not this plugin should perform the deploy.
24       *
25       * @parameter expression="${deploy}"
26       */
27      private String deploy;
28  
29      /**
30       * The string indicating whether or not the deploy should be exploded or not.
31       */
32      private static final String EXPLODED = "exploded";
33  
34      /**
35       * Any additional files to include in the deploy liked datasource files etc
36       * (the files must reside in the project build directory).
37       * By default nothing besides the file artifact is deployed.
38       *
39       * @parameter
40       */
41      private String[] includes = new String[0];
42  
43      /**
44       * Any files to exclude in the deploy.
45       *
46       * @parameter
47       */
48      private String[] excludes = new String[0];
49  
50      /**
51       * @see org.apache.maven.plugin.AbstractMojo#execute()
52       */
53      public void execute()
54          throws MojoExecutionException, MojoFailureException
55      {
56          File artifactFile = this.project.getArtifact().getFile();
57  
58          // - if we're deploying within a phase then deploy has to be set, otherwise
59          //   its not needed (we know we're not deploying in a phase when the artifactFile is null).
60          if (this.deploy != null && this.deploy.equals(Boolean.TRUE.toString()) || artifactFile == null)
61          {
62              final Build build = this.project.getBuild();
63              if (EXPLODED.equalsIgnoreCase(this.deploy))
64              {
65                  artifactFile = new File(
66                          build.getDirectory(),
67                          build.getFinalName());
68              }
69              else if (artifactFile == null)
70              {
71                  artifactFile = new File(
72                          build.getDirectory(),
73                          build.getFinalName() + '.' + this.getPackaging());
74              }
75              if (artifactFile.exists())
76              {
77                  final File deployFile = this.getDeployFile();
78                  if (this.deployLocation.exists() && this.deployLocation.isDirectory())
79                  {
80                      try
81                      {
82                          if (EXPLODED.equalsIgnoreCase(this.deploy))
83                          {
84                              this.getLog().info("Deploying exploded " + artifactFile + " to " + deployFile);
85                              FileUtils.copyDirectory(
86                                  artifactFile,
87                                  deployFile);
88                          }
89                          else
90                          {
91                              // - if the deploy file is a directory, then attempt to delete it first before we
92                              //   attempting deploying
93                              if (deployFile.exists() && deployFile.isDirectory())
94                              {
95                                  this.getLog().info("Removing exploded artifact: " + deployFile);
96                                  FileUtils.deleteDirectory(deployFile);
97                              }
98                              final List<File> deployFiles = this.getAdditionalFiles();
99                              deployFiles.add(0, artifactFile);
100                             for (final File file : deployFiles)
101                             {
102                                 this.getLog().info("Deploying file " + file + " to " + this.deployLocation);
103                                 FileUtils.copyFileToDirectory(
104                                     file,
105                                         this.deployLocation);
106                             }
107                         }
108                     }
109                     catch (final Throwable throwable)
110                     {
111                         throw new MojoExecutionException("An error occurred while attempting to deploy artifact",
112                             throwable);
113                     }
114                 }
115                 else
116                 {
117                     this.getLog().error(
118                         "Deploy did not occur because the specified deployLocation '" + deployLocation +
119                         "' does not exist, or is not a directory");
120                 }
121             }
122             else
123             {
124                 this.getLog().warn("Deploy did not occur because file '" + artifactFile + "' does not exist");
125             }
126         }
127     }
128 
129     /**
130      * Retrieves any additional files to include in the deploy.
131      *
132      * @return all poms found.
133      * @throws MojoExecutionException
134      */
135     private List<File> getAdditionalFiles()
136     {
137         final DirectoryScanner scanner = new DirectoryScanner();
138         scanner.setBasedir(this.project.getBuild().getDirectory());
139         scanner.setIncludes(this.includes);
140         scanner.setExcludes(this.excludes);
141         scanner.scan();
142 
143         final List<File> files = new ArrayList<File>();
144         for (int ctr = 0; ctr < scanner.getIncludedFiles().length; ctr++)
145         {
146             final File file = new File(
147                     this.project.getBuild().getDirectory(),
148                     scanner.getIncludedFiles()[ctr]);
149             if (file.exists())
150             {
151                 files.add(file);
152             }
153         }
154 
155         return files;
156     }
157 }