001 /*
002 * Copyright 2013 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.beans.maven;
017
018 import java.util.ArrayList;
019 import java.util.List;
020
021 import org.apache.maven.plugin.AbstractMojo;
022 import org.apache.maven.plugin.MojoExecutionException;
023 import org.apache.maven.plugin.MojoFailureException;
024 import org.joda.beans.gen.BeanCodeGen;
025
026 /**
027 * Maven plugin for validating that the generated Joda-Beans are up to date.
028 *
029 * @goal validate
030 * @phase validate
031 *
032 * @author Stephen Colebourne
033 */
034 public class JodaBeansValidateMojo extends AbstractMojo {
035
036 /**
037 * @parameter alias="indent" expression="${joda.beans.indent}"
038 */
039 private String indent;
040 /**
041 * @parameter alias="prefix" expression="${joda.beans.prefix}"
042 */
043 private String prefix;
044 /**
045 * @parameter alias="verbose" expression="${joda.beans.verbose}"
046 */
047 private Integer verbose;
048 /**
049 * @parameter alias="stopOnError" expression="${joda.beans.stopOnError}"
050 */
051 private boolean stopOnError = true;
052 /**
053 * @parameter expression="${project.build.sourceDirectory}"
054 * @required
055 * @readonly
056 */
057 private String sourceDir;
058
059 /**
060 * Executes the Joda-Beans generator, validating that there are no changes.
061 */
062 public void execute() throws MojoExecutionException, MojoFailureException {
063 if (sourceDir == null) {
064 throw new MojoExecutionException("Source directory must not be null");
065 }
066
067 // build args
068 List<String> argsList = new ArrayList<String>();
069 argsList.add("-R");
070 if (indent != null) {
071 argsList.add("-indent=" + indent);
072 }
073 if (prefix != null) {
074 argsList.add("-prefix=" + prefix);
075 }
076 if (verbose != null) {
077 argsList.add("-verbose=" + verbose);
078 }
079 argsList.add("-nowrite");
080 argsList.add(sourceDir);
081
082 // run generator without writing
083 getLog().info("Joda-Bean validator started, directory: " + sourceDir);
084 BeanCodeGen gen = null;
085 try {
086 String[] args = argsList.toArray(new String[argsList.size()]);
087 gen = BeanCodeGen.createFromArgs(args);
088 } catch (RuntimeException ex) {
089 throw new MojoFailureException("Invalid Joda-Beans Mojo configuration: " + ex.getMessage(), ex);
090 }
091 int changes = 0;
092 try {
093 changes = gen.process();
094 } catch (Exception ex) {
095 throw new MojoFailureException("Error while running Joda-Beans generator: " + ex.getMessage(), ex);
096 }
097 if (changes > 0) {
098 if (stopOnError) {
099 throw new MojoFailureException("Some Joda-Beans need to be re-generated (" + changes + " files)");
100 }
101 getLog().info("*** Joda-Bean validator found " + changes + " beans in need of generation ***");
102 } else {
103 getLog().info("Joda-Bean validator completed");
104 }
105 }
106
107 }