Here's how to do it.
First, in your pom.xml, declare where the webapp folder is located:
<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 the Maven Shade plugin to the 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 run Jetty as follows:
public static void startJetty() throws Exception { logger.info("starting Jetty..."); Server server = new Server(8080); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/"); String webxmlLocation = App.class.getResource("/webapp/WEB-INF/web.xml").toString(); webAppContext.setDescriptor(webxmlLocation); String resLocation = App.class.getResource("/webapp").toString(); webAppContext.setResourceBase(resLocation); webAppContext.setParentLoaderPriority(true); server.setHandler(webAppContext); server.start(); server.join(); }
An important part is the use of <YourApp>.class.getResource(<your location>) , which 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 to the file system.
Then create a package
$mvn clean package
A uber-jar file is created and contains the webapp directory, which was declared as a resource.
Move the jar anywhere or on the production server and execute it as follows:
$ java -jar myjettyembededwithwebxmlandhtmljspfile.jar
source share