How to make a WAR file from angular 2 (angular-cli) project? - maven

I want to make a war file to deploy the angular2 project in an apache tomcat server. I made a maven project and inserted the angular2 project inside it. Then I made the webapp folder(instead of the dist folder in the angular2 project) in the src/main in the maven project using angular-cli. When I run the apache server it shows the following errors.
Error loading http://localhost:8080/vendor/angularfire2/angularfire2.js as "angularfire2" from http://localhost:8080/app/app.module.js ; Zone: ; Task: Promise.then ; Value: Error: Error: XHR error (404 Not Found) loading http://localhost:8080/traceur(…) null
This looks like the troublesome dependency is the angularfire2. How to figure this our? Btw, I use angular2 rc-5.

I wanted to post a complete answer to this question since there are lots of views to this question.
The answer works for all angular 2+ versions.
The procedure is as follows.
First you need to create a POM file in your project's root directory. Include the following code into the 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 http://maven.apache.org/POM/4.0.0 ">
<modelVersion>4.0.0</modelVersion>
<groupId>it.your-company</groupId>
<artifactId>your-project-artifact-id</artifactId>
<version>1.0.0</version>
<name>your-project-name</name>
<description>Any description</description>
<packaging>war</packaging>
<build>
<finalName>target-file-name</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>dist</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/${project.build.finalName}</path>
<update>true</update>
<url>http://localhost:8080/manager/text</url>
<username>tomcat</username>
<password>tomcat321</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here, I have included the maven war plugin to build the war file as well as the maven tomcat plugin to run the war using IntelliJ idea.
Then you need to change the base URL of your index.html file as base href="/target-file-name".
If you are running the war using maven tomcat plugin, the URL for your app would be http://localhost:8080/target-file-name
Now build your angular project using ng build --prod. This will create all the required deployment files (build files) in the dist folder.
Now run mvn clean package to package your build files to a war file. The war file will be created inside the target folder from your root directory of your project.
(Optional) You may also run the war file using maven tomcat plugin too.

If you want to deploy locally. Say specifically at localhost:8080(Tomcat) , go to the service.msc and start tomcat Services. Build your angular 2 /angular 4 using (ng build). Now open the angular project folder and copy the files inside the dist folder to a new folder say(webui). Open index.html page and give as . Copy this folder to "C:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps". Go to browser and type localhost:8080/webui.
This is how i deployed my angular 4 static content in tomcat.
Hope this helps you.

In your index.html set base href to "" or in your case (tomcat) to "webapps" that works for me

Related

Vaadin 8 + AudioVideo Addon: "Failed to load the widgetset"

I added the AudioVideo Addon to my Vaadin 8 (version 8.7.2, Maven 3, Tomcat) project and everything woks fine in my Eclipse development environment (I did run Maven clean install). But if I export the WAR file to the deployment server the app fails to start with the following error:
Failed to load the widgetset: ./VAADIN/widgetsets/AppWidgetset/AppWidgetset.nocache.js?1556793473728
I found the following in my Maven POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Exclude an unnecessary file generated by the GWT compiler. -->
<packagingExcludes>WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
</configuration>
</plugin>
I think I need to change something in the Maven config. I tried the "packagingIncludes" instead of "packagingExcludes" but this did not help.
I would be glad for your help since I cannot do "experiments" on the deployment server.
How do I have to configure Maven or what else do I have to do to get the following folder from the eclipse depolyment environment into my exported WAR file?
myApp\target\myApp-0.0.1-SNAPSHOT\WEB-INF\classes\VAADIN\widgetsets\AppWidgetset

IntelliJ IDEA - Run/debug doesn't copy resources

