Why aren't all resources being published using WebSphere Liberty Tools for Eclipse? - websphere-liberty

I'm experiencing an issue with running a WebSphere Liberty server from within Eclipse. I'm using Eclipse 2019-09 R (4.13.0), WAS Liberty with Java EE 8 Web Profile (V19.0.0.11), and WebSphere Liberty Server Liberty Tools Eclipse plugin (19.0.900.v20190912_1541). I'm intending to run a Maven web project on the above Liberty server, to no avail.
I downloaded the Liberty runtime to a directory and configured a server within Eclipse to use this runtime. I then added the web project as a resource to the server. The server seems to start up fine, however, when trying to access a resource, I see several java.lang.NoClassDefFoundError exceptions. When using the WebSphere Liberty Server Liberty Tools plugin, instead of publishing the WAR file to the Liberty server, it simply publishes an XML file pointing to the resources and the dependencies that would make up the WAR file. The XML is in the usr/servers/<server_name>/apps directory and has a name like app-name.war.xml. The contents of the XML file are pretty sparse:
<?xml version="1.0" encoding="UTF-8"?>
<archive>
<dir sourceOnDisk="/path/to/web/project/WebContent" targetInArchive="/"/>
<dir sourceOnDisk="/path/to/web/project/target/classes" targetInArchive="/WEB-INF/classes"/>
<dir sourceOnDisk="/path/to/web/project/target/test-classes" targetInArchive="/WEB-INF/classes"/>
</archive>
It seems pretty obvious looking at the above why the server is throwing exceptions because it can't find all any resources. Previous, older versions of Eclipse, the plugin, and Liberty were configured the same way, but the war.xml file contained a reference to all the dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<archive>
<archive targetInArchive="/WEB-INF/lib/lib1.jar">
<dir sourceOnDisk="/path/to/workspace/lib1/target/classes" targetInArchive="/"/>
</archive>
<archive targetInArchive="/WEB-INF/lib/lib2.jar">
<dir sourceOnDisk="/path/to/workspace/lib2/target/classes" targetInArchive="/"/>
</archive>
...
<dir sourceOnDisk="/path/to/web/project/target/m2e-wtp/web-resources" targetInArchive="/"/>
<dir sourceOnDisk="/path/to/web/project/src/main/webapp" targetInArchive="/"/>
<dir sourceOnDisk="/path/to/web/project/target/classes" targetInArchive="/WEB-INF/classes"/>
<dir sourceOnDisk="/path/to/web/project/src/main/resources" targetInArchive="/WEB-INF/classes"/>
<file sourceOnDisk="~/.m2/repository/some-lib1.jar" targetInArchive="/WEB-INF/lib/some-lib1.jar"/>
<file sourceOnDisk="~/.m2/repository/some-lib2.jar" targetInArchive="/WEB-INF/lib/some-lib2.jar"/>
...
</archive>
Does anyone have any idea as to why in this newer version of Liberty and the Liberty Tools plugin I'm not getting all the dependencies published? Thanks for your help.

How did you import your project into Eclipse? I created a simple Maven war project and imported it into Eclipse using the Existing Maven Projects import wizard. I then ran it on a server that I had created and it included the dependency jar that was needed:
<?xml version="1.0" encoding="UTF-8"?>
<archive>
<dir sourceOnDisk="C:\home\work\maven\ServletJsonSample\target\m2e-wtp\web-resources" targetInArchive="/"/>
<dir sourceOnDisk="C:\home\work\maven\ServletJsonSample\src\main\webapp" targetInArchive="/"/>
<dir sourceOnDisk="C:\home\work\maven\ServletJsonSample\target\classes" targetInArchive="/WEB-INF/classes"/>
<file sourceOnDisk="C:\Users\xxx\.m2\repository\org\json\json\20190722\json-20190722.jar" targetInArchive="/WEB-INF/lib/json-20190722.jar"/>
</archive>
You can also try disabling loose applications by double clicking on the server in the servers view to open the server editor, uncheck Run applications directly from the workspace, and save your changes. Or you may have it enabled in your pom.xml, in which case remove the line: <looseApplication>true</looseApplication>.

Related

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.

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

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

Deploying a Maven Spring Boot project on a server offline

