001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 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 017 package org.codehaus.gmaven.mojo; 018 019 import groovy.lang.GroovyObjectSupport; 020 import groovy.util.AntBuilder; 021 import org.apache.maven.plugin.ContextEnabled; 022 import org.apache.maven.plugin.Mojo; 023 import org.apache.maven.plugin.MojoExecutionException; 024 import org.apache.maven.plugin.logging.Log; 025 import org.apache.maven.plugin.logging.SystemStreamLog; 026 import org.apache.tools.ant.BuildLogger; 027 028 import java.util.Map; 029 030 /** 031 * Provides support for Maven 2 plugins implemented in Groovy. 032 * 033 * @version $Id: GroovyMojo.java 85 2009-12-11 11:02:23Z user57 $ 034 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 035 */ 036 public abstract class GroovyMojo 037 extends GroovyObjectSupport 038 implements Mojo, ContextEnabled 039 { 040 private AntBuilder ant; 041 042 /** 043 * Lazily initialize the AntBuilder, so we can pick up the log impl correctly. 044 */ 045 private AntBuilder getAnt() { 046 if (this.ant == null) { 047 AntBuilder ant = new AntBuilder(); 048 BuildLogger logger = (BuildLogger) ant.getAntProject().getBuildListeners().get(0); 049 logger.setEmacsMode(true); 050 this.ant = ant; 051 } 052 return this.ant; 053 } 054 055 public Object getProperty(final String property) { 056 // TODO: Check if this is really needed 057 if ("ant".equals(property)) { 058 return getAnt(); 059 } 060 if ("log".equals(property)) { 061 return getLog(); 062 } 063 return super.getProperty(property); 064 } 065 066 protected void fail(final Object msg) throws MojoExecutionException { 067 if (msg instanceof Throwable) { 068 Throwable cause = (Throwable)msg; 069 fail(cause.getMessage(), cause); 070 } 071 072 throw new MojoExecutionException(String.valueOf(msg)); 073 } 074 075 protected void fail(final Object msg, final Throwable cause) throws MojoExecutionException { 076 throw new MojoExecutionException(String.valueOf(msg), cause); 077 } 078 079 // 080 // Mojo 081 // 082 083 private Log log; 084 085 private Map pluginContext; 086 087 public void setLog(final Log log) { 088 this.log = log; 089 } 090 091 public Log getLog() { 092 if (log == null) { 093 log = new SystemStreamLog(); 094 } 095 096 return log; 097 } 098 099 public Map getPluginContext() { 100 return pluginContext; 101 } 102 103 public void setPluginContext(final Map context) { 104 this.pluginContext = context; 105 } 106 }