How to target maven executions to the build folder only?

17,370

Solution 1

It's possible to use <installDirectory> configuration parameter to choose where to install NodeJS.

frontend-maven-plugin will install node_modules in the place where it founds package.json. That's why you need to provide copy of your web resources with package.json to some target/<sub-path>.

Then frontend-maven-plugin may be configured by this way:

       <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>0.0.24</version>

            <configuration>
                <nodeVersion>v0.11.14</nodeVersion>
                <npmVersion>2.13.4</npmVersion>
                <installDirectory>target/<sub-path></installDirectory>
                <workingDirectory>target/<sub-path></workingDirectory>
            </configuration>
            ...

Solution 2

The plugin (frontend-maven-plugin) mostly supports this. The "workingDirectory" parameter tells plugin where to do the work (i.e. npm install). That requires though that the build files (i.e. package.json, gruntFile.js) be in that workingDirectory. To accomodate this I added a antrun execution to copy those files over (with filtering) before the rest of the build. My grunt file then references the files from source when appropriate and any output goes in my target-grunt folder.

Here is my config:

        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>0.0.20</version>
            <configuration>
                <workingDirectory>target-grunt</workingDirectory>
            </configuration>
            <executions>
               ...
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>prepare-grunt</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <!-- copy, filter, rename -->
                            <filter token="moduleName" value="${moduleName}" />
                            <filter token="project.version" value="${project.version}" />
                            <filter token="project.artifactId" value="${project.artifactId}" />
                            <copy file="${basedir}/target-grunt/imported-js/main/js/com/verisk/underwriting/config/grunt/npm-package-module/0.0.1/npm-package-module-0.0.1.js" tofile="${basedir}/target-grunt/package.json" filtering="true" failonerror="true" verbose="true" />
                            <copy file="${basedir}/Gruntfile.js" tofile="${basedir}/target-grunt/Gruntfile.js" failonerror="true" verbose="true" />
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Share:
17,370

Related videos on Youtube

ViniciusPires
Author by

ViniciusPires

Student, Musician, Wushu fighter &amp; Programmer.

Updated on June 04, 2022

Comments

  • ViniciusPires
    ViniciusPires almost 2 years

    I have a project using Maven and the frontend-maven-plugin (com.github.eirslett).

    As I run mvn install all the executions from the plugin run, and they create a node_modules, bower_components, and node folders in the src/main/webapp root, where the actual frontend code is.

    The thing is, I wanted to mvn install only execute and create those in war the package generated in the build directory, not in the versioned application code, just like it does with Java libraries.

    Is there a way to achieve that?

    This is the relevant part of my pom.xml:

    <build>
        <directory>build</directory>
        <outputDirectory>build/classes</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>WEB-INF/weblogic.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            ...
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>0.0.20</version>
    
                <configuration>
                    <workingDirectory>src/main/webapp</workingDirectory>
                </configuration>
    
                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>v0.10.34</nodeVersion>
                            <npmVersion>2.1.11</npmVersion>
                        </configuration>
                    </execution>
    
                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>
    
                    <execution>
                        <id>bower install</id>
                        <goals>
                            <goal>bower</goal>
                        </goals>
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>
    
                    <execution>
                        <id>grunt build</id>
                        <goals>
                            <goal>grunt</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>