Spring Boot - Convert Jar Packaging to War Packaging - java

I'm working on a web application build with the Spring Boot-Framework... Comming to end, I wanted to start my application from terminal with
java -jar application.jar
Then I identify that jar-files can not handle JSP-Files. With this mind I edited my POM-file from Maven like this here:
<project>
....
<groupId>com.test.test</groupId>
<artifactId>Application</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
....
</project>
When building the application this error-statement is shown:
------------------------------------------------------------------------ Building Testbackend 0.1.0
------------------------------------------------------------------------ Downloading:
file://C://mod_repository/org/apache/maven/plugins/maven-war-plugin/2.6/maven-war-plugin-2.6.pom
Exception in thread "pool-1-thread-1" java.lang.NoClassDefFoundError:
org/apache/commons/lang/StringUtils at
org.apache.maven.wagon.providers.file.FileWagon.resolveDestinationPath(FileWagon.java:206)
at
org.apache.maven.wagon.providers.file.FileWagon.resourceExists(FileWagon.java:265)
at
org.sonatype.aether.connector.wagon.WagonRepositoryConnector$GetTask.run(WagonRepositoryConnector.java:577)
at
org.sonatype.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:60)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745) Caused by:
java.lang.ClassNotFoundException: org.apache.commons.lang.StringUtils
at
org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at
org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
at
org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
... 7 more
Do someone know this error-statement in combination with war-files?

I don't have enough points to comment but here's a resource that I think will help: https://spring.io/guides/gs/convert-jar-to-war/
Select the "Packaging executable jar and war files with Maven" link. I hope this points you in the right direction.

I did something like this for a personal project. You need to do some changes:
Add some dependencies to pom:
groupId: org.springframework.boot
artifactId: spring-boot-starter-web
groupId: javax.servlet
artifactId: jstl
Change the package jar to war: <packaging>war</packaging>
Extend from SpringBootServletInitializer your 'SpringBootWebApplication' class.
Create the following folders structure to add the *.jsp files : src/main/webapp/WEB-INF/jsp/
In /src/main/resources/static/ folder you can add the *.css files.
In application.properties file add the following:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
I used the following references:
Spring Boot example JSP
Convert JAR to WAR

Related

jersey - ClassNotFoundException org.glassfish.jersey.servlet.ServletContainer

