【问题标题】:Configure angular 2 project with maven and spring REST使用 Maven 和 Spring REST 配置 Angular 2 项目
【发布时间】:2016-09-27 11:39:53
【问题描述】:

我想将我的小型应用程序从 angular 1 升级到 angular 2。我对 angular 2 和节点配置有些陌生。我的 web 应用程序使用 eclipse 和 maven。问题是我无法使用 angular 2 进行配置。

我应该使用什么目录结构?

有可用的 maven 插件,但我对我的 angular 2 应用程序的目录结构有点困惑。

【问题讨论】:

  • 我目前正在做同样的事情,但看看这些答案stackoverflow.com/questions/35704973/…
  • 如果你没有在这里运行打字稿是一个很好的答案,我使用的那个stackoverflow.com/questions/35890887/…
  • 谢谢查尔斯。我关注了这些链接,但我仍然不清楚目录结构和编译。如果有任何解释更多的链接将非常有帮助。
  • 编译?您能否更具体地说明您计划使用的内容,例如哪个服务器、哪些插件?您是否使用普通的旧 javascript 或 typescript 来编写您的 Angular 2 代码?
  • 我正在使用打字稿。还计划使用 maven 插件。

标签: node.js eclipse spring maven angular


【解决方案1】:

这是我所做的:-

  • 安装 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 应用程序的源,就像 ma​​ven 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插件:-

  1. compiler-plugin:在 /src/main/ngapp 文件夹中没有要编译的 Java 内容,排除它。
  2. war-plugin:/src/main/ngapp 是 Angular 项目文件夹,不应该打包在 WAR 中,排除它。
  3. 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,瞧!!

【讨论】:

  • 如果您在项目上方有 github 链接..请发给我们..它会帮助我们...@J_Dev
  • 你好,开发者,你能发布github项目链接吗?
【解决方案2】:

Here from the angular site 展示了如何构建 angular 2 项目的几个演示。在 Eclipse Maven Web 应用程序中,我会将客户端文件启动到与 web-inf 文件夹相同级别的 src/main/resources 文件夹中。

Maven pom.xml,将其包含到您的项目中。 Maven-fronted-plugin 不应该用于生产。 Maven 将使用此设置在项目根级别安装两个文件夹,node 和 node_modules。

<build>
   <plugins>    
        <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <configuration>
                            <nodeVersion>v5.3.0</nodeVersion>
                            <npmVersion>3.3.12</npmVersion>
                        </configuration>
                    </execution>

                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <!-- Optional configuration which provides for running any npm command -->
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
</build>

package.json 在 maven clean install 之前将它添加到项目的根目录。

{
  "name": "budget_calculator",
  "version": "1.0.0",
  "dependencies": {
    "@angular/common":  "2.0.0-rc.1",
    "@angular/compiler":  "2.0.0-rc.1",
    "@angular/core":  "2.0.0-rc.1",
    "@angular/http":  "2.0.0-rc.1",
    "@angular/platform-browser":  "2.0.0-rc.1",
    "@angular/platform-browser-dynamic":  "2.0.0-rc.1",
    "@angular/router":  "2.0.0-rc.1",
    "@angular/router-deprecated":  "2.0.0-rc.1",
    "@angular/upgrade":  "2.0.0-rc.1",
    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.10",
    "bootstrap": "^3.3.6"
  }
}

【讨论】:

  • 嗨,这适用于 webpack 和 angualar 2 最终版本吗?谢谢。
猜你喜欢
  • 2016-01-22
  • 2017-03-19
  • 2013-07-10
  • 2018-04-08
  • 2012-08-06
  • 2018-02-11
  • 1970-01-01
  • 2013-01-17
  • 2017-07-24
相关资源
最近更新 更多