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
14
15
16
17
18
19 public class DeployMojo
20 extends AppManagementMojo
21 {
22
23
24
25
26
27 private String deploy;
28
29
30
31
32 private static final String EXPLODED = "exploded";
33
34
35
36
37
38
39
40
41 private String[] includes = new String[0];
42
43
44
45
46
47
48 private String[] excludes = new String[0];
49
50
51
52
53 public void execute()
54 throws MojoExecutionException, MojoFailureException
55 {
56 File artifactFile = this.project.getArtifact().getFile();
57
58
59
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
92
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
131
132
133
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 }