Gradle入门二——创建task

1.在项目的build.gradle中添加新的task

task hello1{
    doLast{
        println 'hello from other script'
    }
}

添加后如下:

plugins {
    id 'java'
    id 'war'
}

group 'com.test.gradle_01'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile group: 'junit', name: 'junit', version: '4.12'

    // 添加  Apache Commons Lang 依赖
    compile('org.apache.commons:commons-lang3:3.5')
      

}

task hello1{
    doLast{
        println 'hello from other script'
    }
}

2.执行命令

gradle tasks --all

显示所有的task,新加入的hello1 task在other中:

PS E:\软件\sts-workspace\gradle_01> gradle tasks --all

> Configure project :
configuring root project 'gradle_01'

> Task :tasks

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

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
war - Generates a war archive with all the compiled classes, the web-app content and the libraries.

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

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

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

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
compileJava - Compiles main Java source.
compileTestJava - Compiles test Java source.
hello
hello1
prepareKotlinBuildScriptModel
processResources - Processes main resources.
processTestResources - Processes test resources.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

3.如果一个gradle文件太大,可以进行拆分,在build.gradle中加入拆分的文件,如下:

apply from:'other.gradle'

4.创建的other.gradle和build.gradle在同一个目录中,并添加如下内容:

println "configuring $project"

task hello{
    doLast{
        println 'hello from other script'
    }
}

def showMyName(){
    'i am a boy'
}

ext{
    showName = showMyName()
}

5.再执行gradle hello 命令,输出如下:

PS E:\软件\sts-workspace\gradle_01> gradle hello

> Configure project :
configuring root project 'gradle_01'

> Task :hello
hello from other script

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

6.查看属性

gradle properties

输出结果为:

PS E:\软件\sts-workspace\gradle_01> gradle properties

> Configure project :
configuring root project 'gradle_01'

> Task :properties

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

allprojects: [root project 'gradle_01']
ant: org.gradle.api.internal.project.DefaultAntBuilder@eebb915
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@6a7646b1
archivesBaseName: gradle_01
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@3948679b
asDynamicObject: DynamicObject for root project 'gradle_01'
autoTargetJvmDisabled: false
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@4357478d
buildDir: E:\软件\sts-workspace\gradle_01\build
buildFile: E:\软件\sts-workspace\gradle_01\build.gradle
buildPath: :
buildScriptSource: org.gradle.groovy.scripts.TextResourceScriptSource@10277de3
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@483cf298
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@32b230bb
components: SoftwareComponentInternal set
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@4b20624f
configurationTargetIdentifier: org.gradle.configuration.ConfigurationTargetIdentifier$1@60c3c6ad
configurations: configuration container
convention: org.gradle.api.internal.plugins.DefaultConvention@4fc4501f
defaultArtifacts: org.gradle.api.internal.plugins.DefaultArtifactPublicationSet_Decorated@6cd096b4
defaultTasks: []
deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@24aaa37b
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@5a22efed
dependencyLocking: org.gradle.internal.locking.DefaultDependencyLockingHandler_Decorated@378b181e
depth: 0
description: null
displayName: root project 'gradle_01'
distsDir: E:\软件\sts-workspace\gradle_01\build\distributions
distsDirName: distributions
docsDir: E:\软件\sts-workspace\gradle_01\build\docs
docsDirName: docs
ext: org.gradle.internal.extensibility.DefaultExtraPropertiesExtension@5f864d6d
extensions: org.gradle.api.internal.plugins.DefaultConvention@4fc4501f
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@2ec68c9d
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@6356432f
gradle: build 'gradle_01'
group: com.test.gradle_01
hello: task ':hello'
hello1: task ':hello1'
identityPath: :
inheritedScope: org.gradle.internal.extensibility.ExtensibleDynamicObject$InheritedDynamicObject@c72ecb3
java: org.gradle.api.plugins.internal.DefaultJavaPluginExtension_Decorated@61f22575
layout: org.gradle.api.internal.file.DefaultProjectLayout@7bcaefb2
libsDir: E:\软件\sts-workspace\gradle_01\build\libs
libsDirName: libs
listenerBuildOperationDecorator: org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator@2a5484df
logger: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@7f3064ce
logging: org.gradle.internal.logging.services.DefaultLoggingManager@31d8e252
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@41f1a091
modelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@6d3c9e35
module: org.gradle.api.internal.artifacts.ProjectBackedModule@761e8e7f
mutationState: project :
name: gradle_01
normalization: org.gradle.normalization.internal.DefaultInputNormalizationHandler_Decorated@6dea6062
objects: org.gradle.api.internal.model.DefaultObjectFactory@6316737c
parent: null
parentIdentifier: null
path: :
pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@15aac73e
plugins: [org.gradle.buildinit.plugins.BuildInitPlugin@59931832, org.gradle.buildinit.plugins.WrapperPlugin@67c49a12, org.gradle.api.plugins.HelpTasksPlugin@37998805, org.gradle.language.base.plugins.LifecycleBasePlugin@74493aff, org.gradle.api.plugins.BasePlugin@355afecc, org.gradle.api.plugins.ReportingBasePlugin@760df67f, org.gradle.api.plugins.JavaBasePlugin@daf911b, org.gradle.api.plugins.JavaPlugin@308cdc93, org.gradle.api.plugins.WarPlugin@5242e8d0]
processOperations: org.gradle.process.internal.DefaultExecActionFactory$DecoratingExecActionFactory@4b6552e9
project: root project 'gradle_01'
projectConfigurator: org.gradle.api.internal.project.BuildOperationCrossProjectConfigurator@3ab325d7
projectDir: E:\软件\sts-workspace\gradle_01
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@6c8d0e56
projectPath: :
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@29cc6f7f
properties: {...}
providers: org.gradle.api.internal.provider.DefaultProviderFactory@1fce1630
publicType: org.gradle.api.plugins.BasePluginConvention
reporting: org.gradle.api.reporting.ReportingExtension_Decorated@48df82b5
reportsDir: E:\软件\sts-workspace\gradle_01\build\reports
repositories: repository container
resourceLoader: org.gradle.internal.resource.transfer.DefaultUriTextResourceLoader@6be19be4
resources: org.gradle.api.internal.resources.DefaultResourceHandler@6f1ad770
rootDir: E:\软件\sts-workspace\gradle_01
rootProject: root project 'gradle_01'
script: false
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@25e7fa7b
scriptPluginFactory: org.gradle.configuration.ScriptPluginFactorySelector@11191e58
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@76409924
services: ProjectScopeServices
showName: i am a boy
sourceCompatibility: 1.8
sourceSets: SourceSet container
standardOutputCapture: org.gradle.internal.logging.services.DefaultLoggingManager@31d8e252
state: project state 'EXECUTED'
status: integration
subprojects: []
targetCompatibility: 1.8
tasks: task set
testReportDir: E:\软件\sts-workspace\gradle_01\build\reports\tests
testReportDirName: tests
testResultsDir: E:\软件\sts-workspace\gradle_01\build\test-results
testResultsDirName: test-results
version: 1.0-SNAPSHOT
webAppDir: E:\软件\sts-workspace\gradle_01\src\main\webapp
webAppDirName: src/main/webapp

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

猜你喜欢

转载自blog.csdn.net/zht245648124/article/details/97616225