找出消失的已引入的类库

最近使用软件加密的时候用到了一个密码库,顺手从Maven Repository上,把对应的坐标给粘贴下来,引入后,项目也更新了。但是发现用不了对应的库里面的函数。

对应的Maven Repository的坐标信息如下。

<!-- https://mvnrepository.com/artifact/com.tencent.kona/kona-crypto -->
<dependency>
    <groupId>com.tencent.kona</groupId>
    <artifactId>kona-crypto</artifactId>
    <version>1.0.11</version>
    <scope>runtime</scope>
</dependency>
  1. 一开始的时候,以为是类库没有下载成功。去到本地路径,找了下,地区是在的也是完整的。
  2. 然后删除类库,重新下载,重新导入项目。发现还是没法引用到对应的项目。
  3. 最后用了最原始的办法,重新新建了一个项目,把对应的包直接手工放到项目路径,居然发现可以了。
  4. 因为想到这个方法可行,就想了下两个项目的差异点,一个是maven 项目,一个是普通的项目。唯一的差异点就是POM文件。
  5. 然后打开POM文件,发现就只有这个类库的scope 是 runtime导致,项目没法引入对应的类和方法。
  6. 去掉scope后使用默认的范围,问题修复。

重新看了下官网对scope范围的定义:

Dependency Scope

Dependency scope is used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath.

There are 6 scopes:

  • compile

    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

  • provided

    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. A dependency with this scope is added to the classpath used for compilation and test, but not the runtime classpath. It is not transitive.

  • runtime

    This scope indicates that the dependency is not required for compilation, but is for execution. Maven includes a dependency with this scope in the runtime and test classpaths, but not the compile classpath.

  • test

    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive. Typically this scope is used for test libraries such as JUnit and Mockito. It is also used for non-test libraries such as Apache Commons IO if those libraries are used in unit tests (src/test/java) but not in the model code (src/main/java).

  • system

    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

  • import

    This scope is only supported on a dependency of type pom in the section. It indicates the dependency is to be replaced with the effective list of dependencies in the specified POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

Each of the scopes (except for import) affects transitive dependencies in different ways, as is demonstrated in the table below. If a dependency is set to the scope in the left column, a transitive dependency of that dependency with the scope across the top row results in a dependency in the main project with the scope listed at the intersection. If no scope is listed, it means the dependency is omitted.

compile provided runtime test
compile compile(*) - runtime -
provided provided - provided -
runtime runtime - runtime -
test test - test -