Reputation: 1139
I'm trying to deploy a web application on an embedded Jetty Server. My application runs fine locally in a windows environment with the code below but when i deploy it as a JAR File on a Linux Server, it looks like my web.xml File is not picked up. Is there something i need to change in Descriptor or ResourceBase fields below before building a JAR?
static void startJetty() {
try {
Server server = new Server(9090);
WebAppContext context = new WebAppContext();
context.setDescriptor("/WEB-INF/web.xml");
context.setResourceBase("../DemoWithMultiChannels/src/");
context.setContextPath("/");
context.setParentLoaderPriority(true);
server.setHandler(context);
System.out.println("Starting Server!");
server.start();
Upvotes: 6
Views: 10665
Reputation: 21
Deploy embedded Jetty as follows:
public static void main(String[] args) throws Exception {
Server server = new Server(8085);
WebAppContext webContext = new WebAppContext();
webContext.setDescriptor("WEB-INF/web.xml");
webContext.setResourceBase("src/sim/ai/server/start");
webContext.setServer(server);
webContext.setParentLoaderPriority(true);
server.setHandler(webContext);
server.start();
server.join();
}
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>sim.ai.server.start</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>sim.ai.server.start</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Create a WEB_INF
folder in the same folder as the jar file; copy web.xml
into WEB_INF
, such as:
sim/light.jar
sim/WEB-INF/web.xml
Upvotes: 2
Reputation: 16272
Here is how to do it.
First, in your pom.xml, declare where the webapp folder is:
<build>
<resources>
<resource>
<directory>src/main</directory>
</resource>
</resources>
Here is the tree of my src/main directory:
├── java
│ └── com
│ └── myco
│ └── myapp
│ └── worker
│ ├── App.java
| ...
├── resources
│ ├── log4j.properties
│ └── version.properties
└── webapp
├── index.html
├── index.jsp
├── lib
│ ├── inc_meta.jsp
│ └── inc_navigation.jsp
├── query.html
├── scripts
│ ├── angular.min.js
│ └── bootstrap.min.css
├── showresults.jsp
├── status.jsp
└── WEB-INF
└── web.xml
Add Maven Shade plugin in your pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>uber-${artifactId}-${version}/finalName>
</configuration>
</plugin>
Then start Jetty like this:
public static void startJetty() throws Exception {
logger.info("starting Jetty...");
Server server = new Server(8080);
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
/* Important: Use getResource */
String webxmlLocation = App.class.getResource("/webapp/WEB-INF/web.xml").toString();
webAppContext.setDescriptor(webxmlLocation);
/* Important: Use getResource */
String resLocation = App.class.getResource("/webapp").toString();
webAppContext.setResourceBase(resLocation);
webAppContext.setParentLoaderPriority(true);
server.setHandler(webAppContext);
server.start();
server.join();
}
The important part is to use <YourApp>.class.getResource(<your location>)
that will give the path to the files inside the jar. The wrong way would be to do it like this: webContext.setDescriptor("WEB-INF/web.xml");
which gives the path on the file system.
Then create package
$mvn clean package
The uber-jar file is generated and contains the webapp directory that was declared as resource.
Move the jar anywhere or on the production server and run it like this:
$ java -jar myjettyembededwithwebxmlandhtmljspfile.jar
Upvotes: 4
Reputation: 2773
I have replaced WebAppContext and WebXmlConfiguration with specific descendants which can find resources in classpath:
WebAppContex: https://github.com/jreznot/diy-remote/blob/master/server/src/org/strangeway/diyremote/server/sys/ClasspathWebAppContext.java
WebXmlConfiguration: https://github.com/jreznot/diy-remote/blob/master/server/src/org/strangeway/diyremote/server/sys/ClasspathWebXmlConfiguration.java
It works good for me
Upvotes: 0
Reputation: 5619
I had the same problem and just found the solution:
It was working fine when I run "java -jar ..." from terminal, but when I spawned it off from another project, web.xml was not picked up.
Reason was web.xml path was wrong, it was relative to original project, what I end up doing is:
context.setDescriptor(Launch.class.getResource("/WEB-INF/web.xml").toString());
If you don't use resource you just read the regular file inside your src folder, not the one inside the .jar
Upvotes: 6