Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Adding custom frontend module to AEM repo - nx monorepo

Avatar

Level 1

Hello,

 

I am trying to add new frontend module which is "npm nx build".  I have seen npm install & build are working fine using "com.github.eirslett" plugin, but same plugin is not working for nx install and nx build. Please let me know if anyone have tried this approach.

 

Thanks and regards,

Swathi.

4 Replies

Avatar

Community Advisor

Hi @SwathiCh1 ,

Integrating a custom frontend module using NX monorepo into an AEM project can be accomplished by extending the existing frontend-maven-plugin configuration to support NX commands. The frontend-maven-plugin is commonly used in AEM projects to run frontend build processes such as npm install and npm build.

To run NX-specific commands like nx install and nx build, you will need to configure the frontend-maven-plugin to execute these commands. Here's how you can achieve this:

Step-by-Step Guide

  1. Add frontend-maven-plugin to your Maven POM file:

    Ensure you have the frontend-maven-plugin in your pom.xml:

 

<build>
    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.10.0</version>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v16.13.0</nodeVersion>
                        <npmVersion>8.1.0</npmVersion>
                    </configuration>
                </execution>
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>nx build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run nx build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 

Customize the Plugin Configuration for NX:

If you need to run additional NX commands or handle different tasks, you can add more executions to the frontend-maven-plugin configuration. For example, to run nx lint, nx test, and other NX-specific tasks:

 

<execution>
    <id>nx lint</id>
    <goals>
        <goal>npm</goal>
    </goals>
    <configuration>
        <arguments>run nx lint</arguments>
    </configuration>
</execution>
<execution>
    <id>nx test</id>
    <goals>
        <goal>npm</goal>
    </goals>
    <configuration>
        <arguments>run nx test</arguments>
    </configuration>
</execution>

 

Ensure Compatibility with Your NX Monorepo:

  • Make sure the paths and configurations in your NX workspace are correctly set up.
  • Ensure that the package.json scripts are correctly defined to map to your NX commands, for example:

 

"scripts": {
    "nx": "nx",
    "build": "nx build",
    "lint": "nx lint",
    "test": "nx test"
}
​

 

Run Maven Build:

After setting up the frontend-maven-plugin in your pom.xml, you can run your Maven build commands as usual. Maven will handle the frontend tasks via the frontend-maven-plugin

mvn clean install

  1. Verify the Build Process:

    Ensure that the build process correctly executes the NX commands and that the frontend build artifacts are generated as expected.

Additional Notes

  • Node and NPM Versions: Ensure that the versions specified for Node and NPM in the frontend-maven-plugin configuration are compatible with your NX workspace.
  • Execution Order: The order of executions in the Maven configuration matters. Ensure that npm install is executed before any other NX commands to install dependencies first.

Example Project Structure

Your project structure might look like this:

 

/my-aem-project
|-- /ui.apps
|-- /ui.content
|-- /ui.frontend
|   |-- /apps
|   |-- /libs
|   |-- /nx-workspace
|       |-- /apps
|       |-- /libs
|       |-- package.json
|       |-- nx.json
|       |-- angular.json
|-- pom.xml

 

In the example above, the ui.frontend module contains your NX workspace, and the pom.xml is set up to run the necessary NX commands via the frontend-maven-plugin.

By following these steps, you should be able to integrate and build your NX-based frontend module within an AEM project using Maven. This approach leverages the frontend-maven-plugin to handle the frontend build process seamlessly within the AEM build lifecycle.

Avatar

Community Advisor

@SwathiCh1 

Did you to configure the execution with the "npm" goal and specify the "arguments" parameter with the command? If not, try adding it. This should solve the problem.
 
 
 
 

Avatar

Community Advisor

@SwathiCh1 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.