这里是如何做到这一点。
首先,在你的pom.xml,声明在Web应用程序文件夹是:
<build>
<resources>
<resource>
<directory>src/main</directory>
</resource>
</resources>
这里是我的src/main目录树:
├── 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
添加的Maven插件阴影在你的pom.xml文件:
<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>
然后开始码头是这样的:
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();
}
重要的部分是使用<YourApp>.class.getResource(<your location>)
,它将提供jar中文件的路径。错误的方法是这样做:webContext.setDescriptor("WEB-INF/web.xml");
它给出了文件系统上的路径。
然后创建包
$mvn clean package
产生尤伯杯jar文件,并包含被宣布为资源的webapp目录。
任何地方或在生产服务器上移动的罐子,这样运行:
$ java -jar myjettyembededwithwebxmlandhtmljspfile.jar
链接似乎被打破。 – 2014-03-21 19:46:53
链接已修复 – jreznot 2014-03-24 16:50:13