【问题标题】:Why does Maven Jetty Plugin not find a class in a standard jetty jar为什么 Maven Jetty Plugin 在标准的码头 jar 中找不到类
【发布时间】:2013-08-01 13:24:13
【问题描述】:

我正在使用 Jetty 9.0.4v20130625 使用 maven-jetty-plugin 运行它。我已经实现了自己的LoginService 类来处理登录到领域的用户。在其中一行中,我尝试使用 org.eclipse.jetty.util.security.Credential 类,但在执行期间它会在该行抛出 NoClassDefFoundError

我的target 目录包含部署到 Jetty 实例的所有内容。其中WEB-INF/lib 文件夹不包含预期的jetty-util-9.0.4.v20130625.jar,因为插件应该已经拥有它,所以我相信排除了冲突的jar。那么是什么原因导致 jetty 实例找不到这个 jar 呢?

我正在使用 eclipse,它在代码中没有显示错误。我将它设置为 Maven 项目,Maven 处理依赖项。我将其设置为不打包码头罐子,因为它们应该在部署时由码头提供。这是我的 pom.xml:

<project ...>
  ...
  <packaging>war</packaging>
  ...
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-webapp</artifactId>
      <version>9.0.4.v20130625</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>2.11.2</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>...</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.0.4.v20130625</version>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <webApp>
            <contextPath>/...</contextPath>
          </webApp>
          <login-config>
            <auth-method>FORM</auth-method>
            <realm-name>Test Realm</realm-name>
            <form-login-config>
              <form-login-page>/loginAuth.html?param=redirect</form-login-page>
              <form-error-page>/loginAuth.html?param=failed</form-error-page>
            </form-login-config>
          </login-config>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
        <version>1.8.1</version>
        <configuration>
          <connectionType>developerConnection</connectionType>
        </configuration>
      </plugin>

      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>install</phase>
            <configuration>
              <tasks>
                <ant target="deploy" />
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.50</version>
          </dependency>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.9.2</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

我检查以确保插件依赖于 jetty-util.jar 并且它确实如 here 所见

可能是问题的一部分:为了实现一个新的 LoginService 接口类,我正在扩展已经实现 LoginServiceMappedLoginService 类(它在 Jetty jars 中)并且我覆盖了一些它的方法。这会导致问题吗?

【问题讨论】:

    标签: jetty maven-3 noclassdeffounderror maven-jetty-plugin


    【解决方案1】:

    Jetty 对 webapp 的类加载器隐藏了几乎所有的实现类。如果您需要从 webapp 内部访问它们,则需要将它们放入 webapp 的 WEB-INF/lib 中(在您的情况下,添加 jetty-util jar 的依赖项)。

    这里描述了这个类加载机制:http://www.eclipse.org/jetty/documentation/current/jetty-classloading.html

    顺便说一句,您的 pom.xml 中的 jetty-maven-plugin 配置似乎包含 web.xml 中的一些行(即元素内的那些行)。我相信 maven 会忽略它不理解的配置,但也许事情没有按照您认为的方式配置?另外,我没有看到使用您的自定义 LoginService 的任何设置。

    【讨论】:

    • 感谢您的回答 Jan。我认为可能是这种情况。关于您为 jetty-util.jar 添加依赖项的建议——如果我将 jetty-util.jar 放在我的 lib 文件夹中,则会违反加载程序约束。该错误表明 org.codehaus.plexus.classworlds.realm.ClassRealm 类加载器和 ...jetty.webapp.WebAppClassLoader 具有类型 ...jetty.util.security.Credential 的不同对象。我认为我必须手动将 Credential 类和我拥有的任何其他码头依赖项导入到我的项目中的不同包下?对吗?
    • 关于插件配置:你是对的,login-config 是不必要的。我在某处看到它包含在其他一些示例中,但我的 web.xml 也包含登录配置,并且工作正常。此外,我没有费心将自定义 loginService 的配置放在这里,因为我知道它配置正确——它应该在我的类中执行代码,但在 Credential 上失败。在 lib 中包含 jetty-util.jar 会导致我上一条评论中描述的 LinkageError。
    • 听起来好像您的自定义 LoginService 在您的战争中有效。我认为这不会成功,因为 LoginService 是容器工件的一部分。我建议您为自定义的 LoginService 创建一个单独的项目,然后将其 jar 添加到 jetty-maven-plugin 依赖项 - 这样所有内容都在容器的类路径中。
    猜你喜欢
    • 2017-04-21
    • 2013-06-27
    • 2016-05-19
    • 1970-01-01
    • 2011-07-04
    • 2018-06-16
    • 2014-10-10
    • 2022-07-10
    • 1970-01-01
    相关资源
    最近更新 更多