Index: maven-generator-plugin/src/java/org/codelutin/generator/plugin/CopyVersionFiles.java diff -u /dev/null maven-generator-plugin/src/java/org/codelutin/generator/plugin/CopyVersionFiles.java:1.1 --- /dev/null Fri Apr 20 08:40:40 2007 +++ maven-generator-plugin/src/java/org/codelutin/generator/plugin/CopyVersionFiles.java Fri Apr 20 08:40:35 2007 @@ -0,0 +1,254 @@ +/* *##% + * Copyright (C) 2006 Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.codelutin.generator.plugin; + +import java.io.File; +import java.io.FileFilter; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; +import org.apache.tools.ant.BuildLogger; +import org.apache.tools.ant.NoBannerLogger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.Copy; +import org.apache.tools.ant.types.FileSet; +import org.dom4j.DocumentException; +import org.dom4j.Node; +import org.dom4j.io.SAXReader; +import org.dom4j.Document; + +/* + * CopyVersionFiles + * + * @author chatellier + * + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2007/04/20 08:40:35 $ By : $Author: chatellier $ + */ +/** + * CopyVersionFiles.java + * + * @goal copyVersionFiles + */ +public class CopyVersionFiles extends AbstractMojo implements FileFilter { + + /** + * Fichiers objectModel a lire pour determiner la version + * + * @parameter + * @required + */ + protected String includes; + + /** + * Répertoire source + * + * @parameter alias="srcGenDest" + * @required + */ + protected File srcDirGen; + + /** + * Le dossier de destination des mappings + * + * @parameter + * @required + */ + protected String copyVersionDir; + + /** + * Les mappings a sauvegarder + * + * @parameter + * @required + */ + protected String copyVersionFiles; + + /** + * Répertoire cible + * + * @parameter + * @required + */ + protected File destDirGen; + + /** + * Ecrase les fichiers deja presents ? + * + * @parameter default-value="false" + */ + protected boolean copyOverwrite = false; + + /** + * Version trouvee dans les fichiers objectModel. + * + * Type string, parce que elle peut avoir la forme "1.3.2" par exemple + */ + protected String versionFound = null; + + /** + * Maven logger + */ + protected Log logger; + + /** + * Dossier incluant le nom de la version + */ + protected File fVersionDir; + + /* + * (non-Javadoc) + * + * @see org.apache.maven.plugin.Mojo#execute() + */ + public void execute() throws MojoExecutionException, MojoFailureException { + + // get log + logger = getLog(); + + // find version + findVersion(); + + if (versionFound == null || !versionFound.matches("[0-9]+(\\.[0-9]+)*")) { + versionFound = "0"; + logger.info("No version found in model files, setting version to '" + + versionFound + "'"); + } else { + logger.info("Version '" + versionFound + + "' found in model description"); + } + + String destDir = copyVersionDir + File.separator + versionFound; + fVersionDir = new File(destDir); + + boolean doCopy = true; + + if (!copyOverwrite) { + doCopy = !checkExistence(); + } + if (doCopy) + copyAction(); + } + + /** + * Check if previous saved files are already present + */ + protected boolean checkExistence() { + + boolean exist = false; + + if (fVersionDir.exists() && fVersionDir.listFiles().length > 0) { + logger.warn("[COPY] Warning saved files for version '" + + versionFound + "' already exists"); + logger.warn("[COPY] Copy won't be done unless copyOverwrite " + + "parameter is set to 'true' or version is updated"); + + exist = true; + } + + return exist; + } + + /** + * Copy hibernate files. + * + * Using ant task + */ + protected void copyAction() { + // creation du repertoire + fVersionDir.mkdirs(); + + /* Création d'un projet ant */ + Project project = createProject(); + + /* Création de la tâche ant Copy */ + Copy copy = new Copy(); + copy.setProject(project); + copy.setTaskName("Copy ressources"); + + /* Configuration */ + copy.setTodir(fVersionDir); + copy.setOverwrite(true); + + FileSet fileSet = new FileSet(); + fileSet.setDir(destDirGen); + fileSet.setIncludes(copyVersionFiles); + copy.addFileset(fileSet); + + /* Execution */ + copy.execute(); + } + + /** + * Find version in object model files + */ + protected void findVersion() { + + File srcModelDir = srcDirGen; + + File[] modelFiles = srcModelDir.listFiles(this); + + for (int i = 0; i < modelFiles.length; ++i) { + SAXReader saxR = new SAXReader(); + Document document; + try { + document = saxR.read(modelFiles[i]); + Node node; + node = document.selectSingleNode("/objectModel/@version"); + if (node != null) { + versionFound = node.getStringValue(); + } + } catch (DocumentException e) { + logger.error("Can't read document", e); + } + } + } + + protected Project createProject() { + /* Création d'un projet ant */ + Project project = new Project(); + + BuildLogger logger = new NoBannerLogger(); + logger.setMessageOutputLevel(org.apache.tools.ant.Project.MSG_INFO); + logger.setOutputPrintStream(System.out); + logger.setErrorPrintStream(System.err); + + project.init(); + project.getBaseDir(); + project.addBuildListener(logger); + return project; + } + + /* + * (non-Javadoc) + * + * @see java.io.FileFilter#accept(java.io.File) + */ + public boolean accept(File arg0) { + String fullPath = arg0.getAbsolutePath(); + // regex + String regexInclude = includes.replaceAll("\\.", "\\.").replaceAll( + "([^\\*])\\*([^\\*])", "$1[^/]*$2").replaceAll("\\*\\*", ".*") + + "$"; + return fullPath.matches(regexInclude); + } +}