001/* 002 * Copyright 2013 Chris Pheby 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 */ 016package org.jadira.maven.plugins.fmpp; 017 018import java.io.File; 019 020import org.apache.maven.plugin.AbstractMojo; 021import org.apache.maven.plugin.MojoExecutionException; 022import org.apache.maven.plugin.MojoFailureException; 023import org.apache.maven.plugins.annotations.LifecyclePhase; 024import org.apache.maven.plugins.annotations.Mojo; 025import org.apache.maven.plugins.annotations.Parameter; 026import org.apache.maven.project.MavenProject; 027 028import fmpp.ProcessingException; 029import fmpp.progresslisteners.ConsoleProgressListener; 030import fmpp.setting.SettingException; 031import fmpp.setting.Settings; 032import fmpp.util.MiscUtil; 033 034/** 035 * A Maven front-end for FMPP. Inspired by Faisal Feroz' FMPP-Maven-Plugin, this implementation 036 * gives the same general behaviour, but also allows configuration of whether the generated sources are 037 * added to src or test. The default configuration adds the generated-sources to compile scope (not test) 038 * @author Chris Pheby 039 */ 040@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES) 041public class FmppMojo extends AbstractMojo { 042 043 @Parameter(defaultValue="${project}", required=true, readonly=true) 044 private MavenProject project; 045 046 /** 047 * If true, the generated sources are added as a source compilation root. 048 */ 049 @Parameter(defaultValue="true", property="compileSourceRoot") 050 private boolean compileSourceRoot; 051 052 /** 053 * If true, the generated sources are added as a test-source compilation root. 054 */ 055 @Parameter(defaultValue="false", property="testCompileSourceRoot") 056 private boolean testCompileSourceRoot; 057 058 /** 059 * The location of the FreeMarker configuration file to use. 060 */ 061 @Parameter(defaultValue="src/main/resources/fmpp/config.fmpp", required=true, property="cfgFile") 062 private File cfgFile; 063 064 /** 065 * The location where the FreeMarker template files to be processed are stored. 066 */ 067 @Parameter(defaultValue="src/main/resources/fmpp/templates/", required=true, property="templateDirectory") 068 private File templateDirectory; 069 070 /** 071 * The directory where generated sources should be output 072 */ 073 @Parameter(defaultValue="${project.build.directory}/generated-sources/fmpp/", required=true, property="outputDirectory") 074 private File outputDirectory; 075 076 public void execute() throws MojoExecutionException, MojoFailureException { 077 078 checkParameters(); 079 080 if (!outputDirectory.exists()) { 081 outputDirectory.mkdirs(); 082 } 083 084 getLog().info("Beginning FMPP Processing... "); 085 086 try { 087 Settings settings = new Settings(new File(".")); 088 089 settings.set("sourceRoot", templateDirectory.getAbsolutePath()); 090 settings.set("outputRoot", outputDirectory.getAbsolutePath()); 091 092 settings.load(cfgFile); 093 094 settings.addProgressListener(new ConsoleProgressListener()); 095 settings.execute(); 096 } catch (SettingException e) { 097 handleError(e); 098 } catch (ProcessingException e) { 099 handleError(e); 100 } 101 102 getLog().info("Successfully completed FMPP Processing... "); 103 104 if (compileSourceRoot) { 105 project.addCompileSourceRoot(outputDirectory.getAbsolutePath()); 106 } 107 if (testCompileSourceRoot) { 108 project.addTestCompileSourceRoot(outputDirectory.getAbsolutePath()); 109 } 110 } 111 112 private void handleError(Exception e) throws MojoFailureException { 113 114 getLog().error(MiscUtil.causeMessages(e)); 115 throw new MojoFailureException(MiscUtil.causeMessages(e), e); 116 } 117 118 private void checkParameters() throws MojoExecutionException { 119 120 if (project == null) { 121 throw new MojoExecutionException("This plugin can only be used as part of a Maven project."); 122 } 123 if (cfgFile == null) { 124 throw new MojoExecutionException("configFile is a required parameter"); 125 } 126 if (templateDirectory == null) { 127 throw new MojoExecutionException("templateDirectory is a required parameter"); 128 } 129 if (outputDirectory == null) { 130 throw new MojoExecutionException("outputDirectory is a required parameter"); 131 } 132 } 133}