Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized Angular CLI build call from Maven (Eirslett's plugin)

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",
    (...)
  }
like image 239
Silver Quettier Avatar asked Oct 17 '22 21:10

Silver Quettier


2 Answers

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.

like image 70
Pazkooda Avatar answered Oct 21 '22 05:10

Pazkooda


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
like image 23
souser Avatar answered Oct 21 '22 03:10

souser