frontend-maven-plugin

repository·master·Indexed 26 days ago

https://github.com/eirslett/frontend-maven-plugin

A Maven plugin that automates the management of frontend build tools by downloading and installing local versions of Node, NPM, Yarn, Corepack, or Bun. It allows developers to run frontend tasks such as Webpack, Gulp, Grunt, and Karma tests without requiring global installations on their machines, ensuring consistent build environments across different systems.

Tokens
3.5K
Snippets
14
Records
17
Agent score
38%

What's inside frontend-maven-plugin

  1. Use Eclipse M2E for incremental builds

    master

    The plugin includes M2E support, including lifecycle mappings and support for incremental builds in Eclipse.

    To optimize build times:

    • The install-node-and-npm goal only runs during a full project build.
    • The npm goal only runs during an incremental build if the package.json file has changed.
    • For grunt and gulp goals, you can use the srcdir and triggerfiles configuration options to check for changes in specific source files before execution.
  2. Install Node and Yarn locally

    master

    To use Yarn as your package manager, use the install-node-and-yarn goal.

    Yarn Berry (2.x+) Support: If your project uses Yarn Berry, the plugin detects a .yarnrc.yml file and automatically installs the version specified in yarnVersion as a 'bootstrap' 1.x version before handing over to your project-specific version.

    <plugin>
        <executions>
            <execution>
                <id>install node and yarn</id>
                <goals>
                    <goal>install-node-and-yarn</goal>
                </goals>
                <phase>generate-resources</phase>
            </execution>
        </executions>
        <configuration>
            <nodeVersion>v24.12.0</nodeVersion>
            <yarnVersion>v0.16.1</yarnVersion>
            <nodeDownloadRoot>http://myproxy.example.org/nodejs/</nodeDownloadRoot>
            <yarnDownloadRoot>http://myproxy.example.org/yarn/</yarnDownloadRoot>
        </configuration>
    </plugin>
  3. Install the frontend-maven-plugin

    master

    To use this plugin, add it as a dependency in your Maven pom.xml. This plugin allows you to manage Node and NPM locally within your project, ensuring consistent build environments across different machines without requiring global installations.

    Note: This plugin is designed to download and install Node/NPM locally. It does not support using Node or NPM versions that are already installed on your system. If you need to use an existing installation, use exec-maven-plugin instead.

    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <!-- Use the latest released version: 
            https://repo1.maven.org/maven2/com/github/eirslett/frontend-maven-plugin/ -->
            <version>LATEST_VERSION</version>
            ...
        </plugin>
    </plugins>
  4. Install Node and npm locally

    master

    To install Node.js and npm locally within your project (without affecting the global system), use the install-node-and-npm goal. The versions are downloaded from https://nodejs.org/dist and extracted into a node folder in your installation directory.

    Note: You should add the node folder to your .gitignore.

    <plugin>
        <executions>
            <execution>
                <id>install node and npm</id>
                <goals>
                    <goal>install-node-and-npm</goal>
                </goals>
                <phase>generate-resources</phase>
            </execution>
        </executions>
        <configuration>
            <nodeVersion>v24.12.0</nodeVersion>
            <npmVersion>11.6.2</npmVersion>
            <downloadRoot>http://myproxy.example.org/nodejs/</downloadRoot>
        </configuration>
    </plugin>
  5. Install Node and Corepack locally

    master

    Use the install-node-and-corepack goal to let Corepack manage your package manager versions. Node is downloaded from https://nodejs.org/dist. Corepack is either provided with Node or downloaded from https://repository.npmjs.org.

    <plugin>
        <executions>
            <execution>
                <id>install-node-and-corepack</id>
                <goals>
                    <goal>install-node-and-corepack</goal>
                </goals>
                <phase>generate-resources</phase>
            </execution>
        </executions>
        <configuration>
            <nodeVersion>v24.12.0</nodeVersion>
            <corepackVersion>v0.25.2</corepackVersion>
            <nodeDownloadRoot>http://myproxy.example.org/nodejs/</nodeDownloadRoot>
            <corepackDownloadRoot>http://myproxy.example.org/corepack/</corepackDownloadRoot>
        </configuration>
    </plugin>
  6. Install Bun locally

    master

    To install Bun, use the install-bun goal. Bun is downloaded from https://github.com/oven-sh/bun/releases/download/ and extracted into a bun folder in your installation directory.

    <plugin>
        <executions>
            <execution>
                <id>install bun</id>
                <goals>
                    <goal>install-bun</goal>
                </goals>
                <phase>generate-resources</phase>
            </execution>
        </executions>
        <configuration>
            <bunVersion>v1.1.34</bunVersion>
            <bunDownloadRoot>http://myproxy.example.org/bun/</bunDownloadRoot>
        </configuration>
    </plugin>
  7. Configure plugin working directory and installation directory

    master

    Working Directory

    Set the directory where package.json and frontend config files (like Gruntfile.js) reside. Defaults to the project base directory.

    Installation Directory

    Set the folder where Node/npm/Yarn/etc. are installed. This can be set globally for the plugin or per goal.

    <configuration>
        <workingDirectory>src/main/frontend</workingDirectory>
        <installDirectory>target</installDirectory>
    </configuration>
  8. Configure Yarn with a private registry

    master

    To use a private npm registry with Yarn, you can use the npmRegistryURL configuration option within a yarn execution.

    <execution>
        <id>yarn install</id>
        <goals>
            <goal>yarn</goal>
        </goals>
        <configuration>
             <arguments>install</arguments>
             <npmRegistryURL>http://myregistry.example.org/</npmRegistryURL>
        </configuration>
    </execution>
  9. Configure proxy settings for npm, bower, and yarn

    master

    The plugin automatically inherits proxy settings from Maven's settings.xml. If you need to disable this inheritance for specific tools, use the following configuration keys:

    • npmInheritsProxyConfigFromMaven: Set to false to stop npm from using Maven proxy settings.
    • bowerInheritsProxyConfigFromMaven: Set to false to stop bower from using Maven proxy settings.
    • yarnInheritsProxyConfigFromMaven: Set to false to stop yarn from using Maven proxy settings.
    <configuration>
        <npmInheritsProxyConfigFromMaven>false</npmInheritsProxyConfigFromMaven>
        <bowerInheritsProxyConfigFromMaven>false</bowerInheritsProxyConfigFromMaven>
        <yarnInheritsProxyConfigFromMaven>false</yarnInheritsProxyConfigFromMaven>
    </configuration>
  10. Run Karma tests

    master

    Use the karma goal to run JavaScript tests. You can specify a custom configuration file using karmaConfPath.

    CI Best Practices: It is recommended to use a separate configuration (e.g., karma.conf.ci.js) for CI environments that generates XML reports and code coverage.

    Maven Flags:

    • -DskipTests: Skips Karma tests.
    • -Dmaven.test.failure.ignore: Allows the build to continue even if tests fail (useful for CI).
    <execution>
        <id>javascript tests</id>
        <goals>
            <goal>karma</goal>
        </goals>
        <phase>test</phase>
        <configuration>
            <karmaConfPath>src/test/javascript/karma.conf.ci.js</karmaConfPath>
        </configuration>
    </execution>