001package org.andromda.maven.plugin.modelarchiver; 002 003import org.apache.commons.io.FileUtils; 004import org.apache.commons.lang.StringUtils; 005import org.apache.maven.artifact.Artifact; 006import org.apache.maven.plugin.AbstractMojo; 007import org.apache.maven.project.MavenProject; 008import org.apache.maven.project.MavenProjectHelper; 009import org.codehaus.plexus.archiver.manager.ArchiverManager; 010 011import java.io.File; 012import java.io.IOException; 013import java.util.Iterator; 014 015/** 016 * Base class for all ArchiverMojos 017 * 018 * @author Plushnikov Michail 019 * @version $Id: $ 020 * @since 3.4 021 */ 022public abstract class BaseArchiveMojo extends AbstractMojo 023{ 024 /** 025 * Single directory that contains the model 026 * 027 * @parameter expression="${basedir}/src/main/uml" 028 * @required 029 */ 030 protected File modelSourceDirectory; 031 /** 032 * Directory that resources are copied to during the build. 033 * 034 * @parameter expression="${project.build.directory}" 035 * @required 036 */ 037 protected File workDirectory; 038 /** 039 * The directory for the generated emx. 040 * 041 * @parameter expression="${project.build.outputDirectory}" 042 * @required 043 */ 044 protected File outputDirectory; 045 /** 046 * The name of the emx file to generate. 047 * 048 * @parameter alias="modelName" expression="${project.build.finalName}" 049 * @required 050 * @readonly 051 */ 052 protected String finalName; 053 /** 054 * The maven project. 055 * 056 * @parameter expression="${project}" 057 * @required 058 * @readonly 059 * @description "the maven project to use" 060 */ 061 protected MavenProject project; 062 063 /** 064 * To look up Archiver/UnArchiver implementations 065 * 066 * @component role="org.codehaus.plexus.archiver.manager.ArchiverManager" 067 * @required 068 */ 069 protected ArchiverManager archiverManager; 070 071 /** 072 * The maven project's helper. 073 * 074 * @component role="org.apache.maven.project.MavenProjectHelper" 075 * @required 076 * @readonly 077 */ 078 protected MavenProjectHelper projectHelper; 079 080 /** 081 * Whether or not to do replacement of embedded model HREF reference extensions. 082 * 083 * @parameter expression=false 084 * @required 085 */ 086 protected boolean replaceExtensions; 087 088 /** 089 * Deletes all files with given extension in the given directory 090 * 091 * @param pPath path to directory 092 * @param pExtension extension of files 093 */ 094 protected void deleteFiles(String pPath, String pExtension) 095 { 096 Iterator<File> lFileIter = FileUtils.iterateFiles(new File(pPath), new String[]{pExtension}, false); 097 while (lFileIter.hasNext()) 098 { 099 FileUtils.deleteQuietly(lFileIter.next()); 100 } 101 } 102 103 /** 104 * Escapes the pattern so that the reserved regular expression 105 * characters are used literally. 106 * 107 * @param pattern the pattern to replace. 108 * @return the resulting pattern. 109 */ 110 protected String escapePattern(String pattern) 111 { 112 pattern = StringUtils.replace( 113 pattern, 114 ".", 115 "\\."); 116 pattern = StringUtils.replace( 117 pattern, 118 "-", 119 "\\-"); 120 return pattern; 121 } 122 123 /** 124 * Replace all extensions in the file 125 * 126 * @param pReplacementExtensions Extensions to replace 127 * @param pFile File where all Extensions should be replaced 128 * @throws java.io.IOException Exception on IO-Error 129 */ 130 protected void replaceExtensions(String pReplacementExtensions, File pFile) throws IOException 131 { 132 String[] replacementExtensions = pReplacementExtensions != null ? pReplacementExtensions.split(",\\s*") : new String[0]; 133 134 final String version = escapePattern(this.project.getVersion()); 135 String contents = FileUtils.readFileToString(pFile); 136 for (String replacementExtension : replacementExtensions) 137 { 138 final String extension = escapePattern(replacementExtension); 139 // add ' to extension, to match only href elements (example: href='abcdefg.xml') 140 // and not abc.xml.efg substrings 141 final String extensionPattern = "((\\-" + version + ")?)" + extension + "(['\"|])"; 142 final String newExtension = "\\-" + version + extension + "$3"; 143 contents = contents.replaceAll( 144 extensionPattern, 145 newExtension); 146 // Fix replacement error for standard UML profiles which follow the _Profile. naming convention. 147 contents = 148 contents.replaceAll( 149 "_Profile\\-" + version, 150 "_Profile"); 151 } 152 FileUtils.writeStringToFile(pFile, contents); 153 } 154 155 /** 156 * Sets File for current Artifact 157 * @param newFile File to set for current Artifact 158 */ 159 protected void setArtifactFile(File newFile) 160 { 161 final Artifact artifact = this.project.getArtifact(); 162 // - set the artifact file to the correct file 163 artifact.setFile(newFile); 164 getLog().debug("File artifact set " + newFile.getAbsolutePath() + " packaging " + project.getPackaging()); 165 } 166}