角度配置
首先,我需要配置 Angular 应用程序,以使 Angular 应用程序的构建可在 Spring Boot Maven 项目的源代码目录中使用。
一个简单的方法需要进行 2 个简单的更改:
那是 :
{
...
"apps": [
{
"root": "src",
"outDir": "../src/main/resources/static",
...
}
默认情况下,Spring Boot 从名为的目录提供静态内容/static
(or /public
or /resources
or /META-INF/resources
)在类路径中或从根目录ServletContext
.
这样,当我执行ng build
命令时,Webpack 捆绑的 Angular 应用程序将位于 Spring Boot 静态内容的资源目录中。
Spring引导配置
我不想在开发和开发后环境中执行完全相同的 Spring Boot 构建任务。
当然,这并不意味着构建将根据目标环境产生独特的功能行为。
开发中,我想要快速构建。
当我修改源代码(Java、JavaScript 或其他任何代码)时,我需要快速反馈。例如,我不想在每次修改源代码时打包和重新部署应用程序。
我也不想每次更改源代码时都做一些特殊的事情。
我希望更改能够快速、自动地反映在应用程序中。
为开发后环境构建应用程序,我想要一个可靠的构建并且包含最终内容。
以下是开发后环境的详细要求:
我想从头开始执行 Angular 应用程序(甚至整个 Spring Boot 应用程序)的完整构建。
在开发过程中,每次修改源代码时对 webpack 捆绑应用程序进行增量更新是没问题的,但是当我交付我的应用程序时,我将真正最大限度地减少潜在的副作用。
我不在乎是否必须从头开始打包应用程序,因为我不会经常创建此构建,并且我还将此任务留给了自动化工具,例如持续集成。
我想通过运行部署它的嵌入式 Tomcat 来以自治的方式重新打包应用程序。
The repackage goalSpring Boot Maven 插件实现了它。
为了能够以干净/可靠的方式从一种构建类型切换到另一种构建类型,我想指定两个不同的构建。
Maven(作为 Gradle)提供了建立档案功能很好地满足了这一要求。
所以,我用它。
以下是开发构建的 Maven 配置文件:
<profile>
<id>fast-build</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
</jvmArguments>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
一些附加说明:
-
在开发中,我希望始终有一种方法可以在需要时添加断点,并且无需重新启动应用程序。
The jvmArguments
Spring Boot Maven 插件的参数按以下方式赋值:
<jvmArguments>
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
</jvmArguments>
指定5005
作为调试器侦听器的端口而不被阻塞(suspend=n
)对于服务器。
- 为了启用静态资源的热刷新,我设置了
addResources
to true
.
以下是开发后构建的 Maven 配置文件(加上它使用的声明属性):
<properties>
...
<node.version>v6.9.1</node.version>
<npm.version>3.10.8</npm.version>
...
</properties>
...
<profile>
<id>full-build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<installDirectory>angular-app</installDirectory>
<workingDirectory>angular-app</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>${node.version}</nodeVersion>
<npmVersion>${npm.version}</npmVersion>
</configuration>
</execution>
<execution>
<id>install angular app</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
一些附加说明:
angular-app
此处声明:<workingDirectory>angular-app</workingDirectory>
是我在Maven Spring Boot项目中复制的角度应用程序的目录。
<installDirectory>angular-app</installDirectory>
是工作文件夹frontend-maven-plugin
将安装节点和所需的依赖项。
就其本身而言,angular-app
目录不直接用于生成打包的应用程序。
因此,使用这个目录就可以了,并且还允许从 Angular CLI 运行 Angular 应用程序。
用于运行构建的 Angular 和 Spring Boot 命令
-
对于开发构建:
- Angular CLI:来自
angular-app
目录,执行ng build -w
。
它将构建 Angular 应用程序并增量更新构建(w
用于手表)
- Spring Boot :从maven项目的基目录中,执行
mvn spring-boot:run -Pfast-build
(欢迎提供执行它的脚本)
-
对于开发后构建:
- 不需要 Angular CLI。
- Spring Boot :从maven项目的基目录中,执行
mvn spring-boot:run