找出消失的已引入的類庫

KassonWu發表於2024-03-13

最近使用軟體加密的時候用到了一個密碼庫,順手從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 -

相關文章