2016-07-22 89 views
18

我创建了一个Angular 2前端Application.and我创建了一个连接到数据库的Java Rest WS后端应用程序。如何集成Angular 2 + Java Maven Web应用程序

我对角2 app文件夹结构如下─

  • Angular2App
    • CONFG
    • DIST
    • E2E
    • node_modules
    • 公共
    • SRC
      • 应用
      • 的favicon.ico
      • 的index.html
      • main.ts
      • 系统config.ts
      • tsconfig.json
      • typings.d.ts
    • tmp
    • 型号
    • .editorconfig
    • 的.gitignore
    • 角cli.json
    • 角-CLI-build.js
    • 的package.json
    • README.md
    • tslint.json
    • 分型.json

而且我的Maven的Java Web应用程序的结构如下─

  • JerseyWebApp
    • SRC
      • 主要
      • 的Java
        • 定制包
        • java类
      • 资源
      • 的webapp
        • WEB-INF
        • 的web.xml
        • 指数。HTML
    • 的pom.xml

我想知道如何整合这两个应用到一个应用程序,这将产生只有一个war文件。

+0

我建立我的应用程序使用弹簧引导,我们为angular2罐。对于示例请参阅https://github.com/Kiran-N/springboot-and-angular2 – Developer

+0

有类似的任务,这里是我的方法http://stackoverflow.com/questions/37512154/configure-angular-2-project-with -maven-and-spring-rest/42283956#42283956 –

回答

1

我建议让两个应用程序分开,这样你就具有模块化。 通过这种方式,您可以在不影响您的服务的情况下更改Angular App,反之亦然。其次,你的apache/nginx能够更快地将你的js和html从Angular转换为Tomcat(例如)。但是如果你仍然想将Angular应用放入战争中,那么模式就是所有的网络资源都在src/main/webapp中。

+0

这样我试着把所有的网络资源都放在src/main/webapp下,但是在这里我面临的一个问题就是angular2是建立在typescript上的,所以tomcat服务器无法编译这些文件。并且应用程序停滞不前。 – RAJESHPODDER007

+0

这是正确的..也许这可以帮助你然后:http://stackoverflow.com/a/37398298/2285259 –

5

有趣的是,上周我只做了这个!

使用NetBeans 8.1和Tomcat的Servlet版本8.0.27

角和Java项目的文件结构。

Java Project被称为Foo。角项目是酒吧

Foo (Java Maven Project) 
|__ src 
| |__ main 
| | |__ webapp (This folder contains the entire Angular Project) 
| | | |__ META-INF 
| | | | \__ context.xml 
| | | |__ WEB-INF 
| | | | \__ web.xml 
| | | |__ includes 
| | | | |__ css 
| | | | |__ images 
| | | | \__ js 
| | | | 
| | | | ## Bar project files are located here ## 
| | | | 
| | | |__ app 
| | | | \__ All .ts and compiled .js files are located here 
| | | |__ node_modules 
| | | | \__ any dependencies used for Bar are located here 
| | | |__ typings 
| | | | \__ typings for Typescript located here 
| | | | 
| | | |__ README.txt 
| | | |__ index.jsp 
| | | |__ package.json 
| | | |__ systemjs.config.js 
| | | |__ tsconfig.json 
| | | |__ typings.json 
| | | \ ## Bar project files end here 
| | | 
| | |__ resources 
| | | |__META-INF 
| | | | \__ persistence.xml 
| | |__ java 
| | | |__ hibernate.cfg.xml 
| | | |__ com 
| | | | |__ orgName 
| | | | | |__ packageName 
| | | | | | \__ .java files are here 
|__ pom.xml 
\__ nb-configuration.xml 
28

这里是我所做的: -

  • 安装V6.9的NodeJS +
  • 运行NPM安装@角/ CLI -g对角CLI
  • 安装Apache Maven或使用任何Maven友好的IDE
  • 使用您所需的Maven配置,我使用了简单的webapp(WAR)。

指南Stucture(除ngapp夹其余部分是标准的Maven结构。)

ngfirst 
├── pom.xml 
├── src 
│   └── main 
│    ├── java 
│    ├── resources 
│    ├── webapp 
│    └── ngapp 

角部位

打开ngapp在终端文件夹并键入纳克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 

这种结构是Maven项目结构的角度相当于和SRC目录是角应用程序的源代码,就像maven build命令在目标文件夹中产生其输出,ng build命令在dist文件夹中产生其输出。

为了封装内的Maven所产生的角应用程序生成WAR修改构建配置以从DIST更改输出文件夹web应用,开放角cli.json文件并修改其OUTDIR如下: -

