Maven 笔记

By | 2021年12月31日

依赖版本范围

maven项目中所依赖的jar包版本不是唯一特定的版本,可以使用版本范围设定,设定的规则见下表:

1.0 x >= 1.0 * The default Maven meaning for 1.0 is everything (,) but with 1.0 recommended. Obviously this doesn’t work for enforcing versions here, so it has been redefined as a minimum version.
(,1.0] x <= 1.0
(,1.0) x < 1.0
[1.0] x == 1.0
[1.0,) x >= 1.0
(1.0,) x > 1.0
(1.0,2.0) 1.0 < x < 2.0
[1.0,2.0] 1.0 <= x <= 2.0
(,1.0],[1.2,) x <= 1.0 or x >= 1.2. Multiple sets are comma-separated
(,1.1),(1.1,) x != 1.1
<dependency>
    <groupId>com.falsec.nsm</groupId>
    <artifactId>fal-common</artifactId>
    <version>[1.4.0,1.5.0)</version>
</dependency>

标识依赖版本为大于等于1.4.0,且小于1.5.0的jar包。

FAQ

1 设置第三方依赖包中依赖scope值,由runtime到compile

我们在pom文件中添加 dependency时,如果不设置 scope,那么默认使用的是 compile,例如:

<dependency>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-server-core-webflow</artifactId>
    <version>5.2.0</version>
</dependency>

这个依赖包中,又包含了 org.apereo.cas:cas-server-core-util:jar:5.2.0:runtime,注意它的scope是 runtime:

mvn dependency:tree

......
......
+- org.apereo.cas:cas-server-core-webflow:jar:5.2.0:compile
[INFO] |  +- org.apereo.cas:cas-server-core-api-ticket:jar:5.2.0:compile
[INFO] |  +- org.apereo.cas:cas-server-core-api:jar:5.2.0:compile
[INFO] |  |  +- org.apereo.cas:cas-server-core-api-logout:jar:5.2.0:compile
[INFO] |  |  \- org.apereo.cas:cas-server-core-api-validation:jar:5.2.0:compile
[INFO] |  +- org.apereo.cas:cas-server-core-util:jar:5.2.0:runtime
[INFO] |  |  +- org.bitbucket.b_c:jose4j:jar:0.6.1:runtime
[INFO] |  |  +- org.apereo.cas:cas-server-core-api-util:jar:5.2.0:runtime
[INFO] |  |  +- org.apereo.cas:cas-server-core-api-web:jar:5.2.0:runtime
[INFO] |  |  +- org.apache.shiro:shiro-core:jar:1.4.0:runtime
[INFO] |  |  |  +- org.apache.shiro:shiro-lang:jar:1.4.0:runtime
[INFO] |  |  |  +- org.apache.shiro:shiro-cache:jar:1.4.0:runtime
[INFO] |  |  |  +- org.apache.shiro:shiro-crypto-hash:jar:1.4.0:runtime
[INFO] |  |  |  |  \- org.apache.shiro:shiro-crypto-core:jar:1.4.0:runtime
[INFO] |  |  |  +- org.apache.shiro:shiro-crypto-cipher:jar:1.4.0:runtime
[INFO] |  |  |  +- org.apache.shiro:shiro-config-core:jar:1.4.0:runtime
[INFO] |  |  |  +- org.apache.shiro:shiro-config-ogdl:jar:1.4.0:runtime
[INFO] |  |  |  \- org.apache.shiro:shiro-event:jar:1.4.0:runtime
[INFO] |  |  +- com.google.zxing:core:jar:3.3.0:runtime
[INFO] |  |  \- com.vdurmont:semver4j:jar:2.0.3:runtime
......
......

同事直接使用它来在项目中写代码,提交后,导致我这边一片红:

解决方法很简单,将这个 runtime 改为 compile 就可以了:

发表评论

您的电子邮箱地址不会被公开。