I have created a Angular 2 front-end Application.and i have created one Java Rest WS Back-end Application which is connected to DB.
My Folder structure for Angular 2 App is below-
- Angular2App
- confg
- dist
- e2e
- node_modules
- public
- src
- app
- favicon.ico
- index.html
- main.ts
- system-config.ts
- tsconfig.json
- typings.d.ts
- tmp
- typings
- .editorconfig
- .gitignore
- angular-cli.json
- angular-cli-build.js
- package.json
- README.md
- tslint.json
- typings.json
And My Java Maven Web Application Structure is below-
- JerseyWebApp
- src
- main
- java
- Custom Package
- java classes
- resources
- webapp
- WEB-INF
- web.xml
- index.html
- pom.xml
- src
I want to know how to integrate these two application into one application which will produce only one war file.
My side I have a maven module for angular sources called prj-angular, and anoter one for war application called prj-war.
first prj angular is built:
npm install
andng build
(thanks to @J_Dev!)dist/
then, second prj_war is build:
Follow under the corresponding plugin configuration I used:
prj angular (pom.xml extract)
prj war (pom.xml extract)
Here is what I did:-
Directory Stucture (Except for ngapp folder rest is standard Maven structure.)
Angular Part
Open ngapp folder in terminal and type ng init command to initialize node and npm configuration, the result will be a simple Angular2 example application will the following directory structure inside ngapp folder:-
This structure is Angular equivalent of Maven project structure and src directory is Angular Application's source, just like maven build command generates its output in target folder, ng build command generates its output in dist folder.
In order to package the generated Angular application within Maven generated WAR modify the build configuration to change the output folder from dist to webapp, open angular-cli.json file and modify its outDir as below:-
At this point ng build command will generate built Angular Application inside ng directory of ngfirst/src/main/webapp folder.
Maven Part
Open pom.xml and configure following three maven plugins:-
Here is how it should look like:-
Building Maven Project (and Angular App too)
Open Terminal in project root folder ngfirst and run mvn package command, this will generate a WAR file (ngfirst.war) in target folder.
Deploy ngfirst.war in a container, open http://localhost:8080/ngfirst/ng/index.html in Browser. (adjust your hostname and port if required)
If everything went right, you should see app works! in browser, that is Angular Application at work!!
JSP Pre-Processing
We can leverage dynamic configuration and page rendering capabilities of JSP technology with Angular application, Angular SPA is served by the Java container as regular HTML page, index.html in this case, if we configure JSP Engine to pre-process html files too, then all JSP magic can be included inside Angular SPA Page, just include the following inside web.xml
Save, rebuild maven project, deploy WAR and voila!!
I recommend let the two applications separated, this way you have modularity. This way you can change the Angular App without affect your service, and vice versa. For second, your apache/nginx are faster to deliver your js and html from Angular instead Tomcat (for example). But if you still want put the Angular application inside the war, the pattern is that all web resources are in src/main/webapp.
Funnily enough, I did just this last week!
Angular and Java project file structure.
Java Project is called Foo. Angular Project is Bar