From 04fdaf931804b8380867db613e8d247149a35809 Mon Sep 17 00:00:00 2001 From: Uwe Barthel Date: Sat, 24 May 2014 01:25:43 +0200 Subject: [PATCH] #16 skip execution if source directory is missing * whitespace formatting reverted --- .../github/plantuml/maven/PlantUMLMojo.java | 351 +++++++++--------- 1 file changed, 176 insertions(+), 175 deletions(-) diff --git a/src/main/java/com/github/plantuml/maven/PlantUMLMojo.java b/src/main/java/com/github/plantuml/maven/PlantUMLMojo.java index f541339..24fb14f 100644 --- a/src/main/java/com/github/plantuml/maven/PlantUMLMojo.java +++ b/src/main/java/com/github/plantuml/maven/PlantUMLMojo.java @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -19,12 +19,14 @@ import java.io.File; import java.util.Iterator; import java.util.List; + import net.sourceforge.plantuml.FileFormat; import net.sourceforge.plantuml.GeneratedImage; import net.sourceforge.plantuml.Option; import net.sourceforge.plantuml.OptionFlags; import net.sourceforge.plantuml.SourceFileReader; import net.sourceforge.plantuml.preproc.Defines; + import org.apache.maven.model.FileSet; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -36,188 +38,187 @@ */ public final class PlantUMLMojo extends AbstractMojo { - private final Option option = new Option(); - - /** - * Fileset to search plantuml diagrams in. - * @parameter property="plantuml.sourceFiles" - * @required - * @since 7232 - */ - private FileSet sourceFiles; - - /** - * Directory where generated images are generated. - * @parameter property="plantuml.outputDirectory" default-value="${basedir}/target/plantuml" - * @required - */ - private File outputDirectory; - - /** - * Whether or not to generate images in same directory as the source file. - * This is useful for using PlantUML diagrams in Javadoc, - * as described here: - * http://plantuml.sourceforge.net/javadoc.html. + private final Option option = new Option(); + + /** + * Fileset to search plantuml diagrams in. + * @parameter property="plantuml.sourceFiles" + * @required + * @since 7232 + */ + private FileSet sourceFiles; + + /** + * Directory where generated images are generated. + * @parameter property="plantuml.outputDirectory" default-value="${basedir}/target/plantuml" + * @required + */ + private File outputDirectory; + + /** + * Whether or not to generate images in same directory as the source file. + * This is useful for using PlantUML diagrams in Javadoc, + * as described here: + * http://plantuml.sourceforge.net/javadoc.html. * - * If this is set to true then outputDirectory is ignored. - * @parameter property="plantuml.outputInSourceDirectory" default-value="false" - */ - private boolean outputInSourceDirectory; - - /** - * Charset used during generation. - * @parameter property="plantuml.charset" - */ - private String charset; - - /** - * External configuration file location. - * @parameter property="plantuml.config" - */ - private String config; - - /** - * Wether or not to keep tmp files after generation. - * @parameter property="plantuml.keepTmpFiles" default-value="false" - */ - private boolean keepTmpFiles; - - /** + * If this is set to true then outputDirectory is ignored. + * @parameter property="plantuml.outputInSourceDirectory" default-value="false" + */ + private boolean outputInSourceDirectory; + + /** + * Charset used during generation. + * @parameter property="plantuml.charset" + */ + private String charset; + + /** + * External configuration file location. + * @parameter property="plantuml.config" + */ + private String config; + + /** + * Wether or not to keep tmp files after generation. + * @parameter property="plantuml.keepTmpFiles" default-value="false" + */ + private boolean keepTmpFiles; + + /** * Specify output format. Supported values: xmi, xmi:argo, xmi:start, eps, pdf, eps:txt, svg, png, dot, txt and utxt. - * @parameter property="plantuml.format" - */ - private String format; - - /** - * Fully qualified path to Graphviz home directory. - * @parameter property="plantuml.graphvizDot" - */ - private String graphvizDot; - - /** - * Wether or not to output details during generation. - * @parameter property="plantuml.verbose" default-value="false" - */ - private boolean verbose; - - protected final void setFormat(final String format) { - if ("xmi".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.XMI_STANDARD); - } else if ("xmi:argo".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.XMI_ARGO); - } else if ("xmi:start".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.XMI_STAR); - } else if ("eps".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.EPS); - } else if ("eps:txt".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.EPS_TEXT); - } else if ("svg".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.SVG); - } else if ("txt".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.ATXT); - } else if ("utxt".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.UTXT); - } else if ("png".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.PNG); - } else if ("pdf".equalsIgnoreCase(format)) { - this.option.setFileFormat(FileFormat.PDF); - } else { - throw new IllegalArgumentException("Unrecognized format <" + format + ">"); - } + * @parameter property="plantuml.format" + */ + private String format; + + /** + * Fully qualified path to Graphviz home directory. + * @parameter property="plantuml.graphvizDot" + */ + private String graphvizDot; + + /** + * Wether or not to output details during generation. + * @parameter property="plantuml.verbose" default-value="false" + */ + private boolean verbose; + + protected final void setFormat(final String format) { + if ("xmi".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.XMI_STANDARD); + } else if ("xmi:argo".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.XMI_ARGO); + } else if ("xmi:start".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.XMI_STAR); + } else if ("eps".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.EPS); + } else if ("eps:txt".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.EPS_TEXT); + } else if ("svg".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.SVG); + } else if ("txt".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.ATXT); + } else if ("utxt".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.UTXT); + } else if ("png".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.PNG); + } else if ("pdf".equalsIgnoreCase(format)) { + this.option.setFileFormat(FileFormat.PDF); + } else { + throw new IllegalArgumentException("Unrecognized format <"+format+">"); + } + } + + @Override + public void execute() throws MojoExecutionException { + // early exit if sourceFiles directory is not available + final String invalidSourceFilesDirectoryWarnMsg = this.sourceFiles.getDirectory() + " is not a valid path"; + if( null == this.sourceFiles.getDirectory() || this.sourceFiles.getDirectory().isEmpty()) { + getLog().warn(invalidSourceFilesDirectoryWarnMsg); + return; + } + File baseDir = null; + try { + baseDir = new File(this.sourceFiles.getDirectory()); + } catch (Exception e) { + getLog().debug(invalidSourceFilesDirectoryWarnMsg, e); + } + if( null == baseDir || !baseDir.exists() || !baseDir.isDirectory()) { + getLog().warn(invalidSourceFilesDirectoryWarnMsg); + return; + } + if (!this.outputInSourceDirectory) { + if (!this.outputDirectory.exists()) { + // If output directoy does not exist yet create it. + this.outputDirectory.mkdirs(); + } + if (!this.outputDirectory.isDirectory()) { + throw new IllegalArgumentException("<" + this.outputDirectory + "> is not a valid directory."); + } } - @Override - public void execute() throws MojoExecutionException { - // early exit if sourceFiles directory is not available - final String invalidSourceFilesDirectoryWarnMsg = this.sourceFiles.getDirectory() + " is not a valid path"; - if( null == this.sourceFiles.getDirectory() || this.sourceFiles.getDirectory().isEmpty()) { - getLog().warn(invalidSourceFilesDirectoryWarnMsg); - return; - } - File baseDir = null; - try { - baseDir = new File(this.sourceFiles.getDirectory()); - } catch (Exception e) { - getLog().debug(invalidSourceFilesDirectoryWarnMsg, e); - } - if( null == baseDir || !baseDir.exists() || !baseDir.isDirectory()) { - getLog().warn(invalidSourceFilesDirectoryWarnMsg); - return; - } - - if (!this.outputInSourceDirectory) { - if (!this.outputDirectory.exists()) { - // If output directoy does not exist yet create it. - this.outputDirectory.mkdirs(); - } - if (!this.outputDirectory.isDirectory()) { - throw new IllegalArgumentException("<" + this.outputDirectory + "> is not a valid directory."); - } + try { + if (!this.outputInSourceDirectory) { + this.option.setOutputDir(this.outputDirectory); + } + if (this.charset != null) { + this.option.setCharset(this.charset); + } + if (this.config != null) { + this.option.initConfig(this.config); + } + if (this.keepTmpFiles) { + OptionFlags.getInstance().setKeepTmpFiles(this.keepTmpFiles); + } + if (this.graphvizDot != null) { + OptionFlags.getInstance().setDotExecutable(this.graphvizDot); + } + if (this.format != null) { + setFormat(this.format); + } + if (this.verbose) { + OptionFlags.getInstance().setVerbose(true); + } + + final List files = FileUtils.getFiles( + baseDir, + getCommaSeparatedList(this.sourceFiles.getIncludes()), + getCommaSeparatedList(this.sourceFiles.getExcludes()) + ); + for(final File file : files) { + getLog().info("Processing file <"+file+">"); + + if (this.outputInSourceDirectory) { + this.option.setOutputDir(file.getParentFile()); + } else { + this.option.setOutputDir(outputDirectory.toPath().resolve( + baseDir.toPath().relativize(file.toPath().getParent())).toFile()); } - try { - if (!this.outputInSourceDirectory) { - this.option.setOutputDir(this.outputDirectory); - } - if (this.charset != null) { - this.option.setCharset(this.charset); - } - if (this.config != null) { - this.option.initConfig(this.config); - } - if (this.keepTmpFiles) { - OptionFlags.getInstance().setKeepTmpFiles(this.keepTmpFiles); - } - if (this.graphvizDot != null) { - OptionFlags.getInstance().setDotExecutable(this.graphvizDot); - } - if (this.format != null) { - setFormat(this.format); - } - if (this.verbose) { - OptionFlags.getInstance().setVerbose(true); - } - - final List files = FileUtils.getFiles( - baseDir, - getCommaSeparatedList(this.sourceFiles.getIncludes()), - getCommaSeparatedList(this.sourceFiles.getExcludes()) - ); - for (final File file : files) { - getLog().info("Processing file <" + file + ">"); - - if (this.outputInSourceDirectory) { - this.option.setOutputDir(file.getParentFile()); - } else { - this.option.setOutputDir(outputDirectory.toPath().resolve( - baseDir.toPath().relativize(file.toPath().getParent())).toFile()); - } - - final SourceFileReader sourceFileReader = - new SourceFileReader( - new Defines(), file, this.option.getOutputDir(), - this.option.getConfig(), this.option.getCharset(), - this.option.getFileFormatOption()); - for (final GeneratedImage image : sourceFileReader.getGeneratedImages()) { - getLog().debug(image + " " + image.getDescription()); - } - } - } catch (Exception e) { - throw new MojoExecutionException("Exception during plantuml process", e); + final SourceFileReader sourceFileReader = + new SourceFileReader( + new Defines(), file, this.option.getOutputDir(), + this.option.getConfig(), this.option.getCharset(), + this.option.getFileFormatOption()); + for (final GeneratedImage image : sourceFileReader.getGeneratedImages()) { + getLog().debug(image + " " + image.getDescription()); } + } + } catch (Exception e) { + throw new MojoExecutionException("Exception during plantuml process", e); } - - protected String getCommaSeparatedList(final List list) { - final StringBuilder builder = new StringBuilder(); - final Iterator it = list.iterator(); - while (it.hasNext()) { - final Object object = it.next(); - builder.append(object.toString()); - if (it.hasNext()) { - builder.append(","); - } - } - return builder.toString(); + } + + protected String getCommaSeparatedList(final List list) { + final StringBuilder builder = new StringBuilder(); + final Iterator it = list.iterator(); + while(it.hasNext()) { + final Object object = it.next(); + builder.append(object.toString()); + if (it.hasNext()) { + builder.append(","); + } } + return builder.toString(); + } }