第四十七章. Build Init 插件

Chapter 47. Build Init Plugin

Build Init 插件目前仍然是孵化状态。请务必注意,在以后的 Gradle 版本中,相关的 DSL 和其他配置可能会有所改变。
The Build Init plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions.

Gradle Build Init 插件可以用于引导创建一个新的 Gradle 构建的过程。它支持创建不同类型的全新项目,以及将现有的构建 (如 Apache Maven 构建) 转换成 Gradle 构建。
The Gradle Build Init plugin can be used to bootstrap the process of creating a new Gradle build. It supports creating brand new projects of different types as well as converting existing builds (e.g. An Apache Maven build) to be Gradle builds.

Gradle 插件通常需要被应用于 project 中才可以使用(见第 21.1节,"应用插件")。 Build Init 插件是一个自动应用的插件,这意味着您不需要显式地应用它。如果要使用该插件,只需要在你想要的地方,执行名为init 的任务,就可以创建 Gradle 构建。那里不需要创建一个“存根” build.gradle文件以应用该插件。
Gradle plugins typically need to be applied to a project before they can be used (see Section 21.1, “Applying plugins”). The Build Init plugin is an automatically applied plugin, which means you do not need to apply it explicitly. To use the plugin, simply execute the task named init where you would like to create the Gradle build. There is no need to create a “stub” build.gradle file in order to apply the plugin.

它还利用了 Wrapper 插件的 wrapper 任务(见第 48章,Wrapper 插件),这意味着 Gradle Wrapper 也将被安装到该项目。
It also leverages the wrapper task from the Wrapper plugin (see Chapter 48, Wrapper Plugin), which means that the Gradle Wrapper will also be installed into the project.

47.1. 任务

47.1. Tasks

该插件向project 中添加了以下任务:
The plugin adds the following tasks to the project:

表 47.1. Build Init 插件 - 任务 - Table 47.1. Build Init plugin - tasks

任务名称
Task name
依赖于
Depends on
类型
Type
描述
Description
init wrapper InitBuild 生成一个 Gradle 项目。
Generates a Gradle project.
wrapper - Wrapper 生成 Gradle wrapper 文件。
Generates Gradle wrapper files.

47.2. 要设置什么

47.2. What to set up

init支持不同的构建安装程序类型。类型通过提供一个 --type 参数值指定。例如,要创建一个 Java library 项目,只需执行: gradle init --type java-library
The init supports different build setup types. The type is specified by supplying a --type argument value. For example, to create a Java library project simply execute: gradle init --type java-library.

如果没有提供 --type 参数,Gradle 将尝试从当前环境来推断类型。例如,如果它发现pom.xml,它将推断一个“pom”的类型值,以转换为 Gradle 构建。
If a --type parameter is not supplied, Gradle will attempt to infer the type from the environment. For example, it will infer a type value of "pom" if it finds a pom.xml to convert to a Gradle build.

如果无法推断类型,将使用“basic”类型。
If the type could not be inferred, the type "basic" will be used.

所有构建安装类型都包括 Gradle Wrapper 的安装。
All build setup types include the setup of the Gradle Wrapper.

47.3. 构建初始化的类型

47.3. Build init types

由于这个插件目前仍是 孵化状态,当前仅支持3个构建初始化类型。在未来的 Gradle 发布中,将会添加更多类型。

47.3.1. “pom”(Maven 转换)

47.3.1. "pom" (Maven conversion)

pom”类型可以被用于把 Apache Maven 构建转换为 Gradle 构建。这是通过将 POM 转换为一个或多个 Gradle 文件。它仅用于当有一个有效的“pom.xml”文件在 init 任务被调用的目录的时候。如果存在这样的文件,将会自动推断此类型。
The "pom" type can be used to convert an Apache Maven build to a Gradle build. This works by converting the POM to one or more Gradle files. It is only able to be used if there is a valid "pom.xml" file in the directory that the init task is invoked in. This type will be automatically inferred if such a file exists.

Maven 转换实现最初由 Gradle 社区成员开发的maven2gradle tool启发。
The Maven conversion implementation was inspired by the maven2gradle tool that was originally developed by Gradle community members.

