简体   繁体   English

Gradle / Maven:如何使用开发中的依赖项?

[英]Gradle / Maven: How to use in-developement dependencies?

Context: 内容:

I'm working on a Gradle project which depends on external libraries, also created by me. 我正在开发一个Gradle项目,该项目依赖于我创建的外部库。 Currently I was usign a simple dependecy on project(':lib') . 目前,我对project(':lib')一个简单的依赖。

But this is not enough anymore, I need to release and distribute libraries as standalone components, versionned and documented. 但是,这还不够,我需要将库作为独立的组件发布和分发,进行版本化和记录。 I will install Apache Archiva and publish them to an internal maven repository, so I can depend explicitly on 'com.company:lib:1.0' . 我将安装Apache Archiva并将其发布到内部Maven存储库,因此我可以显式依赖'com.company:lib:1.0'

Question: 题:

During developement, I will work on libraries and on projects at the same time. 在开发期间,我将同时从事图书馆和项目的工作。 How can I test my code without publishing the libraries ? 如何在不发布库的情况下测试代码? My application which used to depend on project() will now depend on a specific version. 我以前依赖于project()应用程序现在将依赖于特定版本。 But while developing, I would like to use the local code. 但是在开发时,我想使用本地代码。

Do you know what is the best process to handle this ? 您知道处理此问题的最佳过程是什么吗?

One way would be to add the dependency conditionally. 一种方法是有条件地添加依赖项。 So for your local builds (IDE) you want to build the dependency via source. 因此,对于您的本地构建(IDE),您想通过源构建依赖项。 Then you can distinguish your release builds by having your releases pass a param to a build. 然后,可以通过使发布将参数传递给构建来区分发布的构建。

dependencies {
    if (project.hasProperty('release')) {
        compile 'com.company:lib:1.0'
    } else {
        compile project(':lib')
    }
}

Then in your release builds to use the lib from nexus: 然后在您的发行版中使用nexus的lib:
$ gradle -Prelease=true clean build

If you want to build the project with the lib from inside the project: 如果要从项目内部使用lib构建项目:
$ gradle clean build

The correct answer to this question is available in Gradle's documentation (section 23.8.3.1 ), and is the following: Gradle的文档 (第23.8.3.1节)中提供了关于此问题的正确答案,该答案如下:

configurations.all {
    resolutionStrategy.dependencySubstitution {
        substitute module("com.company:lib") with project(":lib")
        substitute module("com.company:lib2") with project(":lib2")
    }
}

Personally, I used the following code, which makes all -SNAPSHOT versions to be taken from local projects instead of remote repository (if a local project is available): 我个人使用以下代码,使所有-SNAPSHOT版本都从本地项目而不是远程存储库中获取(如果有本地项目):

configurations.all {
    resolutionStrategy.dependencySubstitution {
        all { dependency ->
            if (! dependency.requested.version.endsWith('SNAPSHOT'))
                return
            if (subprojects.find { p ->p.name == dependency.requested.module })
                dependency.useTarget project(":" + dependency.requested.module)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM