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