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 running Joda-Beans.
028     * 
029     * @goal generate
030     * @phase generate-sources
031     * 
032     * @author Stephen Colebourne
033     */
034    public class JodaBeansGenerateMojo 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 expression="${project.build.sourceDirectory}"
050         * @required
051         * @readonly
052         */
053        private String sourceDir;
054    
055        /**
056         * Executes the Joda-Beans generator.
057         */
058        public void execute() throws MojoExecutionException, MojoFailureException {
059            if (sourceDir == null) {
060                throw new MojoExecutionException("Source directory must not be null");
061            }
062            
063            // build args
064            List<String> argsList = new ArrayList<String>();
065            argsList.add("-R");
066            if (indent != null) {
067                argsList.add("-indent=" + indent);
068            }
069            if (prefix != null) {
070                argsList.add("-prefix=" + prefix);
071            }
072            if (verbose != null) {
073                argsList.add("-verbose=" + verbose);
074            }
075            argsList.add(sourceDir);
076            
077            // run generator
078            getLog().info("Joda-Bean generator started, directory: " + sourceDir);
079            BeanCodeGen gen = null;
080            try {
081                String[] args = argsList.toArray(new String[argsList.size()]);
082                gen = BeanCodeGen.createFromArgs(args);
083            } catch (RuntimeException ex) {
084                throw new MojoFailureException("Invalid Joda-Beans Mojo configuration: " + ex.getMessage(), ex);
085            }
086            int changes = 0;
087            try {
088                changes = gen.process();
089            } catch (Exception ex) {
090                throw new MojoFailureException("Error while running Joda-Beans generator: " + ex.getMessage(), ex);
091            }
092            getLog().info("Joda-Bean generator completed, " + changes + " changed files");
093        }
094    
095    }