~hudson-ubuntu/+junk/maven-hpi-plugin

« back to all changes in this revision

Viewing changes to src/main/java/org/jenkinsci/maven/plugins/hpi/AptMojo.java

  • Committer: James Page
  • Date: 2011-07-01 12:02:19 UTC
  • Revision ID: james.page@canonical.com-20110701120219-4bjxf1k3vjblsixm
Upgrade and polish

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// TAKEN FROM GLASSFISH. TODO: FIND CDDL HEADER
 
2
package org.jenkinsci.maven.plugins.hpi;
 
3
 
 
4
import org.apache.maven.plugin.CompilationFailureException;
 
5
import org.apache.maven.plugin.MojoExecutionException;
 
6
 
 
7
import java.lang.reflect.Field;
 
8
 
 
9
/**
 
10
 * @goal apt-compile
 
11
 * @phase compile
 
12
 * @requiresDependencyResolution compile
 
13
 * @author Kohsuke Kawaguchi
 
14
 */
 
15
public class AptMojo extends CompilerMojo {
 
16
    public void execute() throws MojoExecutionException, CompilationFailureException {
 
17
        // overwrite the compilerId value. This seems to be the only way to
 
18
        //do so without touching the copied files.
 
19
        setField("compilerId", "hpi-apt");
 
20
 
 
21
        if(!isMustangOrAbove())
 
22
            throw new MojoExecutionException("JDK6 or later is necessary to build a Jenkins plugin");
 
23
 
 
24
        super.execute();
 
25
    }
 
26
 
 
27
    /**
 
28
     * Are we running on JDK6 or above?
 
29
     */
 
30
    private static boolean isMustangOrAbove() {
 
31
        try {
 
32
            Class.forName("javax.annotation.processing.Processor");
 
33
            return true;
 
34
        } catch(ClassNotFoundException e) {
 
35
            return false;
 
36
        }
 
37
    }
 
38
 
 
39
    private void setField(String name, String value) {
 
40
        try {
 
41
            Field field = AbstractCompilerMojo.class.getDeclaredField(name);
 
42
            field.setAccessible(true);
 
43
            field.set(this, value);
 
44
        } catch (NoSuchFieldException e) {
 
45
            throw new AssertionError(e); // impossible
 
46
        } catch (IllegalAccessException e) {
 
47
            throw new AssertionError(e); // impossible
 
48
        }
 
49
    }
 
50
}