简体   繁体   English

无法使用运行时依赖项进行编译,但可以使用编译

[英]Can't compile with runtime dependency, but can with compile

I have a multi-module project, in one module I'm trying to add Okta-SDK dependency as described in the README: 我有一个多模块项目,在一个模块中,我试图按照自述文件中的说明添加Okta-SDK依赖项:

<properties>
  <okta.version>1.5.4</okta.version>
</properties>
<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-api</artifactId>
    <version>${okta.version}</version>
</dependency>
<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-impl</artifactId>
    <version>${okta.version}</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-httpclient</artifactId>
    <version>${okta.version}</version>
    <scope>runtime</scope>
</dependency>

but it doesn't compile with mvn clean package -pl my-module , it fails with error: 但是它不能与mvn clean package -pl my-module一起编译,它会失败并显示以下错误:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /some/path/OktaUsers.java:[9,38] error: package com.okta.sdk.impl.resource.user does not exist
[ERROR] /some/path/OktaUsers.java:[96,14] error: cannot find symbol
[ERROR]  class OktaUsers
/some/path/OktaUsers.java:[97,13] error: cannot find symbol
[INFO] 3 errors 

This class OktaUsers has references to Okta-SDK classes. 此类OktaUsers引用了Okta-SDK类。 But if I change runtime scope to compile for okta-sdk-impl artifact: 但是,如果我更改runtime范围以针对okta-sdk-impl工件进行compile

<dependency>
    <groupId>com.okta.sdk</groupId>
    <artifactId>okta-sdk-impl</artifactId>
    <version>${okta.version}</version>
    <scope>compile</scope>
</dependency>

Then compiling finished fine: mvn clean package -pl my-module : 然后编译完成: mvn clean package -pl my-module

[INFO] BUILD SUCCESS

But final app is not working as expected because of dependency issues: 但是由于依赖性问题,最终应用无法正常工作:

java.lang.NoClassDefFoundError: Could not initialize class com.okta.sdk.impl.ds.DefaultDataStore
    at com.okta.sdk.impl.client.AbstractClient.createDataStore(AbstractClient.java:73)
    at com.okta.sdk.impl.client.AbstractClient.<init>(AbstractClient.java:68)
    at com.okta.sdk.impl.client.DefaultClient.<init>(DefaultClient.java:99)
    at com.okta.sdk.impl.client.DefaultClientBuilder.build(DefaultClientBuilder.java:305)

I thought all dependencies with runtime scope should be available when compiling. 我认为编译runtime所有具有runtime范围的依赖项都应该可用。 But it seems that doesn't. 但似乎并非如此。 How can I fix this issue? 如何解决此问题? I need to have this dependency for compiling and for runtime both. 我需要在编译和运行时都具有此依赖关系。


UPDATE: 更新:

I can't share sources from this project, since it's private, but here some lines from OktaUser class: 由于它是私有的,因此我无法共享该项目的源,但是这里有OktaUser类的一些行:

import com.okta.sdk.client.Client;
import com.okta.sdk.impl.resource.user.DefaultRole;
import com.okta.sdk.resource.group.Group;
import com.okta.sdk.resource.group.GroupBuilder;
import com.okta.sdk.resource.user.Role;
import com.okta.sdk.resource.user.RoleStatus;
import com.okta.sdk.resource.user.UserBuilder;
import com.okta.sdk.resource.user.UserProfile;


public final class OktaUsers implements Users {

    private final Client okta;


// line 95:
    private Role adminRole() {
        final DefaultRole role =
            (DefaultRole) this.okta.instantiate(Role.class);
        role.setProperty("type", "USER_ADMIN");
        role.setProperty("status", RoleStatus.ACTIVE);
        return role;
    }
}

I'm using classes from impl dependency here, since api dep doesn't have methods to manage Okta roles. 我在这里使用来自impl依赖的类,因为api dep没有管理Okta角色的方法。


This code works fine with integration tests when running mvn verify -pl my-module 当运行mvn verify -pl my-module时,此代码可以很好地与集成测试一起使用


Maven version: Maven版本:

Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T22:00:29+03:00)
Maven home: /usr/share/maven-bin-3.6
Java version: 1.8.0_212, vendor: IcedTea, runtime: /opt/icedtea-bin-3.12.0/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.19.44-gentoo", arch: "amd64", family: "unix"

Can you post the content of /some/path/OktaUsers.java ? 您可以发布/some/path/OktaUsers.java的内容吗? It looks like this class is depending on classes from the Okta IMPL package. 此类似乎取决于Okta IMPL包中的类。 If I understand the Okta API correctly your own code is supposed to depend on/use only classes from the API module, NOT the impl modules. 如果我正确理解Okta API,则您自己的代码应该仅依赖/使用API​​模块中的类,而不依赖impl模块。 If you remove the dependency on the class from the impl module you should be fine. 如果从impl模块中删除对类的依赖关系,那应该没问题。

Edit 1: I see two approaches. 编辑1:我看到两种方法。 You could remove the dependency to DefaultRole. 您可以删除对DefaultRole的依赖。

// line 95:
private Role adminRole() {
    Role role = this.okta.instantiate(Role.class);
    role.setType("USER_ADMIN");
    // use reflection to check if there is a setProperty() method
    // and call it if it's really necessary
    // role.setProperty("status", RoleStatus.ACTIVE);
    return role;
}

The second approach is to find out, why the jvm cannot instantiate com.okta.sdk.impl.ds.DefaultDataStore . 第二种方法是找出为什么jvm无法实例化com.okta.sdk.impl.ds.DefaultDataStore This can be a very tedious task as the jvm doesn't dive you a proper error message. 这可能是一个非常繁琐的任务,因为jvm不会向您显示正确的错误消息。 I'd guess that a dependency is missing from the classpath. 我猜想类路径中缺少依赖项。 You need to look at the source of the class and make sure that all referenced classes (and all classes referenced by these classes and so on) are on the classpath. 您需要查看类的源,并确保所有引用的类(以及这些类引用的所有类,依此类推)都在类路径上。 Doing so, you need to make sure that the correct versions of these classes are used, ie the same versions that the referencing classes were build against. 这样做,您需要确保使用了这些类的正确版本,即,与之建立参照类的版本相同。 For starters, make sure that org.slf4j.Logger and a single implementation of the logger is on the classpath and that it's properly configured. 对于初学者,请确保org.slf4j.Logger和logger的单个实现位于类路径上,并且已正确配置。

The proper fix would be to change the api. 正确的解决方法是更改​​api。 There seems to be a solved issue in the okta issue list describing a similar problem. okta问题列表中似乎存在一个已解决的问题 ,描述了类似的问题。 Maybe this can help you? 也许这可以帮助您? If not, I suggest contacting the okta developer and asking them how to fix your problem. 如果没有,我建议联系okta开发人员并询问他们如何解决您的问题。

A dependency with scope runtime is available at runtime only, not at compile time. 具有作用域runtime的依赖项仅在运行runtime可用,而在编译时不可用。 This is meant for implementation jars and framework jars that should not be called directly. 这适用于不应直接调用的实现jar和框架jar。

A dependency with scope compile is available both at compile time and at runtime. 具有范围compile的依赖项在编译时和运行时均可用。

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

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