SpringBoot整合Angular第二彈:配置支持Angular

使用SpringBoot整合Angular的時候我們需要使用到一個名爲frontend的maven插件,該插件主要用於java項目集成node的項目進行一體化構建部署,也就是說將node的應用部署到java中,使用java容器運行node項目等功能!!!廢話不說直接導上源碼.

  • 修改項目的pom文件增加以下代碼(或者直接使用該代碼覆蓋原有的pom文件代碼)

  
  
  
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  3.    <modelVersion>4.0.0</modelVersion>

  4.    <groupId>com.edurt</groupId>

  5.    <artifactId>springboot-angular-integration</artifactId>

  6.    <packaging>jar</packaging>

  7.    <version>1.0.0</version>

  8.    <name>springboot-angular-integration</name>

  9.    <url>http://maven.apache.org</url>

  10.    <properties>

  11.        <system.java.version>1.8</system.java.version>

  12.        <!-- node & npm -->

  13.        <system.node.version>v8.2.1</system.node.version>

  14.        <system.npm.version>5.4.2</system.npm.version>

  15.        <dependency.springboot.version>1.5.6.RELEASE</dependency.springboot.version>

  16.        <plugin.springboot.version>1.5.9.RELEASE</plugin.springboot.version>

  17.        <!-- plugin -->

  18.        <plugin.frontend.version>0.0.27</plugin.frontend.version>

  19.    </properties>

  20.    <dependencies>

  21.        <dependency>

  22.            <groupId>org.springframework.boot</groupId>

  23.            <artifactId>spring-boot-starter-web</artifactId>

  24.            <version>${dependency.springboot.version}</version>

  25.        </dependency>

  26.        <dependency>

  27.            <groupId>junit</groupId>

  28.            <artifactId>junit</artifactId>

  29.            <version>3.8.1</version>

  30.            <scope>test</scope>

  31.        </dependency>

  32.    </dependencies>

  33.    <build>

  34.        <plugins>

  35.            <plugin>

  36.                <groupId>org.springframework.boot</groupId>

  37.                <artifactId>spring-boot-maven-plugin</artifactId>

  38.                <version>${plugin.springboot.version}</version>

  39.                <configuration>

  40.                    <fork>true</fork>

  41.                </configuration>

  42.            </plugin>

  43.            <!-- Frontend -->

  44.            <plugin>

  45.                <groupId>com.github.eirslett</groupId>

  46.                <artifactId>frontend-maven-plugin</artifactId>

  47.                <version>${plugin.frontend.version}</version>

  48.                <configuration>

  49.                    <!-- angular 源碼根目錄 angular -->

  50.                    <workingDirectory>src/main/angular</workingDirectory>

  51.                    <nodeDownloadRoot>https://npm.taobao.org/mirrors/node/</nodeDownloadRoot>

  52.                    <nodeVersion>${system.node.version}</nodeVersion>

  53.                    <npmVersion>${system.npm.version}</npmVersion>

  54.                    <installDirectory>target</installDirectory>

  55.                </configuration>

  56.                <executions>

  57.                    <execution>

  58.                        <id>install node and npm</id>

  59.                        <goals>

  60.                            <goal>install-node-and-npm</goal>

  61.                        </goals>

  62.                        <phase>generate-resources</phase>

  63.                    </execution>

  64.                    <execution>

  65.                        <id>npm install</id>

  66.                        <goals>

  67.                            <goal>npm</goal>

  68.                        </goals>

  69.                        <configuration>

  70.                            <arguments>install</arguments>

  71.                            <installDirectory>target</installDirectory>

  72.                        </configuration>

  73.                    </execution>

  74.                    <execution>

  75.                        <id>angular cli build</id>

  76.                        <goals>

  77.                            <goal>npm</goal>

  78.                        </goals>

  79.                        <phase>generate-resources</phase>

  80.                        <configuration>

  81.                            <arguments>run build</arguments>

  82.                        </configuration>

  83.                    </execution>

  84.                </executions>

  85.            </plugin>

  86.        </plugins>

  87.    </build>

  88. </project>

我們在 frontend配置項中配置了部分需要的功能,詳細的配置信息大家可以到 frontend插件官網中進行查看

  • 在pom文件中配置完成後,我們需要修改 .angular-cli.json文件進行設置angular項目的編譯輸出配置

  
  
  
  1. "outDir": "../resources/static",

注意 outDir輸出目錄只能是java的resources目錄下(可以是public/static/或者是其他自定義目錄,自定義目錄的話需要用戶自己去寫自定義目錄配置)


本文分享自微信公衆號 - Spring中文網(china-spring-all)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章