使用DropWizard时NoClassDefFoundError:org / eclipse / jetty / io / ByteBufferPool

时间:2014-10-07 20:30:53

标签: java maven jetty dropwizard

我目前正在尝试运行dropwizard(http://dropwizard.io/getting-started.html)的入门应用程序。我可以设法使用mvn包构建jar文件但是当我运行该文件时(java -jar target / my-project-0.0.1-SNAPSHOT.jar)我收到错误:

Exception in thread "main" java.lang.NoClassDefFoundError:
   org/eclipse/jetty/io/ByteBufferPool

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>my-project</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>io.dropwizard</groupId>
        <artifactId>dropwizard-core</artifactId>
        <version>${dropwizard.version}</version>
    </dependency>
</dependencies>

<properties>
    <!-- use UTF-8 for everything -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <dropwizard.version>0.7.0</dropwizard.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <!-- compile for Java 1.7 -->
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <configuration>
        <createDependencyReducedPom>true</createDependencyReducedPom>
        <filters>
            <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
            </filter>
        </filters>
        </configuration>
        <executions>
        <execution>
            <phase>package</phase>
            <goals>
            <goal>shade</goal>
            </goals>
            <configuration>
            <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.example.helloworld.HelloWorldApplication</mainClass>
                </transformer>
            </transformers>
            </configuration>
        </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
        <archive>
            <manifest>
            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
        </archive>
        </configuration>
    </plugin>
    </plugins>
</build>

我对maven配置没有太多经验,所以我没有看到任何错误。我所有的消息来源似乎都在正确的位置。感谢您的任何建议。

修改:

在向pom添加OF399991的建议后,我注意到在运行mvn软件包或安装时发出警告(已经存在,但不是很清楚):

[WARNING] The POM for org.eclipse.jetty:jetty-server:jar:9.0.7.v20131107 is invalid,
transitive dependencies (if any) will not be available: 1 problem was encountered while 
building the effective model for org.eclipse.jetty:jetty-server:9.0.7.v20131107

[FATAL] Non-parseable POM /home/yenox/.m2/repository/org/eclipse/jetty/jetty-
parent/20/jetty-parent-20.pom: end tag name </body> must match start tag name <hr> from 
line 5 (position: TEXT seen ...</center>\r\n</body>... @6:8)  @ 
/home/yenox/.m2/repository/org/eclipse/jetty/jetty-parent/20/jetty-parent-20.pom, line 6, 
column 8

2 个答案:

答案 0 :(得分:2)

我最终在.m2 / repository中删除了包含依赖项的文件夹,然后进行了mvn clean install,一切都恢复正常。

修改: 我想我知道错误的来源:我首先使用了maven2,然后切换到maven3,它可能搞砸了我的存储库。

答案 1 :(得分:1)

您需要确保使用maven构建的jar包含所有dropwizard库。我建议你检查你构建的jar文件,看看它是否包含这些。

如果没有,那么你需要在标签之间的pom.xml文件中添加这样的东西:

   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
        <archive>
        <manifest>
           <mainClass>com.wibblewobble</mainClass>
        </manifest>
        </archive>
        <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
         </descriptorRefs>
         </configuration>
         <executions>
             <execution>
             <id>make-assembly</id>
             <phase>package</phase>
             <goals>
             <goal>single</goal>
             </goals>
             </execution>
        </executions>
        </plugin>

然后使用以下内容构建jar:

mvn package assembly:single

你可能会得到两个罐子,但你想要运行的jar会被称为wibblewobble-1.0-jar-with-dependencies。