001: package org.andromda.maven.plugin.andromdapp;
002:
003: import java.io.File;
004:
005: import java.util.ArrayList;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.apache.commons.io.FileUtils;
010: import org.apache.maven.model.Build;
011: import org.apache.maven.plugin.MojoExecutionException;
012: import org.apache.maven.plugin.MojoFailureException;
013: import org.codehaus.plexus.util.DirectoryScanner;
014:
015: /**
016: * Provides the deployment of applications to a given directory.
017: *
018: * @goal deploy
019: * @phase install
020: * @author Chad Brandon
021: */
022: public class DeployMojo extends AppManagementMojo {
023: /**
024: * Indicates whether or not this plugin should perform the deploy.
025: *
026: * @parameter expression="${deploy}"
027: */
028: private String deploy;
029:
030: /**
031: * The string indicating whether or not the deploy should be exploded or not.
032: */
033: private static final String EXPLODED = "exploded";
034:
035: /**
036: * Any additional files to include in the deploy liked datasource files etc
037: * (the files must reside in the project build directory).
038: * By default nothing besides the file artifact is deployed.
039: *
040: * @parameter
041: */
042: private String[] includes = new String[0];
043:
044: /**
045: * Any files to exclude in the deploy.
046: *
047: * @parameter
048: */
049: private String[] excludes = new String[0];
050:
051: /**
052: * @see org.apache.maven.plugin.AbstractMojo#execute()
053: */
054: public void execute() throws MojoExecutionException,
055: MojoFailureException {
056: File artifactFile = this .project.getArtifact().getFile();
057:
058: // - if we're deploying within a phase then deploy has to be set, otherwise
059: // its not needed (we know we're not deploying in a phase when the artifactFile is null).
060: if (this .deploy != null
061: && !this .deploy.equals(Boolean.FALSE.toString())
062: || artifactFile == null) {
063: final Build build = this .project.getBuild();
064: if (EXPLODED.equalsIgnoreCase(this .deploy)) {
065: artifactFile = new File(build.getDirectory(), build
066: .getFinalName());
067: } else if (artifactFile == null) {
068: artifactFile = new File(build.getDirectory(), build
069: .getFinalName()
070: + '.' + this .getPackaging());
071: }
072: if (artifactFile.exists()) {
073: final File deployFile = this .getDeployFile();
074: final File deployDirectory = new File(
075: this .deployLocation);
076: if (deployDirectory.exists()
077: && deployDirectory.isDirectory()) {
078: try {
079: if (EXPLODED.equalsIgnoreCase(this .deploy)) {
080: this .getLog().info(
081: "Deploying exploded "
082: + artifactFile + " to "
083: + deployFile);
084: FileUtils.copyDirectory(artifactFile,
085: deployFile);
086: } else {
087: // - if the deploy file is a directory, then attempt to delete it first before we
088: // attempting deploying
089: if (deployFile.exists()
090: && deployFile.isDirectory()) {
091: this .getLog().info(
092: "Removing exploded artifact: "
093: + deployFile);
094: FileUtils.deleteDirectory(deployFile);
095: }
096: final List deployFiles = this
097: .getAdditionalFiles();
098: deployFiles.add(0, artifactFile);
099: for (final Iterator iterator = deployFiles
100: .iterator(); iterator.hasNext();) {
101: final File file = (File) iterator
102: .next();
103: this .getLog().info(
104: "Deploying file " + file
105: + " to "
106: + deployDirectory);
107: FileUtils.copyFileToDirectory(file,
108: deployDirectory);
109: }
110: }
111: } catch (final Throwable throwable) {
112: throw new MojoExecutionException(
113: "An error occurred while attempting to deploy artifact",
114: throwable);
115: }
116: } else {
117: this
118: .getLog()
119: .error(
120: "Deploy did not occur because the specified deployLocation '"
121: + deployLocation
122: + "' does not exist, or is not a directory");
123: }
124: } else {
125: this .getLog().warn(
126: "Deploy did not occur because file '"
127: + artifactFile + "' does not exist");
128: }
129: }
130: }
131:
132: /**
133: * Retrieves any additional files to include in the deploy.
134: *
135: * @return all poms found.
136: * @throws MojoExecutionException
137: */
138: private List getAdditionalFiles() {
139: final DirectoryScanner scanner = new DirectoryScanner();
140: scanner.setBasedir(this .project.getBuild().getDirectory());
141: scanner.setIncludes(this .includes);
142: scanner.setExcludes(this .excludes);
143: scanner.scan();
144:
145: final List files = new ArrayList();
146: for (int ctr = 0; ctr < scanner.getIncludedFiles().length; ctr++) {
147: final File file = new File(this.project.getBuild()
148: .getDirectory(), scanner.getIncludedFiles()[ctr]);
149: if (file.exists()) {
150: files.add(file);
151: }
152: }
153:
154: return files;
155: }
156: }
|