方法は次のとおりです。
まず、pom.xml で、webapp フォルダーの場所を宣言します。
<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
pom.xml ファイルに Maven Shade プラグインを追加します。
<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>
次に、次のように Jetty を起動します。
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
uber-jar ファイルが生成され、リソースとして宣言された webapp ディレクトリが含まれます。
jar を任意の場所または運用サーバーに移動し、次のように実行します。
$ java -jar myjettyembededwithwebxmlandhtmljspfile.jar