I have a situation in hand where I have no internet connectivity on my client's server (because it is an intra-net server) and I have to deploy my Spring Boot application on it which uses Maven build. The thing is, I am not allowed to connect the server to the internet due to security reasons but I am able to access internet on my development machine. So I have two questions:
How do I get all the dependencies on the server in question? Can I somehow copy the downloaded files from my development machine to the server? If so, how?
Do I need to setup Apache Tomcat or should I just run the war file from the command line? What are the best practices?
It's better to make a jar file instead of making a war file. That should help you to run without Tomcat since it embeds a web server.
Dependencies are packed with the jar when you do a Maven build. You need to have an internet connection to build it. Copy the jar to the production environment and run.
Here is striaght forward example to deploy spring boot application on tomcat server
Follow the example and add one more tomcat6/tomcat7 plugin
<!-- Tomcat 6 plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<configuration>
<url>${serverUrl}:${port}/manager</url>
<path>/${contextpath}</path>
<username>${username}</username>
<password>${password}</password>
</configuration>
</plugin>
plugin properties as follows
<properties>
<!-- Server Details -->
<serverUrl>http://kpServer</serverUrl>
<port>8080</port>
<contextpath>kp-cxf</contextpath>
<username>admin</username>
<password></password>
<!-- Skip test execution during default steps -->
<skipTests>true</skipTests>
</properties>
And you can use mvn tomcat6:redeploy command to deploy to your server(in shore maven deownloads plugins/dependencies from internet to your development server, when you run mvn tomcat6:deploy/package it creates war by taking jars from you local maven directory)

Deploying Maven project to JBoss AS7 (standalone)

I am new to JBOSS AS7, and am strictly using MAVEN 3 via command line. I would like to deploy the *.war of my project to JBOSS AS7 without ECLIPSE. I have spent couple of days Googling and trying out difference approaches. I am able to deploy the *.war to Tomcat. I would highly appreciate any help on this. So far this is what I have done:
I have entered the server info(Jboss) to C:\maven\conf\settings.xml
I have included the plugin to the POM
I think I may be declaring the path to JBoss AS7 within my JBOSS plugin wrong.
This is how I am including the plugin in the POM file:
<plugins>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.0.2.Final</version>
<configuration>
<url>http://localhost:8080/
<server>jboss</server>
<path>/web-app</path>
<configuration>
</plugin>
</plugins>
Did you have a look at the README on the corresponding GitHub page:
https://github.com/jbossas/jboss-as-maven-plugin
When deploying to your localhost (which is the default value) you don't need to define the <url> in the <configuration>, as well as the <server> and <path> elements (besides: I haven't seen a folder named wep-app in AS7).
We run a project where this works even without touching the settings.xml.
So when you have checked this, post what exactly goes wrong, what is the concrete failure message and what is the maven command you use (mvn jboss-as:deploy).

how to expose WEB-INF/lib inside war using maven

We are using an 3rd party war in our web app (war). In order to communicate with the war, we have created a bridge module (jar). The intention is to prevent our web app from directly communicating with the external war, but instead communicate through the bridge module.
All the 3 modules (2 wars and 1 jar) are inside an ear file which is deployed in JBoss.
ear
- war1 (our web app)
- war2 (external web app)
- bridge jar
Point to note is, the bridge jar uses some API (exposed as jars), which are present inside the WEB-INF/lib directory of the external war.
At the time of bringing up JBoss, we get java.lang.NoClassDefFoundError errors because the bridge jar is not able to find the API present in external war's WEB-INF/lib.
We do not want to place all external jars directly under ear as it will mean the external jars are not confined only within its war.
Is there a way to access the jars present inside WEB-INF/lib of the external war from the bridge jar? Can we achieve this using maven build process, or is there a better approach to this?
We've had a similar problem recently with our jars not able to see other jars. Resolved it by creating a manifest.mf using the maven-ejb-plugin defined in the pom.xml of the "bridge jar"
2 ways to do this:
a) if bridge jar's pom.xml already has war1 and war2 defined as dependencies - then use maven-ejb-plugin with
<configuration>
<ejbVersion>3.0</ejbVersion>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
This should autogenerate manifest.mf with a Classpath matching all dependencies defined in the pom
b) else, define your own manifest.mf with the right entries you need and point to it like so
<configuration>
<ejbVersion>3.0</ejbVersion>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
Since your jars are in the WEB-INF/lib of the war1, I think you should go for option 2 with a Manifest containing direct entries such as
Class-Path: WEB-INF/lib/some-external.jar

Resources