001    package org.apache.torque.mojo;
002    
003    import org.apache.maven.execution.MavenSession;
004    import org.apache.maven.plugin.AbstractMojo;
005    import org.apache.maven.plugin.MojoExecutionException;
006    import org.apache.maven.plugin.MojoFailureException;
007    import org.apache.maven.project.MavenProject;
008    import org.apache.maven.settings.Settings;
009    import org.kuali.maven.mojo.MavenLogger;
010    
011    /**
012     * Mojo essentials. Contains the "skip" logic that is the de facto standard for
013     * maven plugins. Contains a number of maven related properties that are common
014     * to most mojos. Also sets up logging so that if libraries called by a mojo
015     * issue log statements to Jakarta Commons Logging or Log4j, those log messages
016     * are shown in maven's output
017     */
018    public abstract class BaseMojo extends AbstractMojo {
019            public static final String FS = System.getProperty("file.separator");
020            public static final String SKIP_PACKAGING_TYPE = "pom";
021    
022            /**
023             * When true, redirect logging from Log4j and Jakarta Commons Logging to the
024             * Maven logging system
025             * 
026             * @parameter expression="${startMavenLogger}" default-value="true"
027             */
028            private boolean startMavenLogger;
029    
030            /**
031             * When <code>true</code>, skip the execution of this mojo
032             * 
033             * @parameter default-value="false"
034             */
035            private boolean skip;
036    
037            /**
038             * Setting this parameter to <code>true</code> will force the execution of
039             * this mojo, even if it would get skipped usually.
040             * 
041             * @parameter expression="${forceMojoExecution}" default-value="false"
042             * @required
043             */
044            private boolean forceMojoExecution;
045    
046            /**
047             * The encoding to use when reading/writing files. If not specified this
048             * defaults to the platform specific encoding of whatever machine the build
049             * is running on.
050             * 
051             * @parameter expression="${encoding}"
052             *            default-value="${project.build.sourceEncoding}"
053             */
054            private String encoding;
055    
056            /**
057             * The Maven project this plugin runs in.
058             * 
059             * @parameter expression="${project}"
060             * @required
061             * @readonly
062             */
063            private MavenProject project;
064    
065            /**
066             * @parameter expression="${settings}"
067             * @required
068             * @since 1.0
069             * @readonly
070             */
071            private Settings settings;
072    
073            /**
074             * @parameter default-value="${session}"
075             * @required
076             * @readonly
077             */
078            private MavenSession mavenSession;
079    
080            protected void beforeExecution() throws MojoExecutionException, MojoFailureException {
081            }
082    
083            protected void afterExecution() throws MojoExecutionException, MojoFailureException {
084            }
085    
086            @Override
087            public void execute() throws MojoExecutionException, MojoFailureException {
088                    beforeExecution();
089                    if (isStartMavenLogger()) {
090                            MavenLogger.startPluginLog(this);
091                    }
092                    if (skipMojo()) {
093                            return;
094                    }
095                    executeMojo();
096                    if (isStartMavenLogger()) {
097                            MavenLogger.endPluginLog(this);
098                    }
099                    afterExecution();
100            }
101    
102            protected abstract void executeMojo() throws MojoExecutionException, MojoFailureException;
103    
104            /**
105             * <p>
106             * Determine if the mojo execution should get skipped.
107             * </p>
108             * This is the case if:
109             * <ul>
110             * <li>{@link #skip} is <code>true</code></li>
111             * <li>if the mojo gets executed on a project with packaging type 'pom' and
112             * {@link #forceMojoExecution} is <code>false</code></li>
113             * </ul>
114             * 
115             * @return <code>true</code> if the mojo execution should be skipped.
116             */
117            protected boolean skipMojo() {
118                    if (skip) {
119                            getLog().info("Skipping execution");
120                            return true;
121                    }
122    
123                    if (!forceMojoExecution && project != null && SKIP_PACKAGING_TYPE.equals(project.getPackaging())) {
124                            getLog().info("Skipping execution for project with packaging type '" + SKIP_PACKAGING_TYPE + "'");
125                            return true;
126                    }
127    
128                    return false;
129            }
130    
131            /**
132             * Returns the maven project.
133             * 
134             * @return The maven project where this plugin runs in.
135             */
136            public MavenProject getProject() {
137                    return project;
138            }
139    
140            public String getEncoding() {
141                    return encoding;
142            }
143    
144            public void setEncoding(String encoding) {
145                    this.encoding = encoding;
146            }
147    
148            public boolean isSkip() {
149                    return skip;
150            }
151    
152            public void setSkip(boolean skip) {
153                    this.skip = skip;
154            }
155    
156            public boolean isForceMojoExecution() {
157                    return forceMojoExecution;
158            }
159    
160            public void setForceMojoExecution(boolean forceMojoExecution) {
161                    this.forceMojoExecution = forceMojoExecution;
162            }
163    
164            public Settings getSettings() {
165                    return settings;
166            }
167    
168            public void setSettings(Settings settings) {
169                    this.settings = settings;
170            }
171    
172            public MavenSession getMavenSession() {
173                    return mavenSession;
174            }
175    
176            public void setMavenSession(MavenSession mavenSession) {
177                    this.mavenSession = mavenSession;
178            }
179    
180            public void setProject(MavenProject project) {
181                    this.project = project;
182            }
183    
184            public boolean isStartMavenLogger() {
185                    return startMavenLogger;
186            }
187    
188            public void setStartMavenLogger(boolean startMavenLogger) {
189                    this.startMavenLogger = startMavenLogger;
190            }
191    }