Abhishek Kumar
Abhishek Kumar

Reputation: 41

Running JavaScript File during Maven LifeCycle

Earlier I run .sh and .rb file in the maven life cycle using the following,

          <plugin>
               <groupId>org.codehaus.mojo</groupId>
               <artifactId>exec-maven-plugin</artifactId>
               <version>1.1</version>
               <executions>
                   <execution>
                       <id>Version Calculation</id>
                       <phase>generate-sources</phase>
                       <goals>
                           <goal>exec</goal>
                       </goals>
                   </execution>
               </executions>
               <configuration>
                     <executable>${basedir}/scripts/IndexRunner/test.rb</executable>
               </configuration>
           </plugin>

Now I want to run a .js file, I tried by replacing test.rb to some file test.js but its not working, any help to how to run a javascript file during maven lifecycle

Upvotes: 4

Views: 2910

Answers (2)

Archangel1C
Archangel1C

Reputation: 120

FYI, there is also a maven plugin on Github (using Apache Bean Scripting Framework) for this task: https://github.com/alexec/script-maven-plugin

Upvotes: 0

Cemo
Cemo

Reputation: 5570

You should use a java class which is triggering javascript file. And with exec plugin you can run this file. The content of the file is like this.

import javax.script.*;
public class ExecuteScript {
    public static void main(String[] args) throws Exception {
       // create a script engine manager
       ScriptEngineManager factory = new ScriptEngineManager();
       // create a JavaScript engine
       ScriptEngine engine = factory.getEngineByName("JavaScript");
      // evaluate JavaScript code from String
      engine.eval("print('Welocme to java world')");
      // or call file as you wish
      engine.eval(new java.io.FileReader("welcome.js"));
    }
}

Upvotes: 2

Related Questions