这是我所做的:-
- 安装 Nodejs v6.9+
- 为 Angular CLI 运行 npm install @angular/cli –g
- 安装 Apache Maven 或使用任何 Maven 友好的 IDE
- 使用您所需的 Maven 配置,我使用了简单的 webapp (WAR)。
目录结构(除了 ngapp 文件夹其余部分是标准 Maven 结构。)
ngfirst
├── pom.xml
├── src
│ └── main
│ ├── java
│ ├── resources
│ ├── webapp
│ └── ngapp
角部分
在终端打开 ngapp 文件夹并输入 ng init 命令来初始化节点和 npm 配置,结果将是一个简单的 Angular2 示例应用程序,在 ngapp 内的目录结构如下文件夹:-
├── angular-cli.json
├── e2e
├── karma.conf.js
├── node_modules
├── package.json
├── protractor.conf.js
├── README.md
├── tslint.json
├── src
├── app
├── assets
├── environments
├── favicon.ico
├── index.html
├── main.ts
├── polyfills.ts
├── styles.css
├── test.ts
└── tsconfig.json
这个结构是 Angular 等效的 Maven 项目结构,src 目录是 Angular 应用程序的源,就像 maven build 命令在 target 中生成它的输出> 文件夹,ng build 命令在 dist 文件夹中生成其输出。
为了将生成的 Angular 应用程序打包到 Maven 生成的 WAR 中,修改构建配置以将输出文件夹从 dist 更改为 webapp,打开 angular-cli .json 文件并修改其 outDir 如下:-
"outDir": "../webapp/ng"
此时 ng build 命令将在 ngfirst/src/main/webapp 文件夹的 ng 目录中生成已构建的 Angular 应用程序。 p>
Maven 部分
打开pom.xml,配置以下三个maven插件:-
-
compiler-plugin:在 /src/main/ngapp 文件夹中没有要编译的 Java 内容,排除它。
-
war-plugin:/src/main/ngapp 是 Angular 项目文件夹,不应该打包在 WAR 中,排除它。
-
exec-plugin:执行 NPM Install 和 Angular-CLI Build 命令,在 webapp 文件夹中生成 Angular Application 以进行最终打包。注意 --base-href 参数,需要从 webapp 的上下文路径加载 Angular 资源。
它应该是这样的:-
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<excludes>
<exclude>ngapp/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<excludes>
<exclude>ngapp/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>exec-npm-install</id>
<phase>generate-sources</phase>
<configuration>
<workingDirectory>${project.basedir}/src/main/ngapp</workingDirectory>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>exec-npm-ng-build</id>
<phase>generate-sources</phase>
<configuration>
<workingDirectory>${project.basedir}/src/main/ngapp</workingDirectory>
<executable>ng</executable>
<arguments>
<argument>build</argument>
<argument>--base-href=/ngfirst/ng/</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
构建 Maven 项目(以及 Angular 应用程序)
在项目根目录ngfirst打开终端并运行mvn package命令,这将在target中生成一个WAR文件(ngfirst.war)文件夹。
在容器中部署ngfirst.war,在浏览器中打开http://localhost:8080/ngfirst/ng/index.html。 (如果需要,调整您的主机名和端口)
如果一切正常,您应该会在浏览器中看到 应用正常工作!,这就是 Angular 应用程序在工作!
JSP 预处理
我们可以利用 Angular 应用程序的 JSP 技术的动态配置和页面渲染功能,Angular SPA 由 Java 容器作为常规 HTML 页面提供服务,在这种情况下 index.html,如果我们配置 JSP引擎也可以预处理 html 文件,然后所有的 JSP 魔法都可以包含在 Angular SPA 页面中,只需在 web.xml
中包含以下内容
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
保存,重建 maven 项目,部署 WAR,瞧!!