maven中依赖非特定版本

maven中依赖非特定版本

第一篇技术博客,写在CSDN

首先纠正一下发音: maven 读音[英][ˈmeɪvn][美][ˈmevən]

参考:maven这词怎么读?

依赖的版本号在某个范围内

在maven中我们经常像下面这样引用一个依赖:

<dependency>
  <groupId>wonderful-inc</groupId>
  <artifactId>dream-library</artifactId>
  <version>1.2.3</version>
</dependency>

我们指定了固定的版本1.2.3,如果我们想每次都引用最新版本,是否可以呢?答案是可以的,在pom文件的定义中,可以使用版本的一个范围,版本范围的写法说明如下:

(,1.0] x <= 1.0
1.0 “Soft” requirement on 1.0 (just a recommendation - helps select the correct version if it matches all ranges)
[1.0] Hard requirement on 1.0
[1.2,1.3] 1.2 <= x <= 1.3
[1.0,2.0) 1.0 <= x < 2.0
[1.5,) x >= 1.5
(,1.0],[1.2,) x <= 1.0 or x >= 1.2. Multiple sets are comma-separated
(,1.1),(1.1,) This excludes 1.1 if it is known not to work in combination with this library

参考:maven依赖指定版本范围或者最新版本

使用Profile解决编译问题

用途

Profile可以配置在setting.xml中,也可以放在pom.xml中(习惯上写在最后)

  • 定义在settings.xml中:这意味着该profile是全局的
    • repositories
    • pluginRepositories
    • properties
  • 定义在pom.xml中
    • repositories
    • pluginRepositories
    • dependencies
    • plugins
    • properties
    • dependencyManagement
    • distributionManagement
    • build元素下面的子元素,主要包括:defaultGoal resources testResources finalName

生效方式

  • 手动生效:命令行指定要生效的Profile
mvn clean install -Pproduction
  • 自动生效:配置生效条件
<profile>
    <id>mac</id>
    <activation>
        <activeByDefault>false</activeByDefault>
        <os>
            <family>mac</family>
        </os>
    </activation>
    <properties>
        <database.url>jdbc:postgresql://localhost/database</database.url>
        <database.user>username</database.user>
        <database.password>password</database.password>
    </properties>
</profile>

查看当前处于激活状态的profile都有哪些

mvn help:active-profiles

参考:Maven提高篇系列之(四)——使用Profile


版权声明:本文为EatAnApple原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。