在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):FraktonDevelopers/spring-boot-angular-maven-build开源软件地址(OpenSource Url):https://github.com/FraktonDevelopers/spring-boot-angular-maven-build开源编程语言(OpenSource Language):HTML 70.0%开源软件介绍(OpenSource Introduction):spring-boot-angular-maven-buildArchitecture Overview / PrerequisitesJava 8 - Download and install the JDK 8. In the example that I’m detailing below, we used Spring Boot 2.2.1 RELEASE version. Define project modulesThe first step of the implementation is to create a multi-module Spring Boot application. Initially we create a parent module spring-boot-angular-maven-build which will contain the backend module and the frontend module. Assuming that you created the backend and frontend projects in the specified modules.
Then, we need to create the modules using Maven Build Tool. We add the modules in the main pom.xml
Also here we have to specify, the packaging to serve as a container for our sub-modules
Implementing the back-end sideIn the backend module we implement the parent section
Next, we implement the Maven Resources Plugin. This plugin is used to execute our generated frontend module. In the output directory section we select the project build directory and the resources from our dist/frakton generated folder. In the snippet below we have added also some plugins such as:
After implementing the plugins section, we need to include the frontend dependency. This will make sure the frontend is included in the final executable JAR.
Implement front-end sideNext, we need to implement the pom.xml in frontend by adding the parent section:
To execute some of npm commands we need the frontend-maven-plugin. This plugin comes with a set of built-in commands which we can use for triggering npm commands
In the configuration tag, we implement the working directory and we select the Node and Npm versions. Also, in the build section we need to define the resources by specifying the directory. The dist/frakton folder is used to be placed inside the build output.
Testing back-end and front-endAfter completing the configurations steps, we make sure our instances are working correctly before the build. First we can run the backend project by using: mvn spring-boot:run *Also make sure you execute the command inside the backend module. Next. we can start our Angular project using: ng serve Build projectIf everything seems to work correctly we can build the project using: mvn clean install Make sure you are executing the command in the spring-boot-angular-maven-build parent module After building the application, in the backend is generated the target/ folder which contains the jar: backend-0.0.1-SNAPSHOT.jar And in the frontend is generated the dist/ folder and node_modules. If you want to run the executable JAR, open terminal and add: java -jar backend-0.0.1-SNAPSHOT.jar Allow Angular to handle routesAfter running the JAR file, when accessing https://localhost:8080 we will have a Whitelabel Error Page. This happens because Angular by default all paths are supported and accessible but with the usage of Maven, Spring Boot tries to manage paths by itself. To fix this, we need to add some configurations. Create a package config/ and create WebConfig.java. This class has to implement WebMvcConfigurer and to use ResourceHandlers.
The /** pattern is matched by AntPathMatcher to directories in the path, so the configuration will be applied to our project routes. Also the PathResourceResolver will try to find any resource under the location given, so all the requests that are not handled by Spring Boot will be redirected to static/index.html giving access to Angular to manage them. Next, we just build our project and generate the JAR file. Once the application has started, we will be able to see the welcome page. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论