"outDir": "../webapp/ng" 

此时纳克建立命令会产生内建成纳克目录 ngfirst/src目录的角应用/主/ webapp文件夹。

Maven的部分

开放的pom.xml,并配置以下三个Maven插件: -

  1. 编译器插件:没有Java的东西来编译/ src目录/主/ ngapp文件夹,排除它。
  2. war-plugin:/ src/main/ngapp是Angular项目文件夹,它不应该打包成WAR,排除它。
  3. exec-plugin:执行NPM Install和Angular-CLI构建命令以在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项目(与角应用程序也是如此)

打开终端,在项目的根文件夹ngfirst和运行MVN包命令,这将生成一个WAR文件(ngfirst.war)目标文件夹

部署ngfirst.war在一个容器中,在浏览器中打开http://localhost:8080/ngfirst/ng/index.html。 (如果需要,调整您的主机名和端口)

如果一切正常,您应该看到应用程序的作品!在浏览器中,这是在工作中的角度应用程序!

JSP预加工

我们可以利用与角应用JSP技术的动态配置和页面渲染能力,角SPA是由Java容器作为普通的HTML网页中投放,的index.html在这个如果我们配置JSP Engine来预处理HTML文件,那么所有的JSP魔术都可以包含在Angular SPA页面中,只需在网页内包含以下内容即可。XML

<servlet-mapping> 
    <servlet-name>jsp</servlet-name> 
    <url-pattern>*.html</url-pattern> 
</servlet-mapping> 

保存,重建Maven项目,部署WAR瞧!

+1

我建议使用' ngapp/**'排除ngapp文件夹或'packagingExcludes>' – belgoros

+0

@J_Dev这可以正常工作。问题是jar包含Node模块also.how我只能包含'ng serve --prod'或'ng build'输出吗? – gihan

+0

@J_Dev - 你能解释一下为什么你不能将ngapp排除在战争之外。我认为这个问题是关于在同一场战争中整合/捆绑角色2应用程序和Java代码。如果你排除它,那么角码如何得到部署? – user911

5

我的身边,我有角源的行家模块调用PRJ棱角,并anoter一个战争应用程序调用PRJ战

第一PRJ角建:

  • 它使用Maven的EXEC-插件调用npm installng build
  • 更改资源默认目录dist/
  • 跳跃(感谢@J_Dev!)瓶子MANIFEST代
  • maven模块结果:产生角的瓶子dist/仅限内容!

然后,第二prj_war是构建:

  • 具有PRJ角作为依赖性
  • 使用解压阶段解压缩之前的罐子到web应用程序的目的地
  • 这个模块构建你与新鲜角度的应用程序战争。

相应插件配置下跟随我使用:

PRJ角(pom.xml的提取物)

<build> 
    <resources> 
     <resource> 
      <directory>dist</directory> 
     </resource> 
    </resources> 
    <plugins> 
     <!-- skip compile --> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>default-compile</id> 
        <phase /> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>exec-npm-install</id> 
        <phase>generate-sources</phase> 
        <configuration> 
         <workingDirectory>${project.basedir}</workingDirectory> 
         <executable>npm.cmd</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</workingDirectory> 
         <executable>ng.cmd</executable> 
         <arguments> 
          <argument>build</argument> 
          <argument>--no-progress</argument> 
          <argument>--base-href=/app/ng/</argument> <== to update 
         </arguments> 
        </configuration> 
        <goals> 
         <goal>exec</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <configuration> 
       <archive> 
        <addMavenDescriptor>false</addMavenDescriptor> 
        <manifest> 
         <addClasspath>false</addClasspath> 
        </manifest> 
       </archive> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
        <configuration> 
         <filters> 
          <filter> 
           <artifact>*:*</artifact> 
           <excludes> 
            <exclude>META-INF/</exclude> 
           </excludes> 
          </filter> 
         </filters> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

PRJ战争(pom.xml的提取物)

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <executions> 
       <execution> 
        <id>unpack angular distribution</id> 
        <phase>generate-resources</phase> 
        <goals> 
         <goal>unpack</goal> 
        </goals> 
        <configuration> 
         <artifactItems> 
          <artifactItem> 
           <groupId>com.myapp</groupId> <== to update 
           <artifactId>prj-angular</artifactId> <== to update 
           <overWrite>true</overWrite> 
           <includes>**/*</includes> 
          </artifactItem> 
         </artifactItems> 
         <outputDirectory>${project.build.directory}/prjwar/ng</outputDirectory> <== to update 
         <overWriteReleases>true</overWriteReleases> 
         <overWriteSnapshots>true</overWriteSnapshots> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin>