【问题标题】:Parameterized Angular CLI build call from Maven (Eirslett's plugin)来自 Maven 的参数化 Angular CLI 构建调用(Eirslett 的插件)
【发布时间】:2023-03-14 09:48:02
【问题描述】:

我正在构建一个 Java Web 应用程序,它打包在 WAR 静态资源中。这些静态资源是通过 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",
    (...)
  }

【问题讨论】:

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


    【解决方案1】:

    我需要将 Maven 项目版本传递给我的 NPM 脚本,下面的代码使用 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>
    

    在 package.json 中

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

    以上是针对windows的,如果是Linux的话

    $mvn_prj_vrsn
    

    【讨论】:

    【解决方案2】:

    我正在解决类似的问题(假设在package.json 中有常规的angular-cli build 条目):

    pom.xml

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

    然后您可以通过 Maven 配置文件控制 &lt;context.root&gt; 属性,并在父级 Maven 模块 &lt;properties&gt; 部分中设置默认值。

    【讨论】:

    • 正是我所需要的。谢谢!
    【解决方案3】:

    这个解决方案对我有用: 在我的 pom.xml 上:我调用我的脚本 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>
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-15
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      相关资源
      最近更新 更多