Gradle學習二(Gradle構建)

初始化項目

創建文件夾

❯ mkdir basic-demo
❯ cd basic-demo

執行初始化命令

D:\study\gradle\basic-demo>gradle init
Starting a Gradle Daemon (subsequent builds will be faster)

Select type of project to generate:
  1: basic
  2: cpp-application
  3: cpp-library
  4: groovy-application
  5: groovy-library
  6: java-application
  7: java-library
  8: kotlin-application
  9: kotlin-library
  10: scala-library
Enter selection (default: basic) [1..10] 1

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: groovy) [1..2] 1

Project name (default: basic-demo):


BUILD SUCCESSFUL in 27s
2 actionable tasks: 2 executed
├── build.gradle  1
├── gradle
│   └── wrapper 
│       ├── gradle-wrapper.jar  2
│       └── gradle-wrapper.properties   3
├── gradlew  4
├── gradlew.bat  5
└── settings.gradle  6

 

1 Gradle build script for configuring the current project
2 Gradle Wrapper executable JAR
3 Gradle Wrapper configuration properties
4 Gradle Wrapper script for Unix-based systems
5 Gradle Wrapper script for Windows
6 Gradle settings script for configuring the Gradle build

創建一個任務

準備

mkdir src
echo Hello,World! > src/myfile.txt

build.gradle

task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}

定義一個名字叫copy的任務,把src目錄下的內容複製到dest目錄下!

執行任務:

D:\study\gradle\basic-demo>gradlew copy

BUILD SUCCESSFUL in 7s
1 actionable task: 1 executed
├─dest
│      myfile.txt
│
├─gradle
│  └─wrapper
│          gradle-wrapper.jar
│          gradle-wrapper.properties
│
└─src
        myfile.txt

gradle構建命令

  • 查看可用的任務
D:\study\gradle\basic-demo>gradlew tasks

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Custom tasks
------------
copy - Copies sources to the dest directory

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'basic-demo'.
components - Displays the components produced by root project 'basic-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'basic-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.
dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'basic-demo'. [incubating]
projects - Displays the sub-projects of root project 'basic-demo'.
properties - Displays the properties of root project 'basic-demo'.
tasks - Displays the tasks runnable from root project 'basic-demo'.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
  • 查看可用的屬性信息
D:\study\gradle\basic-demo>gradlew properties

> Task :properties

------------------------------------------------------------
Root project
------------------------------------------------------------

allprojects: [root project 'basic-demo']
ant: org.gradle.api.internal.project.DefaultAntBuilder@3e067c13
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@55ff944
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@2de8be92
asDynamicObject: DynamicObject for root project 'basic-demo'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@4a8e4ccc
buildDir: D:\study\gradle\basic-demo\build
buildFile: D:\study\gradle\basic-demo\build.gradle
buildPath: :
buildScriptSource: org.gradle.groovy.scripts.TextResourceScriptSource@4765476a
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@3d8a031b
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@557e3616
components: SoftwareComponentInternal set
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@6daee859
configurationTargetIdentifier: org.gradle.configuration.ConfigurationTargetIdentifier$1@9dbc91
configurations: configuration container
convention: org.gradle.api.internal.plugins.DefaultConvention@6d62e542
copy: task ':copy'
defaultTasks: []
deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@4988b07e
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@53fe6536
dependencyLocking: org.gradle.internal.locking.DefaultDependencyLockingHandler_Decorated@3eea22ec
depth: 0
description: null
displayName: root project 'basic-demo'
ext: org.gradle.internal.extensibility.DefaultExtraPropertiesExtension@44589886
extensions: org.gradle.api.internal.plugins.DefaultConvention@6d62e542
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@48dfcb33
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@68a8c0cb
gradle: build 'basic-demo'
group:
identityPath: :
inheritedScope: org.gradle.internal.extensibility.ExtensibleDynamicObject$InheritedDynamicObject@73a6270
layout: org.gradle.api.internal.file.DefaultProjectLayout@7a646b9
listenerBuildOperationDecorator: org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator@3196832d
logger: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@1f96b34c
logging: org.gradle.internal.logging.services.DefaultLoggingManager@4beb6b2c
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@7ecd6f66
modelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@7bc70479
module: org.gradle.api.internal.artifacts.ProjectBackedModule@a1c3472
mutationState: project :
name: basic-demo
normalization: org.gradle.normalization.internal.DefaultInputNormalizationHandler_Decorated@51e1c49e
objects: org.gradle.api.internal.model.DefaultObjectFactory@41c36785
parent: null
parentIdentifier: null
path: :
pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@54608684
plugins: [org.gradle.buildinit.plugins.BuildInitPlugin@1747a4bd, org.gradle.buildinit.plugins.WrapperPlugin@485d47ff, org.gradle.api.plugins.HelpTasksPlugin@12f8fe3e]
processOperations: org.gradle.api.internal.file.DefaultFileOperations@48dfcb33
project: root project 'basic-demo'
projectConfigurator: org.gradle.api.internal.project.BuildOperationCrossProjectConfigurator@42fc0e43
projectDir: D:\study\gradle\basic-demo
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@683428ed
projectPath: :
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@4af4e8ab
properties: {...}
providers: org.gradle.api.internal.provider.DefaultProviderFactory@1f28e6c5
repositories: repository container
resourceLoader: org.gradle.internal.resource.transfer.DefaultUriTextResourceLoader@3d046d75
resources: org.gradle.api.internal.resources.DefaultResourceHandler@743e5978
rootDir: D:\study\gradle\basic-demo
rootProject: root project 'basic-demo'
script: false
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@196256e4
scriptPluginFactory: org.gradle.configuration.ScriptPluginFactorySelector@41a495c3
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@5fd53747
services: ProjectScopeServices
standardOutputCapture: org.gradle.internal.logging.services.DefaultLoggingManager@4beb6b2c
state: project state 'EXECUTED'
status: release
subprojects: []
tasks: task set
version: unspecified

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章