1 package org.apache.maven.plugins.scripting;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23
24 import javax.script.Bindings;
25 import javax.script.ScriptException;
26 import javax.script.SimpleBindings;
27
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.project.MavenProject;
34
35
36
37
38
39
40
41 @Mojo( name = "eval" )
42 public class EvalMojo
43 extends AbstractMojo
44 {
45 @Parameter
46 private String engineName;
47
48
49
50
51 @Parameter
52 private String script;
53
54
55
56
57
58
59
60 @Parameter
61 private File scriptFile;
62
63
64 @Parameter( defaultValue = "${project}", readonly = true )
65 private MavenProject project;
66
67 @Override
68 public void execute()
69 throws MojoExecutionException, MojoFailureException
70 {
71 try
72 {
73 AbstractScriptEvaluator execute = constructExecute();
74
75 Bindings bindings = new SimpleBindings();
76 bindings.put( "project", project );
77 bindings.put( "log", getLog() );
78
79 Object result = execute.eval( bindings );
80
81 getLog().info( "Result:" );
82 if ( result != null )
83 {
84 getLog().info( result.toString() );
85 }
86 }
87 catch ( ScriptException e )
88 {
89 throw new MojoExecutionException( e.getMessage(), e );
90 }
91 catch ( UnsupportedScriptEngineException e )
92 {
93 throw new MojoFailureException( e.getMessage(), e );
94 }
95 }
96
97 private AbstractScriptEvaluator constructExecute() throws IllegalArgumentException
98 {
99 AbstractScriptEvaluator execute;
100
101 if ( scriptFile != null )
102 {
103 execute = new FileScriptEvaluator( engineName, scriptFile );
104
105 }
106 else if ( script != null )
107 {
108 execute = new StringScriptEvaluator( engineName, script );
109
110 }
111 else
112 {
113 throw new IllegalArgumentException( "Missing script or scriptFile provided" );
114 }
115 return execute;
116 }
117 }