When I deploy my rest api with jersey to a tomcat 7.0 server I get an error that has been discussed on stackoverflow quite often but I haven't been able to resolve it:
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1892)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1735)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:495)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:477)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:113)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1133)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5362)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1700)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1690)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Things I have checked:
Avoiding duplicate jersey versions
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1</version>
</dependency>
So, ServletContainer is definitely imported
I notice however in the dependency hierarchy that the jersey servlet container is also imported through another library (dropwizard-core -> dropwizard-jersey -> jersey-container-servlet: 2.16) and the versions differ (my version is 2.25.1). But it says omitted for conflict with 2.25.1
Where is Apache Tomcat v7.0 package library
My project was originally a pure Maven project and I added the Dynamic Web Project facet lateron. When I compare to a test project where I successfully tested a jersey rest api, I don't see the Apache Tomcat v7.0 libraries on my build path. In my test project I see
versus
WebContent/WEB-INF/lib is empty
As mentioned in this question the jersey libraries should be deployed to my WEB-INF/lib/ folder. I did Project -> Deployment Assembly -> Add -> Java Build Path Entries -> Maven Dependencies -> Finish. But as soon as I do Project -> Maven -> Update the step above is reset (i.e. I don't see Maven Dependencies in the Web Deployment Assembly anymore. Despite this is strange, in the successful test jersey project the lib folder is also empty.
EDIT
There is something more fundamental going wrong. In the successful jersey rest api, I got the subfolder structure
target
+---- generated-sources
+---- JerseyRestDemo-0.0.1-SNAPSHOT
+---- m2e-wtp
+---------- web-resources
+----------------- META-INF
+-----------------------maven
+---- maven-archiver
+--------- pom.properties
+---- test-classes
WebContent
+---- META-INF
+--------- MANIFEST.MF
+---- WEB-INF
+--------- lib
+----------web.xml
However, in my current project I just see
target
+----- generated-sources
+---------- annotations
+----- maven-status
+---------- maven-compiler-plugin
+--------------- compile
WebContent
+---- META-INF
+--------- MANIFEST.MF
+---- WEB-INF
+--------- lib
+----------web.xml
Thanks for your help
I converted the project again from maven to webapp following this post and then it worked

Maven: Failed to read artifact descriptor for

I am writing project in Java using Maven for dependecines and I put on my own maven server lib for client api maven#server:~/public/com/iggroup/publicapi/client/java/api-client-sdk-2.0.5.jar and created in the same location .pom file which looks like below
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.iggroup.publicapi.client.java</groupId>
<artifactId>api-client-sdk</artifactId>
<version>2.0.5</version>
</project>
... and in my project pom.xml file I add dependency
<dependency>
<groupId>com.iggroup.publicapi.client.java</groupId>
<artifactId>api-client-sdk</artifactId>
<version>2.0.5</version>
</dependency>
Maven was refreshed, found correct version but can't load this lib and shows Failed to read artifact descriptor for com.iggroup.publicapi.client.java:api-client-sdk:jar:2.0.5
Lib is OK but maybe I do something wrong with name file, paths or my pom ?
Any idea ?
I found solution. Path in server was wrong, should looks like below:
maven#server:~/public/com/iggroup/publicapi/client/java/api-client-sdk/2.0.5/api-client-sdk-2.0.5.jar

Include Maven Project into NON-MAVEN Dynamic Web Application

I've included the Maven based project into my application, added Deployment Assembly which shows that the .jar file gets copied to WEB-INF/lib/ImportedProject.jar.
Problem occurs when imported lib wants to reference the lib that it is using which, apparently, does not get included inside the ImportedProject.jar.
Example:
ImportedProject pom.xml has:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
When I add ImportedProject.jar to my Non-Maven project and run it, I get exception:
threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
My question is: How to add both that Maven project and its dependencies?
maven dependency plugin helps to get required project library with it's dependencies.
So, If you would like to add POM based project on NON-POM based project, all you would do is to execute mvn dependency:copy-dependencies command and you will have all dependencies on target/dependencies folder. Copy all jar from dependencies and paste to WEB-INF/lib directory.

Master project - Maven dependencies in Intellij

I'm currently working with Intellij as IDE, and I have 4 projects:
- An Admin project (contains java & ember for the front-end)
- An WS project (contains the WS)
- An API project (contains the objects, dao's etc)
- A master project which only contains a pom to reference the other 3.
Admin and WS depend on API.
I deploy everything in JBoss. Maven clean-install works, but I can't deploy to jboss: I get a ClassNotFoundError: WS can't find classes in API
My pom of the master file looks like this:
<dependencies>
<dependency>
<groupId>API</groupId>
<artifactId>API</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/.../api/target/API-1.0.jar</systemPath>
</dependency>
<dependency>
<groupId>WS</groupId>
<artifactId>WS</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/.../ws/www/target/WS-1.0.war</systemPath>
</dependency>
<dependency>
<groupId>Admin</groupId>
<artifactId>Admin</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:/.../admin/www/target/Admin-1.0.war</systemPath>
</dependency>
</dependencies>
I use Maven 3.3.3 and Jboss WildFly 8.2.1
In Project Settings I've simply put the 3 Maven libraries (Admin, WS, API) under
In the Deployement tab of the Jboss condfiguration, the 2 wars (Admin, WS) and the jar (API) are listed
The errors I get:
Failed to define class ...ws.endpoints.helpers.WS in Module "deployment.WS-1.0.war:main" from Service Module Loader: java.lang.LinkageError: Failed to link .../ws/endpoints/helpers/WS (Module "deployment.WS-1.0.war:main" from Service Module Loader) Caused by: java.lang.NoClassDefFoundError: .../dao/helpers/Dao Caused by: java.lang.ClassNotFoundException: ...dao.helpers.Dao from [Module "deployment.WS-1.0.war:main" from Service Module Loader]
What am I doing wrong? Is there a better way to deploy all this?
I've also tried to use the local Maven repository to find the correct depedencies (removed the scope and system path), but I can't find them, although they are correct located under the C:\users.m2 directory
Thanks in advance!
Specifying scope as 'system' means that your artifact is always available and is not looked up in a repository, so if you are building those artifacts during your build life cycle - "system" scope is not the right way to go.
I would recommend:
1. Try packaging your artifacts and deploy to JBoss manually, just to make sure that it works
2. Then try it out with Maven plugin for JBoss: http://docs.jboss.org/jbossas/7/plugins/maven/latest/examples/deployment-example.html

What defines the "war-ness" of my project?

I'm running the goal: tomcat:deploy. There are no errors, but it's not deploying my project into tomcat. I noticed this message:
[INFO] Skipping non-war project
What defines the "war-ness" of my project? How do I make my Eclipse project a war project?
Here's my plugin setting:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
I have my structure like this:
src > main > java
src > main > webapp > WEB-INF > web.xml
This one works with the maven:war plugin. I'm able to build a war with this structure.
My end objective is to do away with the war building part and be able to deploy my project to tomcat with one maven command.
Maybe you are missing the 'packaging' element in your pom.xml:
<packaging>war</packaging>
If you don't include one, the default packaging is 'jar'.

Categories

Resources