I had a similar requirement to have one source project that has a java web services project, as well as an angular project (angular-cli based project), and the maven assembly should create a war with all the corner files in it. I used maven-frontend-plugin with a few configuration changes for the base path.
The goal was to create a war file with all the Java code as well as the compiled corner code in the root folder of war, all with a single mvn clean package command.
One more thing for this to work in order to avoid a conflict between the URLs of the angular-app router and the URLs of your java application. You need to use HashLocationStrategy. one way to configure it in app.module.ts as shown below
app.module.ts -
providers: [ { provide: LocationStrategy, useClass: HashLocationStrategy }, ]
Folder structure for Angular App - below-
Corner Project
- distance
- e2e
- node_modules
- the public
- CSI
- Appendix
- assets
- Environment
- favicon.ico
- index.html
- main.ts
- polyfills.ts
- style.css
- tsconfig.json
- typings.d.ts
- etc. - etc.
- Tmp
- .angular-cli.json
- .gitignore
- karma.conf.js
- package.json
- README.md
- tslint.json
- etc.
Maven Project -
- CSI
- angular-project ( post your corner project here )
- node_installation
- pom.xml
Add maven-frontend-plugin configuration to pom.xml
<properties> <angular.project.location>angular-project</angular.project.location> <angular.project.nodeinstallation>node_installation</angular.project.nodeinstallation> </properties> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.0</version> <configuration> <workingDirectory>${angular.project.location}</workingDirectory> <installDirectory>${angular.project.nodeinstallation}</installDirectory> </configuration> <executions> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> <configuration> <nodeVersion>v6.10.0</nodeVersion> <npmVersion>3.10.10</npmVersion> </configuration> </execution> <execution> <id>npm install</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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.2</version> <executions> <execution> <id>default-copy-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>true</overwrite> <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory> <resources> <resource> <directory>${project.basedir}/${angular.project.location}/dist</directory> </resource> </resources> </configuration> </execution> </executions> </plugin>
As above, the internal plugin calls 'npm run build', make sure package.json must have the build command in the script, as shown below:
package.json
"scripts": { -----//-----, "build": "ng build --prod", -----//------ }
index.html should always load when someone clicks on an application from a browser, so make it a welcome file. For web services, let's say we have the path / rest-services / *, we will explain this later.
web.xml -
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet-mapping> <servlet-name>restservices</servlet-name> <url-pattern>/restservices/*</url-pattern> </servlet-mapping>
The above configuration is sufficient if your application has no context path and is deployed in the root path on the server. But if your application has any contextual path, for example http: // localhost: 8080 / myapplication /, then make changes to the index.html file too -
angular-project / src / index.html - here document.location will be myapplication / (otherwise the context path of your application / if the application does not have a context path)
The goal of making the context path the base path for angular-app is that whenever you make an ajax http call from angular, it will add the base path to the url. for example, if I try to call 'restservices / people', then it will actually make calls to ' http: // localhost: 8080 / myapplication / restservices / people '
index.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>E2E</title> <script>document.write('<base href="' + document.location + '" />'); </script> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> </head> <body> <app-root></app-root> </body>
After all of the above changes, after running mvn clean package it will create the required war. Check if the entire contents of the corner folder dist is in the root of war file.