该转换过程具有以下特点:
The conversion process has the following features:

  • 使用有效的 POM 和有效设置(支持 POM 继承、 依赖管理、 属性)
    Uses effective POM and effective settings (support for POM inheritance, dependency management, properties)
  • 支持单个模块和多模块项目
    Supports both single module and multimodule projects
  • 支持自定义模块名称 (即目录名称有所不同)
    Supports custom module names (that differ from directory names)
  • 生成通用元数据 - id,description 和 version
    Generates general metadata - id, description and version
  • 应用maven,java 和 war 插件(如果需要)
    Applies maven, java and war plugins (as needed)
  • 如果需要,支持把 war 项目打包为 jar 包
    Supports packaging war projects as jars if needed
  • 生成依赖 (包括外部的和模块间的)
    Generates dependencies (both external and inter-module)
  • 生成下载仓库(包括本地 Maven 存储库)
    Generates download repositories (inc. local Maven repository)
  • 调整 java 编译器设置
    Adjusts java compiler settings
  • 支持打包源代码和测试代码
    Supports packaging of sources and tests
  • 支持 TestNG runner
    Supports TestNG runner
  • 从 Maven enforcer 插件设置生成全局的 exclusions
    Generates global exclusions from Maven enforcer plugin settings

47.3.2. “java-library

47.3.2. "java-library"

Java-library”构建初始化类型不是可推断的。它必须显式指定。
The "java-library" build init type is not inferable. It must be explicitly specified.

它具有以下特点:
It has the following features:

  • 使用“java”插件
    Uses the "java" plugin
  • 使用“mavenCentral()”依赖仓库
    Uses the " mavenCentral() dependency repository
  • 使用JUnit测试
    Uses JUnit for testing
  • 在源代码中的约定位置有目录
    Has directories in the conventional locations for source code
  • 如果不存在源文件或测试文件,会包含一个示例类和单元测试
    Contains a sample class and unit test, if there are no existing source or test files

47.3.3. “scala-library

47.3.3. "scala-library"

scala-library”构建初始化类型不是可推断的。它必须显式指定。
The "scala-library" build init type is not inferable. It must be explicitly specified.

它具有以下特点:
It has the following features:

  • 使用“scala”插件
    Uses the "scala" plugin
  • 使用“mavenCentral()”依赖仓库
    Uses the " mavenCentral() dependency repository
  • 使用 Scala 2.10
    Uses Scala 2.10
  • 使用ScalaTest作为测试
    Uses ScalaTest is used for testing
  • 在源代码中的约定位置有目录
    Has directories in the conventional locations for source code
  • 如果不存在源文件或测试文件,会包含一个示例 scala 类和一个相应的 ScalaTest 测试套件
    Contains a sample scala class and an according ScalaTest test suite, if there are no existing source or test files

47.3.4. “groovy-library

47.3.4. "groovy-library"

groovy-library”构建初始化类型不是可推断的。它必须显式指定。
The "groovy-library" build init type is not inferable. It must be explicitly specified.

它具有以下特点:
It has the following features:

  • 使用“groovy”插件
    Uses the "groovy" plugin
  • 使用“mavenCentral()”依赖仓库
    Uses the " mavenCentral() dependency repository
  • 使用 Groovy 2.x
    Uses Groovy 2.x
  • 测试使用 Spock testing framework
    Uses Spock testing framework for testing
  • 在源代码中的约定位置有目录
    Has directories in the conventional locations for source code
  • 如果不存在源文件或测试文件,会包含一个示例 groovy 类和一个相应的 Spock 规范
    Contains a sample groovy class and an according Spock specification, if there are no existing source or test files

47.3.5. “basic”

47.3.5. "basic"

basic”构建初始化类型用于创建一个新的 Gradle 项目。它将创建一个示例build.gradle文件,并包含注释和链接以帮助你开始。
The "basic" build init type is useful for creating a fresh new Gradle project. It creates a sample build.gradle file, with comments and links to help get started.

如果没有显示地指定一个类型,或者无法推断出类型时,会使用该类型。
This type is used when no type was explicitly specified, and no type could be inferred.