I have a maven project which is packaged as WAR. The POM looks something like the following:
<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/maven-v4_0_0.xsd">
<artifactId>my_app</artifactId>
<packaging>war</packaging>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
...
</plugin>
...
</plugins>
</build>
</project>
When I manually run the command mvn package, a WAR is generated with directory target/my_app where my web resources (JSP, CSS, JS, images, etc.) are copied to. The generated WAR, if uploaded to an Application Server, works fine.
However, if I use IntelliJ IDEA's Run/Debug configuration (which is configured to deploy the WAR to an Application Server (JBoss if that matters)) to do the same, I don't see the web resources. Only configuration XMLs and compiled classes is present in the target/my_app directory. The applicaiton server starts up all fine without any errors and WAR is deployed successfully, however I can't accesss it as it returns 404.
How can I acheive what I get when building and manually uploading the WAR using Run/Debug of IntelliJ IDEA?
UPDATE:
Before launch tasks are:
Build
Build 'my_app:war exploded' artifact
I had the same issue after installing 2018.1 IntelliJ IDEA. Reverting back to version 2017.3 solved the problem. It looks this is a bug with the new version since I had the same settings for both. Hope this helped.
I had similar issue in which when I was running from the command line then it was copying all the resources (example JSP etc.) but Intellij was not copying it.
As part of workaround I added the goal of 'war:exploded' along with 'compile' then it resolved my issue.
So in Maven config command line parameters will be (may differ for you):
clean compile war:exploded
Screenshot for reference.

What is the best place for JavaDoc files in a Maven project using Tomcat?

I am regularly deploying a Maven project to a Tomcat server, using Travis CI. My project is a web app, so I have configured my pom.xml for building a WAR file, instead of a JAR:
...
<packaging>war</packaging>
...
With Maven, I can generate a directory containing all the JavaDoc files for my project; Maven puts them in the target/site/apidocs directory. But then, when I deploy my project, Travis doesn't perform any mvn site phase so I don't have my JavaDocs on the server.
Should I edit my pom.xml so that Maven puts the JavaDoc files somewhere in the src directory (instead of target) or is there a way to package the JavaDoc files together with the WAR file? I thought that I could create a docs/ directory inside src/main/webapp/. Specifically: is it "good practice" to generate my JavaDoc in src instead of target? if not, how can I have a WAR file containing my JavaDoc?
What would you suggest is the best thing to do?
I already know how to generate a standalone JAR containing my JavaDoc files (see here), but this is not what I'm looking for.
Use the site plugin https://maven.apache.org/plugins/maven-site-plugin/ and the javdoc plugin https://maven.apache.org/plugins/maven-javadoc-plugin/usage.html.
Add the following to your pom.xml
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<links>
<link>http://commons.apache.org/lang/api</link>
<link>http://java.sun.com/j2se/1.5.0/docs/api</link>
<link>http://this-one-will-not-work</link>
</links>
</configuration>
</plugin>
</plugins>
</reporting>
then mvn site:site your documentation will be in target/site you can also deploy it.

Wildfly Deploy Maven - Remove Version

I want to deploy a war that I have created using maven to wildfly using the wildfly-maven-plugin.
The final name of the war is something like: my-war-1.0.war
The war also contains a jboss-web.xml specifying the context root (e.g. /my-war)
Problem Description
If I now deploy the war to wildfly I will get a "my-war-1.0.war" deployment.
If I later want to deploy a new version (e.g. the war is now named my-war-1.1.war) I get a conflict as the context root is already known but the deployment has a new name.
Is there a way using the wildfly-maven-plugin to deploy a "my-war.war" instead?
I need to keep the original final build name inside the maven build for versioning and deploying to our nexus.
The simplest solution is to use the <finalName/> element on the <build/> configuration.
<build>
<finalName>${project.artifactId}</finalName>
</build>
You can use the maven war plugin to rename the final war. For Eg:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>my-war</warName>
</configuration>
</plugin>
</plugins>
</build>
This will always generate the war with the name my-war.war in your "target" directory.
I found out that I can use the parameters <name/> and <runtimeName/> inside the <configuration/> of the maven-wildfly-plugin.
That way I can specify what the deployment should be called on the server and each time just replace it. It is important to have the two parameters end in ".war", otherwise you will get a 404 error.
Using this method I can keep the original name of the final build result containing the version (my-app-1.0.war) and archive it inside our internal nexus repository.

Deploy Maven project with integrated Tomcat

I've researched many resources how to deploy Maven project to jar file and then to tomcat. So my question: is it possible to integrate tomcat server to my maven project and then deploy it to jar file, because I want to launch my web application just by double clicking on the jar file.
You can use Apache Tomcat Maven Plugin. Add to your pom.xml the following lines:
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
Source: http://tomcat.apache.org/maven-plugin-2.2/
After that you can start your web application via command line:
mvn tomcat7:start
I want to launch my web application just by double clicking on the jar file.
This solution doesn't allow you to run web application just by double-clicking on packaged Java archive, but I provide you with common approach in Java web-development and recommend it.

Resources