If you want to run a Gulp Script from Maven, you use the frontend plugin, right? Unfortunately, that seems to assume that the Gulp script is part of the Java project.

I'm creating a Java Backend to the LearnWith series and am sharing UI code between multiple backend technologies. As such, my directory structure is different and the UI code is not in the web root. I want to run a gulp script in a different directory, and then copy the results into the project's webapp directory. How do you do it?

I was able to do it with the exec Maven Plugin.

First, set up the plugin in the plugins portion of Maven POM:

view plain print about
1<plugin>
2 <groupId>org.codehaus.mojo</groupId>
3 <artifactId>exec-maven-plugin</artifactId>
4 <version>1.6.0</version>

The groupId, arftifactId, and version are specified. For each script you want to run you'll need an execution statement:

view plain print about
1<executions>
2 <execution>
3 <id>Build Angular 5</id>
4 <phase>validate</phase>
5 <goals>
6 <goal>exec</goal>
7 </goals>
8 <configuration>
9 <executable>gulp</executable>
10 <arguments>
11 <argument>build</argument>
12 </arguments>
13 <workingDirectory>C:\Projects\lw\A5\chapter7\Angular5TypeScript</workingDirectory>
14 </configuration>
15 </execution>
16 </executions>

The id is a unique name for the build script. The phase is validate, because it is. The goal should be set to exec. The configuration is where the real magic happens. We tell it to execute the gulp command with the argument 'build', so this will run:

view plain print about
1gulp build

We want to make sure that the script runs in the proper directory and that is specified with the workingDirectory tag. Presumably this directory will be outside the current directory structure.

Finally, close the plugin tag:

view plain print about
1</plugin>

That's all you need to do to run an external Gulp Script from a Maven project. This assumes, of course, you have Gulp installed. Next week I'm going to write about copying the processed files into the Java project.