Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target maven executions to the build folder only?

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>
like image 524
ViniciusPires Avatar asked Jan 15 '15 12:01

ViniciusPires


People also ask

How do I customize a Maven build for different environments?

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments. Profiles are specified in pom.

Where do I put build in POM xml?

Build plugins They execute during the build process and should be configured in the <build/> element of pom. xml.

What is the use of build tag in POM xml?

Plugins defined in your buildsection plugins tag will be executed during the build of your project. There are many plugins that do something with your build. For example the maven-compiler-plugin which allows you to set the Java version for your project.

What is build plugin in Maven?

Plugins are the central feature of Maven that allow for the reuse of common build logic across multiple projects. They do this by executing an "action" (i.e. creating a WAR file or compiling unit tests) in the context of a project's description - the Project Object Model (POM).


2 Answers

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>
            ...
like image 135
chivorotkiv Avatar answered Nov 16 '22 23:11

chivorotkiv


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>
like image 22
Noremac Avatar answered Nov 16 '22 22:11

Noremac