4

I'm building a Java web app that packages in a WAR static resources. These static resources are built through Angular-CLI.

The Maven build triggers the ng build through Eirslett's maven-frontend-plugin, with npm scripts and the npm mojo.

Problem is, I would like to use a custom base href depending on Maven build parameters, and I did not manage to pass it to ng, either by environment variables or parameters.

Can someone tell me how to pass parameters to a ng build from Maven?

Code:

pom.xml

                 <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>node install</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>npm install</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>--registry=${npm.internal.registry} --userconfig ${basedir}/.jenkins-npmrc install</arguments>
                            </configuration>
                        </execution>
                        <execution>
                            <id>npm build</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>--userconfig ${basedir}/.jenkins-npmrc run-script build</arguments>
                                <environmentVariables>
                                    <test>this-does-not-work</test>
                                </environmentVariables>
                            </configuration>
                        </execution>
                        <execution>
                            <id>npm test</id>
                            <phase>test</phase>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <configuration>
                                <arguments>run-script test</arguments>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <nodeVersion>v8.6.0</nodeVersion>
                        <npmVersion>5.3.0</npmVersion>
                        <installDirectory>.</installDirectory>
                        <nodeDownloadRoot>${node.internal.repo}</nodeDownloadRoot>
                        <npmDownloadRoot>${npm.internal.registry}/npm/-/</npmDownloadRoot>
                        <installDirectory>.</installDirectory>
                        <workingDirectory>.</workingDirectory>
                    </configuration>
                </plugin>

package.json

"scripts": {
    (...)
    "build": "ng build --prod --env=prod --bh=process.env.test --base-href=process.env.test",
    (...)
  }
Silver Quettier
  • 2,045
  • 2
  • 26
  • 53

3 Answers3

2

I'm solving similar problem by having in (assumes regular angular-cli build entry in package.json):

pom.xml

...
<execution>
  <id>build</id>
  <goals>
    <goal>npm</goal>
  </goals>
  <configuration>
    <arguments>run build -- --base-href ${context.root}/</arguments>
  </configuration>
</execution>
...

You can then control <context.root> property via Maven profiles and having default value in parents Maven module <properties> section.

Pazkooda
  • 31
  • 3
1

I needed to pass the Maven project version to my NPM script and the below worked for me with the exec-maven-plugin:

pom.xml

<execution>
    <id>my-npm-build</id>
    <goals>
        <goal>exec</goal>
    </goals>
    <phase>compile</phase>
    <configuration>
        <executable>npm</executable>
        <arguments>
            <argument>run</argument>
            <argument>my-npm-script</argument>
        </arguments>
        <environmentVariables>
            <mvn_prj_vrsn>${project.version}</mvn_prj_vrsn>
        </environmentVariables>
    </configuration>
</execution>

And in package.json

"scripts": {
    "my-npm-script": "echo %mvn_prj_vrsn% &&[my other commands]
}

The above was for windows, in case of Linux use

$mvn_prj_vrsn
souser
  • 796
  • 2
  • 8
  • 25
  • This also worked for me. I used this gist as a more complete reference: https://gist.github.com/phillipgreenii/7c954e3c3911e5c32bd0 – elliottregan Aug 25 '21 at 23:54
0

This solution worked for me : On my pom.xml : i make a call to my script run build:prod

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

        <configuration>
            <nodeVersion>v11.8.0</nodeVersion>
            <npmVersion>6.5.0</npmVersion>
            <workingDirectory>src/main/webapp</workingDirectory>
        </configuration>

        <executions>
            <execution>
                <id>install node and npm</id>
                <goals>
                    <goal>install-node-and-npm</goal>
                </goals>
            </execution>
            <execution>
                <id>npm install</id>
                <goals>
                    <goal>npm</goal>
                </goals>
            </execution>
            <execution>
                <id>npm run build</id>
                <goals>
                    <goal>npm</goal>
                </goals>
                <phase>generate-resources</phase>
                <configuration>
                        <arguments>run build:prod</arguments>
                </configuration>
            </execution>

        </executions>
    </plugin>

On my angular app ( packaga.json ) :

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build:prod": "ng build --prod --build-optimizer --base-href /myBaseUrl/"
}, 

You could personalize the script as you like.

Saad Joudi
  • 595
  • 4
  • 5