如何在 IntelliJ IDEA 的 Web 应用程序项目中部署 Maven 依赖项?

How do I deploy maven dependencies in a web application project in IntelliJ IDEA?

我按照 these steps on JetBrain's site 创建了一个 Web 应用程序并让 GlassFish 在本地运行。 运行 很好,我可以将代码的输出嵌入 JSP 中项目的 src 树中,并在 Web 浏览器中查看它。当我添加 Maven 依赖项时,问题就开始了。我加了

<dependency>
    <groupId>com.nimbusds</groupId>
    <artifactId>oauth2-oidc-sdk</artifactId>
    <version>4.9</version>
</dependency>

到我的 pom.xml 并在我的代码中使用依赖项。 Maven 安装依赖项,一切正常,但是当我在 GlassFish 中单击绿色箭头 运行 时,当浏览器打开时出现此错误:

org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP

PWC6199: Generated servlet error: source value 1.5 is obsolete and will be removed in a future release

PWC6199: Generated servlet error: target value 1.5 is obsolete and will be removed in a future release

PWC6199: Generated servlet error: To suppress warnings about obsolete options, use -Xlint:-options.

PWC6197: An error occurred at line: 8 in the jsp file: /index.jsp PWC6199: Generated servlet error: cannot access com.nimbusds.oauth2.sdk.SerializeException class file for com.nimbusds.oauth2.sdk.SerializeException not found

这是 index.jsp 的来源:

<%@ page import="com.idsrvjpoc.IdentityServerConfiguration" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Redirect</title>
    </head>
    <body>
        <% String redirectUrl = new IdentityServerConfiguration().getRedirectUrl(); %>
        <a href="<%=redirectUrl%>"><%=redirectUrl%></a>
    </body>
</html>

在我的模块的 Java 编译器设置中,目标字节码版本设置为 1.8,我使用的是最新版本的 1.8 JDK。

我发现了另一个建议将依赖项的 JAR 文件复制到 glassfish4\glassfish\domains\domain1\lib 文件夹的 SO 答案,所以我尝试了它并更改了错误:

java.lang.NoClassDefFoundError: com/nimbusds/oauth2/sdk/id/ClientID

不再有过时的目标值或 class 找不到文件,而是一个 NoClassDefFoundError。 class 现在不一样了。 SerializeException 和 ClientID 都在同一个 JAR 中,我可以提取 JAR 并在我看来是正确位置的地方查看 .class 文件。

这是 IdentityServerConfiguration.java 的来源:

package com.idsrvjpoc;

import com.nimbusds.oauth2.sdk.*;
import com.nimbusds.oauth2.sdk.id.*;

import java.net.URI;
import java.net.URISyntaxException;

public class IdentityServerConfiguration {
    public String getRedirectUrl() throws URISyntaxException, SerializeException {
        URI authzEndpoint = new URI("myAuthEndpoint");
        ClientID clientID = new ClientID("1234");
        Scope scope = new Scope("openid");
        URI callback = new URI("http://localhost:8080/IdentityServerPoc_war_exploded/return.jsp");
        State state = new State();

        AuthorizationRequest request = new AuthorizationRequest.Builder(
                new ResponseType(ResponseType.Value.CODE), clientID).
                scope(scope).
                state(state).
                redirectionURI(callback).
                endpointURI(authzEndpoint).
                build();

        return request.toURI().toString();        
    }
}

我错过了什么?

E:我刚刚尝试从 GlassFish lib 文件夹中删除 JAR 并编辑 war 工件,在输出布局选项卡上,我将罐子从可用元素移动到 WEB-INF/lib 目录,然后我看到他们在我构建时被复制到那里,但仍然是同样的错误。

我还尝试让工件将 JAR 中的 .class 文件提取到 WEB-INF/classes 文件夹中。同样,我在项目的 out 文件夹中的 WEB-INF/classes 文件夹中看到它们,但仍然是相同的 java.lang.NoClassDefFoundError: com/nimbusds/oauth2/sdk/id/ClientID

E2:这是完整的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>IdSrvJPoc</groupId>
    <artifactId>IdentityServerJavaPoc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.nimbusds</groupId>
            <artifactId>oauth2-oidc-sdk</artifactId>
            <version>4.9</version>
        </dependency>
    </dependencies>
</project>

由于您收到 NoClassDefFoundError,因此您的 lib 文件夹中可能仍然缺少依赖项。确保还包含所有 Numbus 依赖项。