和Maven一樣,Gradle只是提供了構建項目的一個框架,真正起作用的是Plugin。Gradle在默認情況下為我們提供了許多常用的Plugin,其中包括有構建java項目的Plugin,還有War,Ear等。 與Maven不同的是,Gradle不提供內建的項目生命周期管理,只是java Plugin向PRoject中添加了許多Task,這些Task依次執行,為我們營造了一種如同Maven般項目構建周期。https://gradle.org/
Ubuntu安裝Gradle比較簡單, 直接使用apt安裝即可
sudo apt-get install gradle或者從官網下載, 下載完記得配置下環境變量 http://www.gradle.org/downloads
Gradle執行對象主要有task和project,它們提供了執行的上下文.所有的Plugin要么向Project中添加用于配置的Property,要么向Project中添加不同的Task。一個Task表示一個邏輯上較為獨立的執行過程,比如編譯Java源代碼,拷貝文件,打包Jar文件,甚至可以是執行一個系統命令或者調用Ant。另外,一個Task可以讀取和設置Project的Property以完成特定的操作。
在這個目錄下 使用gradle helloWorld
執行這個task
執行結果
Parallel execution with configuration on demand is an incubating feature.:helloWorldhello worldBUILD SUCCESSFULTotal time: 0.525 secs這里的helloWorld是一個DefaultTask類型的對象,這也是定義一個Task時的默認類型,當然我們也可以顯式地聲明Task的類型,甚至可以自定義一個Task類型(我們將在本系列的后續文章中講到)。
ask之間可以存在依賴關系,比如taskA依賴于taskB,那么在執行taskA時,Gradle會先執行taskB,然后再執行taskA。聲明Task依賴關系的一種方式是在定義一個Task的時候:
task taskA(dependsOn: taskB) { //do something}使用gradle tasks
查看所有的task,包括默認的和自己定義的。
Gradle在默認情況下為我們提供了幾個常用的Task,比如查看Project的Properties、顯示當前Project中定義的所有Task等。可以通過一下命令查看Project中所有的Task: 輸出結果
$ gradle tasksParallel execution with configuration on demand is an incubating feature.:tasks------------------------------------------------------------All tasks runnable from root project------------------------------------------------------------Build Setup tasks-----------------init - Initializes a new Gradle build. [incubating]wrapper - Generates Gradle wrapper files. [incubating]Help tasks----------buildEnvironment - Displays all buildscript dependencies declared in root project 'helloWorld'.components - Displays the components produced by root project 'helloWorld'. [incubating]dependencies - Displays all dependencies declared in root project 'helloWorld'.dependencyInsight - Displays the insight into a specific dependency in root project 'helloWorld'.help - Displays a help message.model - Displays the configuration model of root project 'helloWorld'. [incubating]projects - Displays the sub-projects of root project 'helloWorld'.properties - Displays the properties of root project 'helloWorld'.tasks - Displays the tasks runnable from root project 'helloWorld'.Other tasks-----------copyFilehelloWorldTo see all tasks and more detail, run gradle tasks --allTo see more detail about a task, run gradle help --task <task>BUILD SUCCESSFULTotal time: 0.542 secs可以看到,除了我們自己定義的copyFile和helloWorld之外,Gradle還默認為我們提供了dependencies、projects和properties等Task。dependencies用于顯示Project的依賴信息,projects用于顯示所有Project,包括根Project和子Project,而properties則用于顯示一個Project所包含的所有Property。
在默認情況下,Gradle已經為Project添加了很多Property,我們可以調用以下命令進行查看:
gradle properties$ gradle propertiesParallel execution with configuration on demand is an incubating feature.:properties------------------------------------------------------------Root project------------------------------------------------------------allprojects: [root project 'helloWorld']ant: org.gradle.api.internal.project.DefaultAntBuilder@400558b1antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@399cf3cartifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@232287c7asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@28172472baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@42bd3111buildDir: /media/yangtianrui/system/MyGradleLearning/helloWorld/buildbuildFile: /media/yangtianrui/system/MyGradleLearning/helloWorld/build.gradlebuildScriptSource: org.gradle.groovy.scripts.UriScriptSource@f90f655buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@38bb44dfchildProjects: {}class: class org.gradle.api.internal.project.DefaultProject_DecoratedclassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@291ed30ecomponents: []configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@2805a85configurations: []convention: org.gradle.api.internal.plugins.DefaultConvention@aaa36dedefaultTasks: []deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@685506e8dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@370c3e4fdepth: 0description: nullext: org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@37d81e59extensions: org.gradle.api.internal.plugins.DefaultConvention@aaa36defileOperations: org.gradle.api.internal.file.DefaultFileOperations@6c1fd2bafileResolver: org.gradle.api.internal.file.BaseDirFileResolver@2c3dfb97gradle: build 'helloWorld'group: helloWorld: task ':helloWorld'inheritedScope: org.gradle.api.internal.ExtensibleDynamicObject$InheritedDynamicObject@7a954619logger: org.gradle.logging.internal.slf4j.OutputEventListenerBackedLogger@dc8b5c4logging: org.gradle.logging.internal.DefaultLoggingManager@1fd03cc5modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@5cf28debmodelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@c87ade2module: org.gradle.api.internal.artifacts.ProjectBackedModule@1ed8c289name: helloWorldorg.gradle.configureondemand: trueorg.gradle.daemon: trueorg.gradle.jvmargs: -Xmx4096m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8org.gradle.parallel: trueparent: nullparentIdentifier: nullpath: :pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@44a94f23plugins: [org.gradle.api.plugins.HelpTasksPlugin@61d2b87f]processOperations: org.gradle.api.internal.file.DefaultFileOperations@6c1fd2baproject: root project 'helloWorld'projectDir: /media/yangtianrui/system/MyGradleLearning/helloWorldprojectEvaluationBroadcaster: ProjectEvaluationListener broadcastprojectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@5a817567projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@12d42609properties: {...}repositories: []resources: org.gradle.api.internal.resources.DefaultResourceHandler@238d710crootDir: /media/yangtianrui/system/MyGradleLearning/helloWorldrootProject: root project 'helloWorld'scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@2dd3e209scriptPluginFactory: org.gradle.configuration.DefaultScriptPluginFactory@7c288099serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@4f7f157bservices: ProjectScopeServicesstandardOutputCapture: org.gradle.logging.internal.DefaultLoggingManager@1fd03cc5state: project state 'EXECUTED'status: releasesubprojects: []tasks: [task ':helloWorld', task ':properties']version: unspecifiedBUILD SUCCESSFULTotal time: 0.631 secs在以上Property中,allprojects表示所有的Project,這里只包含一個根Project,在多項目構建中,它將包含多個Project;buildDir表示構建結果的輸出目錄;我們自己定義的helloWorld和copyFile也成為了Project中的Property。另外,Project還包括用于執行Ant命令的DefaultAntBuilder(Property名為ant)和Project的描述屬性description。
感謝:http://www.cnblogs.com/davenkin/p/gradle-learning-1.html
新聞熱點
疑難解答