Spring boot run Angular2
I have a REST app as Spring Boot app and client as Angular2.
I used to run applications separately - Spring Booted by running the main SpringBootApplication method (it started listening on port 8080), Angular2 - runs from the command line npm start
(it usually runs on 3000 ports).
Now I want Maven to launch both applications with one command.
- Angular 4.0.0
- Spring Boot 1.5.4.RELEASE
I am trying to apply answer and this tutorial . But in the first case, only Angular application starts, the second - only REST.
My package.json:
"scripts": {
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"serve": "lite-server -c=bs-config.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve --proxy-config proxy-config.json\" ",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"license": "ISC",
"dependencies": {
"@angular/animations": "^4.2.4",
"@angular/common": "~4.0.0",
"@angular/compiler": "~4.0.0",
"@angular/core": "~4.0.0",
"@angular/forms": "~4.0.0",
"@angular/http": "~4.0.0",
"@angular/material": "^2.0.0-beta.7",
"@angular/platform-browser": "~4.0.0",
"@angular/platform-browser-dynamic": "~4.0.0",
"@angular/router": "~4.0.0",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.26",
"angular-in-memory-web-api": "~0.3.0",
"angular2-modal": "^3.0.1",
"bootstrap": "^3.3.6",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"jsplumb": "^2.4.3",
"ng2-bs3-modal": "^0.10.4",
"ng2-popup": "^0.4.0",
"rxjs": "5.0.1",
"systemjs": "0.19.40",
"zone.js": "^0.8.4"
},
"devDependencies": {
"concurrently": "^3.2.0",
"lite-server": "^2.2.2",
"typescript": "~2.1.0",
"canonical-path": "0.0.2",
"tslint": "^3.15.1",
"rimraf": "^2.5.4",
"@types/node": "^6.0.46"
},
"repository": {}
source to share
I agree with sinedsem that for dev I would like both apps to be separate. as far as production build management goes, I would do it a little differently. I don't like the idea of ​​doing any manual build-related actions like copying build files from angular to the source of the spring boot app.
Instead, I could manage the whole build under maven with an angular ui and spring bootstrapping app built as separate modules. The construct will be entirely maven driven, delegating angular ui build to npm / gulp etc. using frontend-maven-plugin . This can be added as a dependency of the spring boot application, the build output can be copied to the target directory of the spring boot module.
Parent POM
Create a new parent POM
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example.angularboot</groupId>
<artifactId>boot-ui-parent</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<modules>
<module>spring-boot-app</module>
<module>angular-ui-resources</module>
</modules>
Angular UI Module
Create an angular UI module using the frontend-maven-plugin to do any npm / bower / gulp etc. build steps. Either customize your build output to target/classes/static
, or copy your angular built resources totarget/classes/static
eg. The following plugin installs node / npm and runs the necessary npm bower and gulp steps to create an angular app.
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<nodeVersion>v6.10.3</nodeVersion>
<npmVersion>4.6.1</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
</execution>
<execution>
<id>bower install</id>
<goals>
<goal>bower</goal>
</goals>
<configuration>
<arguments>install --allow-root</arguments>
</configuration>
</execution>
<execution>
<id>gulp build</id>
<goals>
<goal>gulp</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
Spring Boop Application Module
It should be the same as the current spring boot module with a new parent and dependency for the angular-ui module added.
Since the inline version of the angular UI was packaged in target / classes / static and the angular-ui jar is in the spring boot classpath, the angular ui will be packaged in the spring boot app.
Alternatively you can run copy maven plugin resources to copy resources from angular-ui module build directory to create spring-boot module directory. although I don't really like copying resource files from one module to another for example
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-angular-components</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/static</outputDirectory>
<resources>
<resource>
<directory>${basedir}/../angular-ui/target/classes/static</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
source to share
Your .json package is being used by npm, which is not the main tool here.
I recommend you:
- For development, keep running the two applications separately.
- For production:
- Package your angular app as a bundle. For example using webpack .
- Place your package in your spring boot project under / src / main / resources / static.
- Start spring boot app which will serve your angular app from static resources.
source to share
The solution suggested by tprebs makes sense, but the two maven modules are tightly coupled (since the angular module lays files back in the spring-boot module, which is not good practice).
Keep things just silly, if it's one team that develops the front and back ends, one maven module is enough. For example:
Single Module Modular Approach (KISS)
Assuming spring boot source is in folder src/main/java
and angular is in folder src/main/js
.
src / main / js / angular-cli.json file:
Serve as an angular application like spring load static resources.
{
[...]
"apps": [
{
"root": "src",
"outDir": "../resources/static",
[...]
},
[...]
]
}
pom.xml:
Build angular part from maven using npm
<plugin>
<!-- build UI angular -->
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<workingDirectory>src/main/js</workingDirectory>
<nodeVersion>v6.9.2</nodeVersion>
<npmVersion>4.1.1</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<!-- Optional configuration which provides for running any npm command -->
<configuration>
<workingDirectory>src/main/js</workingDirectory>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm build</id>
<goals>
<goal>npm</goal>
</goals>
<!-- Optional configuration which provides for running any npm command -->
<configuration>
<workingDirectory>src/main/js</workingDirectory>
<arguments>run-script build</arguments>
</configuration>
</execution>
</executions>
</plugin>
On the other hand, if the commands are different, I would choose two separate maven modules, but to limit the communication between these two modules, I will post the angular part as a webjar (see https://spring.io/guides/gs/ spring-boot-cli-and-js ).
source to share