简体   繁体   English

如何使用嵌入式jetty和maven构建发行版?

[英]How can I build a release with embedded jetty and maven?

I am trying to configure maven to build a runnable jar. 我正在尝试配置maven来构建一个可运行的jar。 The project that this is about contains a couple of dependencies as well as embedded jetty. 这个项目包含几个依赖项以及嵌入式jetty。

I want to embed all dependencies (which works) and have one executable jar. 我想嵌入所有依赖项(有效)并拥有一个可执行jar。 I can run the project fine from eclipse but once I try to run the jar I get ClassNotFoundExceptions on org.eclipse.jetty.servlet.ServletContextHandler. 我可以从eclipse运行项目,但是一旦我尝试运行jar,我就会在org.eclipse.jetty.servlet.ServletContextHandler上获得ClassNotFoundExceptions。

However, when I use the maven-plugin and run exec:java the program starts without problems. 但是,当我使用maven-plugin并运行exec:java时,程序启动没有问题。

What am I doing wrong? 我究竟做错了什么?

Here is a copy of my pom.xml 这是我的pom.xml的副本

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>comms</groupId>
  <artifactId>comms-app</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Air</name>
  <properties>
      <jettyVersion>9.2.7.v20150116</jettyVersion>
      <gsonVersion>2.3.1</gsonVersion>
      <logjVersion>2.1</logjVersion>
      <lombokVersion>1.16.2</lombokVersion>
  </properties>


  <dependencies>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jettyVersion}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gsonVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${logjVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${logjVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombokVersion}</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <goals><goal>java</goal></goals>
            </execution>
        </executions>
        <configuration>
          <mainClass>comms.Loader</mainClass>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <mainClass>comms.Loader</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

I noted that you declared jetty-maven-plugin as a dependency , not a plugin declaration. 我注意到你将jetty-maven-plugin声明为依赖项 ,而不是插件声明。 The jetty-maven-plugin depends on Jetty and that (ie the transitive dependency on Jetty) is how the Jetty classes get pulled into the Maven classpath. jetty-maven-plugin依赖于Jetty,而且(即对Jetty的传递依赖性)是Jetty类如何被拉入Maven类路径。

However that dependency is declared as 'provided', which basically says: "my code is dependent on this, but normally it will be provided by the deployment environment". 但是,该依赖项被声明为“提供”,它基本上表示:“我的代码依赖于此,但通常它将由部署环境提供”。 Maven will pull in the dependency because it needs it for building your source (which is probably why the exec plugin does work), but will not include it in your jar, because you specifically told it to do so . Maven会引入依赖,因为它需要它来构建你的源代码(这可能是exec插件工作的原因),但是不会把它包含在你的jar中, 因为你特意告诉它这样做

Seems to me your pom simply does not reflect the situation: 在我看来,你的pom根本不反映情况:

  1. If your code depends on Jetty classes (not the plugin), remove the jetty-maven-plugin dependency and declare Jetty itself as an explicit dependency. 如果您的代码依赖于Jetty类(而不是插件),请删除jetty-maven-plugin 依赖项并将Jetty本身声明为显式依赖项。
  2. Do not define the scope as 'provided' unless the deployment environment really does provide the Jetty classes (which it doesn't, given the error). 不要将范围定义为“提供”,除非部署环境确实提供了Jetty类(鉴于错误,它没有提供)。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM