View Javadoc
1   package org.apache.maven.plugins.scripting;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Evaluate the specified script or scriptFile
37   *
38   * @author Robert Scholte
39   * @since 3.0.0
40   */
41  @Mojo( name = "eval" )
42  public class EvalMojo
43      extends AbstractMojo
44  {
45      @Parameter
46      private String engineName;
47  
48      /**
49       * When used, also specify the engineName
50       */
51      @Parameter
52      private String script;
53  
54      /**
55       * Provide the script as an external file as an alternative to <script>.
56       * When scriptFile provided the script is ignored.
57       * The file name extension identifies the script language to use, as of javax.script.ScriptEngineManager
58       * and {@linkplain "https://jcp.org/aboutJava/communityprocess/final/jsr223/index.html"}
59       */
60      @Parameter
61      private File scriptFile;
62  
63      // script variables
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 ) // configuring the plugin failed
88         {
89           throw new MojoExecutionException( e.getMessage(), e );
90         }
91         catch ( UnsupportedScriptEngineException e ) // execution failure
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 }