Generate a jar with embedded Jetty using maven jetty plugin?

雨燕双飞 提交于 2020-02-22 06:12:25

问题


I use the mvn jetty:run goal from the maven jetty plugin to start my web application project, built using maven.

Now I want to create a fat jar with embedded Jetty so that I will be able to start my app using java -jar.

Is there a way to use the maven jetty plugin to generate a fat jar with embedded Jetty?


回答1:


MarianP already answered your question in the comment.

Add your jetty dependencies in your pom.xml

<!-- jetty dependencies -->
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-server</artifactId>
  <version>9.2.5.v20141112</version>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet</artifactId>
  <version>9.2.5.v20141112</version>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>websocket-server</artifactId>
  <version>9.2.5.v20141112</version>
</dependency>

Add the shade plugin like so:

  <!-- maven shade plugin, executable uber Jar -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>YourMainClassHere</mainClass>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.schemas</resource>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>

Then add your server/client from your main()



来源:https://stackoverflow.com/questions/31295771/generate-a-jar-with-embedded-jetty-using-maven-jetty-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!