2016-07-22 87 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>