来自Maven(Eirslett的插件)的参数化Angular CLI构建调用

时间:2018-03-13 12:20:07

标签: maven angular-cli maven-frontend-plugin

我正在构建一个打包在WAR静态资源中的Java Web应用程序。这些静态资源是通过Angular-CLI构建的。

Maven构建通过Eirslett的maven-frontend-plugin触发ng构建,使用npm脚本和npm mojo。

问题是,我想根据Maven构建参数使用自定义base href,并且我没有设法通过环境变量或参数将其传递给ng。

有人可以告诉我如何将参数传递给Maven的ng版本吗?

代码:

的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",
    (...)
  }

3 个答案:

答案 0 :(得分:0)

我通过参加build中的常规 angular-cli package.json条目来解决类似的问题:

的pom.xml

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

然后,您可以通过Maven个人资料控制<context.root>属性,并在父级Maven模块<properties>部分中使用默认值。

答案 1 :(得分:0)

我需要将Maven项目版本传递给我的NPM脚本,以下内容对我来说{@ 1}有效:

pom.xml

exec-maven-plugin

在package.json中

<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>

以上是针对Windows(如果使用Linux的话)

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

答案 2 :(得分:0)

此解决方案对我有用: 在我的pom.xml上:我打电话给我的脚本运行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>

在我的角度应用程序(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/"
}, 

您可以根据需要个性化脚本。

相关问题