定义工件以用作另一个项目中的依赖项 [英] Define an artifact to be used as a dependency in another project

查看:110
本文介绍了定义工件以用作另一个项目中的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以一个项目使用由另一个项目生成的文件的方式配置两个Gradle项目. includeBuild将第一个项目添加到第二个项目中,并且在第二个项目中将文件定义为依赖项.

I'm trying to configure two Gradle projects in a way that one project uses files built by the other one. The first project is added to the second one by includeBuild and the file is defined in the second project as a dependency.

settings.gradle:

rootProject.name = 'testA'

build.gradle:

group = 'org.test'
version = '0.0.0.1_test'

task someZip (type: Zip) {
    from './settings.gradle'
    archiveName = 'xxx.zip'
    destinationDir = file("${buildDir}/test")
}

artifacts {
    //TODO add something here?
}

项目testB

settings.gradle:

Project testB

settings.gradle:

rootProject.name = 'testB'

if (System.getenv('LOCAL_COMPILATION') == 'true') {
    includeBuild '../testA'
}

build.gradle:

if (System.getenv('LOCAL_COMPILATION') != 'true') {
    repositories {
        maven { url '192.168.1.100' }
    }
}

configurations {
    magic
}

dependencies {
    magic 'org.test:xxx:0.0.0.+@zip'
}

task ultimateZip (type: Zip) {
    from configurations.magic
    archiveName = 'ultimate.zip'
    destinationDir = file("${buildDir}/ultimate-test")
}

说明

您可能会注意到该示例具有使用maven存储库的选项.我想强调的是,最终将有可能做到这一点. 使用Maven存储库不是这个问题的重点,但是除了解决方案之外,不应干扰该问题. (换句话说,您可以假设System.getenv('LOCAL_COMPILATION') == 'true'.)

Description

You may noticed that the example has an option use a maven repository. I wanted to highlight that eventually there will be a possibility to do that. Using Maven repository is not the point of this question, though, other than the solution should not interfere with that. (In other words you can assume that System.getenv('LOCAL_COMPILATION') == 'true'.)

问题是如何以其他项目能够识别的方式定义工件.

The question is how to define the artifact in a way that the other project is able to recognize it.

首选解决方案应该类似于Java插件,因为我在项目中使用jar依赖项,并且它们都通过includeBuild和存储库工作.

The preferred solution should be similar to what the Java plugin does because I'm using jar dependencies in my projects and they are working both through includeBuild and through a repository.

推荐答案

以下设置应该可以正常工作(在Gradle 5.5.1中进行了测试).除了XXX指示的更改外,它基本上与您的原始设置相对应.

The following setup should work (tested with Gradle 5.5.1). It mostly corresponds to your original setup with the exception of the changes indicated by XXX.

settings.gradle:

rootProject.name = 'testA'

build.gradle:

group = 'org.test'
version = '0.0.0.1_test'

task someZip (type: Zip) {
    from './settings.gradle'
    archiveName = 'xxx.zip'
    destinationDir = file("${buildDir}/test")
}

// XXX (replaced your empty "artifacts" block)
configurations.create('default')
def myArtifact = artifacts.add('default', someZip) {
    name = 'xxx'
}

// XXX (only added to show that publishing works)
apply plugin: 'maven-publish'
publishing {
    repositories {
        maven { url = 'file:///tmp/my-repo' }
    }
    publications {
        myPub(MavenPublication) {
            artifactId myArtifact.name
            artifact myArtifact
        }
    }
}

项目testB

settings.gradle:

Project testB

settings.gradle:

rootProject.name = 'testB'

if (System.getenv('LOCAL_COMPILATION') == 'true') {
    // XXX (added a dependency substitution to let Gradle know that
    //      "org.test:xxx" corresponds to the testA project)
    includeBuild('../testA') {
        dependencySubstitution {
            substitute module('org.test:xxx') with project(':')
        }
    }
}

build.gradle:

if (System.getenv('LOCAL_COMPILATION') != 'true') {
    repositories {
        // XXX (only changed to show that resolution still works after
        //      publishing)
        maven { url = 'file:///tmp/my-repo' }
    }
}

configurations {
    magic
}

dependencies {
    magic 'org.test:xxx:0.0.0.+@zip'
}

task ultimateZip (type: Zip) {
    from configurations.magic
    archiveName = 'ultimate.zip'
    destinationDir = file("${buildDir}/ultimate-test")
}


根据注释中的要求,以下是有关创建的default配置和项目testA中添加的工件的更多说明.


As requested in the comments, here’s some more explanation on the created default configuration and the added artifact in project testA.

Gradle中的综合内部版本当前具有以下局限性:替换项目依赖项将始终指向目标项目的default配置" .在您的示例中,这意味着testA需要以default配置发布.因此,我们首先创建default配置.请注意,某些插件(例如java)已经创建了此配置.使用此类插件时,您无需自己创建它.

Composite builds in Gradle currently have the limitation that substituted project dependencies "will always point to the default configuration of the target project". In your example this means that testA needs to publish in the default configuration. We thus first create the default configuration. Note that some plugins (like java) already create this configuration; you don’t need to create it yourself when using such plugins.

似乎在任何地方都没有明确提及,但是您似乎已经发现自己了,项目的PublishedArtifact(用project.artifacts声明)对于Gradle确定依赖关系接线很重要.在复合版本中.因此,我们确保使用

It doesn’t seem to be mentioned explicitly anywhere but as you seem to have found out yourself already, the PublishedArtifacts of a project (as declared with project.artifacts) are important for Gradle to figure out the dependency wiring in composite builds. Hence, we make sure to declare such a PublishedArtifact in testA using this API. The artifact (e.g., its extension) is configured based on the properties of the someZip task. The name seems to not be taken from the someZip task in your example because you manually set archiveName; hence we need to explicitly declare it. If you use archiveBaseName = 'xxx' in someZip instead, then you don’t need the closure when adding the artifact.

这篇关于定义工件以用作另一个项目中的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