本文禁止w3cschool转载!

翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc 。

本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2.0 。
更好的阅读体验请访问:http://gradledoc.githang.com/2.0/userguide/userguide.html 。
另外,Android 手机用户可通过我写的一个程序浏览文档,带缓存功能的,目前0.6.1版本兼容 Android 4.0.3以上系统,项目地址如下:
https://github.com/msdx/gradle-doc-apk

翻译不易,本文采用 CC BY-NC-SA 4.0 许可协议,转载请务必署名及注明本文在CSDN博客上的出处:

https://coder.blog.csdn.net/article/details/93536049

关于我对Gradle的翻译,以Github上的项目及http://gradledoc.githang.com 上的文档为准。如发现翻译有误的地方,将首先在以上两个地方更新。因时间精力问题,博客中发表的译文基本不会同步修改。

第四十七章. 构建初始化插件

Chapter 47. Build Init Plugin

构建初始化插件目前是一个试验性插件。注意,在以后的 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 构建初始化插件可以用于引导创建一个新的 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插件需要在 应用 在项目之后才能使用(请参阅 第 21.1节, 《应用插件》)。构建初始化插件是一个自动应用的插件,这意味着你不需要显式地应用该插件。 要使用这个插件,只需要在你想要创建Gradle构建的地方执行 init 任务。 在这里,你不需要创建一个“存根” 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

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

表 47.1. 构建初始化插件——任务 - Table 47.1. Build Init plugin - tasks

47.2. 如何设置

47.2. What to set up

init 支持不同的构建设置 类型。类型通过提供 --type 参数值来指定。例如,要创建Java库项目,只需执行: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文件。它仅用于当调用 init 任务的目录有一个有效的“pom.xml”文件的时候。如果存在一个这样的文件,那么将会自动推断类型。 
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 运行器
    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.

08-09 03:31