第五十一章. 发布构件

Chapter 51. Publishing artifacts

本章介绍的是在Gradle 1.0版本中的原始的发布机制:在Gradle 1.3 中我们引入了新的发布机制。这种新机制现在还在孵化中而尚未完成,它引入了一些新的概念和特性,它们将使 Gradle 的发布功能更强大。
This chapter describes the original publishing mechanism available in Gradle 1.0: in Gradle 1.3 a new mechanism for publishing was introduced. While this new mechanism is incubating and not yet complete, it introduces some new concepts and features that do (and will) make Gradle publishing even more powerful.

你可以在 64 章,Ivy 发布(新) 65 章, Maven 发布(新)中读到有关新发布插件的内容。请尝试它们,并向我们反馈。
You can read about the new publishing plugins in Chapter 64, Ivy Publishing (new) and Chapter 65, Maven Publishing (new). Please try them out and give us feedback.

51.1. 介绍

51.1. Introduction

本章介绍如何声明你的项目的输出构件,以及如何使用它们(例如上传它们)。我们把项目的构件定义为项目向外界提供的文件。它可能是一个库,一个 ZIP 文件,或者是任何其他文件。一个项目可以发布为它想要的所有构件。
This chapter is about how you declare the outgoing artifacts of your project, and how to work with them (e.g. upload them). We define the artifacts of the projects as the files the project provides to the outside world. This might be a library or a ZIP distribution or any other file. A project can publish as many artifacts as it wants.

51.2. 构件和配置

51.2. Artifacts and configurations

像依赖一样,构件也按配置进行分组。事实上,配置可以同时包含构件和依赖。
Like dependencies, artifacts are grouped by configurations. In fact, a configuration can contain both artifacts and dependencies at the same time.

对于你的项目中的每一个配置,Gradle 提供了uploadConfigurationNamebuildConfigurationName任务。[18]这些任务的执行将会构建或上传属于各自配置的构件。
For each configuration in your project, Gradle provides the tasks uploadConfigurationName and buildConfigurationName. [18] Execution of these tasks will build or upload the artifacts belonging to the respective configuration.

表 23.5,“Java 插件 - 依赖配置”显示了 Java 插件添加的配置。这两个配置都与构件的使用有关。archives 配置是一个指定你的构件的标准配置。Java 插件会自动地把默认的 jar 文件指定到这个配置中。我们将在 51.5 节,“详谈项目库”中更多地对runtime配置进行讨论。作为依赖,你可以如你喜欢尽可能多地定义自定义配置,并把构件指向它们。
Table Table 23.5, “Java plugin - dependency configurations” shows the configurations added by the Java plugin. Two of the configurations are relevant for the usage with artifacts. The archives configuration is the standard configuration to assign your artifacts to. The Java plugin automatically assigns the default jar to this configuration. We will talk more about the runtime configuration in Section 51.5, “More about project libraries”. As with dependencies, you can declare as many custom configurations as you like and assign artifacts to them.

51.3. 声明构件

51.3. Declaring artifacts

51.3.1. Archive task artifacts

51.3.1. Archive task artifacts

你可以使用一个archive任务来定义构件:
You can use an archive task to define an artifact:

示例 51.1. 使用archive 任务定义一个构件 - Example 51.1. Defining an artifact using an archive task

build.gradle

task myJar(type: Jar)

artifacts {
    archives myJar
}

重点注意:你所创建的作为构建中的一部分的自定义archives,不会自动地指向任何的配置。你必须进行显示的指定。
It is important to note that the custom archives you are creating as part of your build are not automatically assigned to any configuration. You have to explicitly do this assignment.

51.3.2. 文件构件

51.3.2. File artifacts

你也可以使用一个文件来定义构件:
You can also use a file to define an artifact:

示例 51.2. 使用文件定义构件 - Example 51.2. Defining an artifact using a file

build.gradle

def someFile = file('build/somefile.txt')

artifacts {
    archives someFile
}

Gradle将基于文件的名字找出构件的属性。你可以自定义这些属性:
Gradle will figure out the properties of the artifact based on the name of the file. You can customize these properties:

示例 51.3. 自定义构件 - Example 51.3. Customizing an artifact

build.gradle

task myTask(type:  MyTaskType) {
    destFile = file('build/somefile.txt')
}

artifacts {
    archives(myTask.destFile) {
        name 'my-artifact'
        type 'text'
        builtBy myTask
    }
}

有一个基于map的语法来定义一个构件使用文件。这个 map 必须包含一个定义文件的 file 条目。这个map可能包含其他的构件属性:
There is a map-based syntax for defining an artifact using a file. The map must include a file entry that defines the file. The map may include other artifact properties:

示例 51.4. 基于map的语法定义一个文件构件。 - Example 51.4. Map syntax for defining an artifact using a file

build.gradle

task generate(type:  MyTaskType) {
    destFile = file('build/somefile.txt')
}

