简体   繁体   English

Angular2:通过Maven使用npm安装外部库

[英]Angular2: Install external library using npm through Maven

Am building an Angular2 SpringBoot application which I need to deploy directly to a container like tomcat like a war file. 上午构建Angular2 SpringBoot应用程序,我需要直接部署到像一个容器tomcatwar文件。 Am able to successfully do that by install npm pluging in maven like: 能够通过安装npm来成功地执行以下操作:

<plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <workingDirectory>${angular.project.location}</workingDirectory>
                <installDirectory>${angular.project.nodeinstallation}</installDirectory>
            </configuration>
            <executions>
                <!-- It will install nodejs and npm -->
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v9.2.0</nodeVersion>
                        <npmVersion>5.6.0</npmVersion>
                    </configuration>
                </execution>

                <!-- It will execute command "npm install" inside "/e2e-angular2" directory -->
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <!-- It will execute command "npm build" inside "/e2e-angular2" directory 
                    to clean and create "/dist" directory -->
                <execution>
                    <id>npm build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Now, am also using Ng2Charts library to build highcharts on UI. 现在,我还在使用Ng2Charts库在UI上构建highcharts I can simply run npm install ng2charts and would be good to go but I want to install these during mvn clean install because I don't want to create an added npm dependency on production. 我可以简单地运行npm install ng2charts ,这很不错,但是我想在mvn clean install npm install ng2charts安装期间安装它们,因为我不想在生产中创建附加的npm依赖项。

Is there a way I can install these libraries using maven? 有什么办法可以使用Maven安装这些库吗?

I tried something line: 我尝试了一些行:

<execution>
    <id>ng2charts</id>
    <goals>
            <goal>npm</goal>
    </goals>
    <configuration>
            <arguments>install</arguments>
    </configuration>
</execution>

But it gives me error saying: 但这给了我一个错误的说法:

[ERROR] [0m[1m[31mERROR in src/app/app.module.ts(48,5): Error during template compile of 'AppModule'[39m[22m[0m
[ERROR] [0m[1m[31m  Could not resolve ng2-charts relative to /Users/user/Documents/workspace/MyPortal/src/frontend/src/app/app.module.ts..[39m[22m[0m
[ERROR] [0m[1m[31msrc/app/chart/chart.component.ts(5,20): error TS2307: Cannot find module 'jquery'.[39m[22m[0m
[ERROR] [0m[1m[31msrc/app/app.module.ts(23,30): error TS2307: Cannot find module 'ng2-charts'.[39m[22m[0m
[ERROR] [0m[1m[31m[39m[22m[0m.............

Please guide. 请指导。

After a lot of research, i figured out a way to include external dependencies in Angular. 经过大量研究,我找到了一种在Angular中包含外部依赖项的方法。 We just need to update package.json file: 我们只需要更新package.json文件:

"dependencies": {
  "ng2-charts": "^1.1.0",
  "jquery": "^3.3.1"
}

This loads the libraries. 这将加载库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM