简体   繁体   中英

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

I am trying to configure maven to build a runnable jar. The project that this is about contains a couple of dependencies as well as embedded jetty.

I want to embed all dependencies (which works) and have one executable 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.

However, when I use the maven-plugin and run exec:java the program starts without problems.

What am I doing wrong?

Here is a copy of my 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. 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.

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 .

Seems to me your pom simply does not reflect the situation:

  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.
  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).

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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