gradle解析mavenLocal()仓库地址流程

https://issues.gradle.org/browse/GRADLE-1900


Description

We have the situation where our CI server uses a custom maven localRepository location, configured in a settings.xml file in the conf subdirectory of the maven home dir. The Maven Ant Tasks, which we used before switching to Gradle, and which Gradle uses internally in the Maven plugin, read and apply the settings in this file. However, Gradle's mavenLocal() repository location does not.

So let's say I use the Maven Plugin's install task in Gradle project A and the mavenLocal() repository location in Gradle project B. Project B has an external dependency on artifacts from Project A. Project A will install its artifacts into the local repository configured in my $M2_HOME/conf/settings.xml file, whereas project B will always look them up in my ~/.m2/repository/ folder.

Since Gradle supports both directions, publishing and retrieving from Maven repositories out of the box, I think the behavior should be symmetrical here. It should just work without me having to manually specify repository locations in one of the two directions.

I've written a little workaround that mimics the behavior of the Maven Ant Tasks in looking up and applying the localRepository location from the maven config file locations:

buildscript {
    repositories {
        project.myMavenLocal()
    }

    ...
}
    myMavenLocal = {
        File mavenSettingsFile = new File(System.getProperty("user.home") + "/.m2/settings.xml");
        if (!mavenSettingsFile.exists() && System.getenv().get("M2_HOME") != null) {
            println "" + mavenSettingsFile + " doesn't exist..."
            mavenSettingsFile = new File(System.getenv().get("M2_HOME") + "/conf/settings.xml");
        }
        if (mavenSettingsFile.exists()) {
            println "" + mavenSettingsFile + " exists..."
            String repoLocation = new XmlSlurper().parse(mavenSettingsFile).localRepository.text();
            println "Repo location is " + repoLocation
            if ( repoLocation != null && repoLocation != "" ) {
                String localMavenRepo = 'file://' + new File(repoLocation).getAbsolutePath();
                println "Absolute repo location is " + localMavenRepo
                return repositories.mavenRepo(url: localMavenRepo)
            }
        }
        println "Falling back to default method..."
        return repositories.mavenLocal();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VCHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值