artifacts {
    archives file: generate.destFile, name: 'my-artifact', type: 'text', builtBy: generate
}

51.4 发布构件

51.4. Publishing artifacts

我们说过,每一个配置都有一个特定的上传任务。但是在你可以上传之前,你必须配置上传任务,并定义发布这些构件的位置。你所定义的仓库(第 50.6 节,“仓库”所描述的)并不会自动地用于上传。实际上,有些仓库只允许下载构件。以下是一个有关配置上传任务的例子:
We have said that there is a specific upload task for each configuration. But before you can do an upload, you have to configure the upload task and define where to publish the artifacts to. The repositories you have defined (as described in Section 50.6, “Repositories”) are not automatically used for uploading. In fact, some of those repositories allow only for artifact downloading. Here is an example how you can configure the upload task of a configuration:

示例 51.5. 上传任务的配置 - Example 51.5. Configuration of the upload task

build.gradle

repositories {
    flatDir {
        name "fileRepo"
        dirs "repo"
    }
}

uploadArchives {
    repositories {
        add project.repositories.fileRepo
        ivy {
            credentials {
                username "username"
                password "pw"
            }
            url "http://repo.mycompany.com"
        }
    }
}

正如你所见,你可以使用一个现有的仓库的引用,或者创建一个新的仓库。如50.6.8节,“更多关于Ivy解析器的信息”中所述,你可以使用符合上传的所有Ivy解析器。
As you can see, you can either use a reference to an existing repository or create a new repository. As described in Section 50.6.8, “More about Ivy resolvers”, you can use all the Ivy resolvers suitable for the purpose of uploading.

如果一个上传仓库中定义了多个模式,Gradle 必须为每个文件选择上传模式。默认情况下,Gradle 将上传到由url参数以及可选的layout参数所联合定义的模式。如果没有提供url参数,Gradle 将使用第一个定义的artifactPattern来上传,或者对于上传Ivy文件而言,将使用第一个定义的ivyPattern,如果这个参数有设置。
If an upload repository is defined with multiple patterns, Gradle must choose a pattern to use for uploading each file. By default, Gradle will upload to the pattern defined by the url parameter, combined with the optional layout parameter. If no url parameter is supplied, then Gradle will use the first defined artifactPattern for uploading, or the first defined ivyPattern for uploading Ivy files, if this is set.

关于上传到Maven仓库的内容,在52.6节,“与Maven仓库的交互”中描述。
Uploading to a Maven repository is described in Section 52.6, “Interacting with Maven repositories”.

51.5. 更多关于项目库的内容

51.5. More about project libraries

如果你的项目是作为一个库使用,你需要定义这个库的构件是什么,以及这些构件的依赖。Java 插件已经为此添加了一个runtime配置,它隐式的假设runtime依赖项是你想要发布的构件的依赖。当然,这是可完全自定义的。你可以添加你自己的自定义配置,或者让现有配置继承自扩展配置。你可以有不同组的构件,它们有不同的依赖。这一机制非常强大和灵活。
If your project is supposed to be used as a library, you need to define what are the artifacts of this library and what are the dependencies of these artifacts. The Java plugin adds a runtime configuration for this purpose, with the implicit assumption that the runtime dependencies are the dependencies of the artifact you want to publish. Of course this is fully customizable. You can add your own custom configuration or let the existing configurations extend from other configurations. You might have different group of artifacts which have a different set of dependencies. This mechanism is very powerful and flexible.

如果其他人想要把你的项目作为一个库使用,她只需要在依赖配置上声明,使她的项目依赖你的库。一个Gradle依赖提供了configuration 属性来声明它。如果它没有指定,则会使用default 配置(参阅第 50.4.9节,“依赖配置”)。把一个项目作为类库使用,可能是因为多项目构建,也可能是从仓库中获取项目。在后者的情况下,这个仓库的 ivy.xml描述符应该包含所有必要的信息。如果你使用 Maven 仓库,由没有如上文所述的灵活性。有关如何发布到 Maven 仓库,请参阅第 52.6节,”与Maven 仓库的交互“
If someone wants to use your project as a library, she simply needs to declare on which configuration of the dependency to depend on. A Gradle dependency offers the configuration property to declare this. If this is not specified, the default configuration is used (see Section 50.4.9, “Dependency configurations”). Using your project as a library can either happen from within a multi-project build or by retrieving your project from a repository. In the latter case, an ivy.xml descriptor in the repository is supposed to contain all the necessary information. If you work with Maven repositories you don't have the flexibility as described above. For how to publish to a Maven repository, see the section Section 52.6, “Interacting with Maven repositories”.



[18] 确切地说,Base插件提供了这些任务。这个Base插件会在你使用 Java 插件时自动的添加进来。
[18] To be exact, the Base plugin provides those tasks. This plugin is automatically applied if you use the Java plugin.