001package org.andromda.maven.plugin.andromdapp; 002 003import java.io.File; 004import java.util.ArrayList; 005import java.util.List; 006import org.apache.commons.io.FileUtils; 007import org.apache.maven.model.Build; 008import org.apache.maven.plugin.MojoExecutionException; 009import org.apache.maven.plugin.MojoFailureException; 010import org.codehaus.plexus.util.DirectoryScanner; 011 012/** 013 * Provides the deployment of applications to a given directory. 014 * 015 * @goal deploy 016 * @phase package 017 * @author Chad Brandon 018 */ 019public class DeployMojo 020 extends AppManagementMojo 021{ 022 /** 023 * Indicates whether or not this plugin should perform the deploy. 024 * 025 * @parameter expression="${deploy}" 026 */ 027 private String deploy; 028 029 /** 030 * The string indicating whether or not the deploy should be exploded or not. 031 */ 032 private static final String EXPLODED = "exploded"; 033 034 /** 035 * Any additional files to include in the deploy liked datasource files etc 036 * (the files must reside in the project build directory). 037 * By default nothing besides the file artifact is deployed. 038 * 039 * @parameter 040 */ 041 private String[] includes = new String[0]; 042 043 /** 044 * Any files to exclude in the deploy. 045 * 046 * @parameter 047 */ 048 private String[] excludes = new String[0]; 049 050 /** 051 * @see org.apache.maven.plugin.AbstractMojo#execute() 052 */ 053 public void execute() 054 throws MojoExecutionException, MojoFailureException 055 { 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 && this.deploy.equals(Boolean.TRUE.toString()) || artifactFile == null) 061 { 062 final Build build = this.project.getBuild(); 063 if (EXPLODED.equalsIgnoreCase(this.deploy)) 064 { 065 artifactFile = new File( 066 build.getDirectory(), 067 build.getFinalName()); 068 } 069 else if (artifactFile == null) 070 { 071 artifactFile = new File( 072 build.getDirectory(), 073 build.getFinalName() + '.' + this.getPackaging()); 074 } 075 if (artifactFile.exists()) 076 { 077 final File deployFile = this.getDeployFile(); 078 if (this.deployLocation.exists() && this.deployLocation.isDirectory()) 079 { 080 try 081 { 082 if (EXPLODED.equalsIgnoreCase(this.deploy)) 083 { 084 this.getLog().info("Deploying exploded " + artifactFile + " to " + deployFile); 085 FileUtils.copyDirectory( 086 artifactFile, 087 deployFile); 088 } 089 else 090 { 091 // - if the deploy file is a directory, then attempt to delete it first before we 092 // attempting deploying 093 if (deployFile.exists() && deployFile.isDirectory()) 094 { 095 this.getLog().info("Removing exploded artifact: " + deployFile); 096 FileUtils.deleteDirectory(deployFile); 097 } 098 final List<File> deployFiles = this.getAdditionalFiles(); 099 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}