gradle学习(9)-获取build相关信息

最后更新于:2022-04-01 06:51:39

**(以下所有内容都是根据[官方文档](https://www.gradle.org/docs/current/userguide/userguide.pdf)学习。如果你对gradle有兴趣,希望你能直接学习官方文档,官方文档是最权威的。而且这样才能产生2份有意义的学习资料)** ##1.列举当前目录下的所有项目,包括子项目 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q projects ------------------------------------------------------------ Root project ------------------------------------------------------------ Root project 'multproject' +--- Project ':api' +--- Project ':services' | +--- Project ':services:shared' | \--- Project ':services:webservice' \--- Project ':shared' To see a list of the tasks of a project, run gradle <project-path>:tasks For example, try running gradle :api:tasks ~~~ 在gradle后加projects,就可以列举出所有的项目,从上面的结果可以看出来。该目录的根目录为multproject。下面包括了api/services/shared三个子项目。其中services下面又包含了2个子项目shared/webservice。 ## 2.列举任务 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q tasks ------------------------------------------------------------ All tasks runnable from root project - The shared API for the application ------------------------------------------------------------ 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 classes 'main'. clean - Deletes the build directory. jar - Assembles a jar archive containing the main classes. testClasses - Assembles classes 'test'. Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Documentation tasks ------------------- javadoc - Generates Javadoc API documentation for the main source code. Help tasks ---------- components - Displays the components produced by root project 'multproject'. [incubating] dependencies - Displays all dependencies declared in root project 'multproject'. dependencyInsight - Displays the insight into a specific dependency in root project 'multproject'. help - Displays a help message. projects - Displays the sub-projects of root project 'multproject'. properties - Displays the properties of root project 'multproject'. tasks - Displays the tasks runnable from root project 'multproject' (some of the displayed tasks may belong to subprojects). IDE tasks --------- cleanEclipse - Cleans all Eclipse files. cleanEclipseWtp - Cleans Eclipse wtp configuration files. eclipse - Generates all Eclipse files. eclipseWtp - Generates Eclipse wtp configuration files. Verification tasks ------------------ check - Runs all checks. test - Runs the unit tests. To see all tasks and more detail, run with --all. ~~~ 如果想再多的信息可以在后面加上--all,不过还是不要加的好,因为实在太多了。 ## 3.搜索任务 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q help --task test Detailed task information for test Paths :api:test :services:test :shared:test :services:shared:test :services:webservice:test Type Test (org.gradle.api.tasks.testing.Test) Options --debug-jvm Enable debugging for the test process. The process is started suspended and listening on port 5005. [INCUBATING] --tests Sets test class or method name to be included, '*' is supported. Description Runs the unit tests. ~~~ 上面是以test为关键字搜索匹配项。 ## 4.列举依赖 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q dependencies api:dependencies ------------------------------------------------------------ Root project - The shared API for the application ------------------------------------------------------------ No configurations ------------------------------------------------------------ Project :api ------------------------------------------------------------ archives - Configuration for archive artifacts. No dependencies compile - Compile classpath for source set 'main'. \--- project :shared default - Configuration for default artifacts. \--- project :shared runtime - Runtime classpath for source set 'main'. \--- project :shared testCompile - Compile classpath for source set 'test'. +--- project :shared \--- junit:junit:4.11 &nbsp;&nbsp;&nbsp;&nbsp; \--- org.hamcrest:hamcrest-core:1.3 testRuntime - Runtime classpath for source set 'test'. +--- project :shared \--- junit:junit:4.11 &nbsp;&nbsp;&nbsp;&nbsp; \--- org.hamcrest:hamcrest-core:1.3 ~~~ 例举出了api项目的依赖信息。 ## 5.根据特定配置列举依赖 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q dependencies api:dependencies --configuration testCompile ------------------------------------------------------------ Root project - The shared API for the application ------------------------------------------------------------ No configurations ------------------------------------------------------------ Project :api ------------------------------------------------------------ testCompile - Compile classpath for source set 'test'. +--- project :shared \--- junit:junit:4.11 \--- org.hamcrest:hamcrest-core:1.3 ~~~ ## 6.获取插入依赖 获取项目中哪个任务依赖groovy的jar包 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q api:dependencyInsight --dependency groovy --configuration testCompile No dependencies matching given input were found in configuration ':api:testCompile' ~~~ ## 7.列举项目属性 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -q api:properties ------------------------------------------------------------ Project :api ------------------------------------------------------------ allprojects: [project ':api'] ant: org.gradle.api.internal.project.DefaultAntBuilder@51cbc6c2 antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@19661482 archivesBaseName: api artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@7ccc6fd4 asDynamicObject: org.gradle.api.internal.ExtensibleDynamicObject@51c2074e assemble: task ':api:assemble' baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@196dbbd1 binaries: [classes 'main', classes 'test'] build: task ':api:build' buildDependents: task ':api:buildDependents' buildDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build buildFile: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build.gradle buildNeeded: task ':api:buildNeeded' buildScriptSource: org.gradle.groovy.scripts.UriScriptSource@7dd26a3e buildTasks: [build] buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@13d4cfb check: task ':api:check' childProjects: {} class: class org.gradle.api.internal.project.DefaultProject_Decorated classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@5f88f35f classes: task ':api:classes' clean: task ':api:clean' cleanEclipse: task ':api:cleanEclipse' cleanEclipseClasspath: task ':api:cleanEclipseClasspath' cleanEclipseJdt: task ':api:cleanEclipseJdt' cleanEclipseProject: task ':api:cleanEclipseProject' cleanEclipseWtp: task ':api:cleanEclipseWtp' compileJava: task ':api:compileJava' compileTestJava: task ':api:compileTestJava' components: [org.gradle.api.internal.java.JavaLibrary@182212f9] configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@650ba5cf configurations: [configuration ':api:archives', configuration ':api:compile', configuration ':api:default', configuration ':api:runtime', configuration ':api:testCompile', configuration ':api:testRuntime'] convention: org.gradle.api.internal.plugins.DefaultConvention@5fb2ecdc defaultArtifacts: org.gradle.api.internal.plugins.DefaultArtifactPublicationSet_Decorated@5bdc9099 defaultTasks: [] dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@1446745 dependencyCacheDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/dependency-cache dependencyCacheDirName: dependency-cache depth: 1 description: null displayName: root project 'multproject' distsDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/distributions distsDirName: distributions docsDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/docs docsDirName: docs eclipse: org.gradle.plugins.ide.eclipse.model.EclipseModel_Decorated@6d995bec eclipseClasspath: task ':api:eclipseClasspath' eclipseJdt: task ':api:eclipseJdt' eclipseProject: task ':api:eclipseProject' eclipseWtp: task ':api:eclipseWtp' ext: org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@6f621b2a extensions: org.gradle.api.internal.plugins.DefaultConvention@5fb2ecdc fileOperations: org.gradle.api.internal.file.DefaultFileOperations@11e59b87 fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@7945c53e gradle: build 'multproject' group: multproject inheritedScope: org.gradle.api.internal.ExtensibleDynamicObject$InheritedDynamicObject@5398a7a5 jar: task ':api:jar' javadoc: task ':api:javadoc' libsDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/libs libsDirName: libs logger: org.gradle.api.logging.Logging$LoggerImpl@4b266c83 logging: org.gradle.logging.internal.DefaultLoggingManager@479e3300 mayImplementMissingMethods: false mayImplementMissingProperties: false modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@632669bb module: org.gradle.api.internal.artifacts.ProjectBackedModule@79bc5432 name: api parent: root project 'multproject' parentIdentifier: root project 'multproject' path: :api plugins: [org.gradle.language.base.plugins.LifecycleBasePlugin@73258ce6, org.gradle.api.plugins.BasePlugin@385ec662, org.gradle.api.plugins.ReportingBasePlugin@754348e8, org.gradle.language.base.plugins.LanguageBasePlugin@35677c16, org.gradle.api.plugins.LegacyJavaComponentPlugin@2c8c4d3f, org.gradle.api.plugins.JavaBasePlugin@7ccd7637, org.gradle.api.plugins.JavaPlugin@5b39746e, org.gradle.plugins.ide.eclipse.EclipsePlugin@211c9201, org.gradle.plugins.ide.eclipse.EclipseWtpPlugin@6783c490, org.gradle.api.plugins.HelpTasksPlugin@690f268] processOperations: org.gradle.api.internal.file.DefaultFileOperations@11e59b87 processResources: task ':api:processResources' processTestResources: task ':api:processTestResources' project: project ':api' projectDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api projectEvaluationBroadcaster: ProjectEvaluationListener broadcast projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@6ee7a6d7 projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@2a7aa623 properties: {...} rebuildTasks: [clean, build] reporting: org.gradle.api.reporting.ReportingExtension_Decorated@5a31599e reportsDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/reports repositories: [org.gradle.api.internal.artifacts.repositories.DefaultMavenArtifactRepository_Decorated@5df2c254] resources: org.gradle.api.internal.resources.DefaultResourceHandler@6cb448fb rootDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject rootProject: root project 'multproject' runtimeClasspath: file collection scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@30238e7e scriptPluginFactory: org.gradle.configuration.DefaultScriptPluginFactory@6e600ae2 serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$5@636ab40a services: ProjectScopeServices sourceCompatibility: 1.7 sourceSets: [source set 'main', source set 'test'] sources: [source set 'main', source set 'test'] standardOutputCapture: org.gradle.logging.internal.DefaultLoggingManager@479e3300 state: project state 'EXECUTED' status: integration subprojects: [] targetCompatibility: 1.7 tasks: [task ':api:assemble', task ':api:build', task ':api:buildDependents', task ':api:buildNeeded', task ':api:check', task ':api:classes', task ':api:clean', task ':api:cleanEclipse', task ':api:cleanEclipseClasspath', task ':api:cleanEclipseJdt', task ':api:cleanEclipseProject', task ':api:cleanEclipseWtp', task ':api:compileJava', task ':api:compileTestJava', task ':api:eclipse', task ':api:eclipseClasspath', task ':api:eclipseJdt', task ':api:eclipseProject', task ':api:eclipseWtp', task ':api:jar', task ':api:javadoc', task ':api:processResources', task ':api:processTestResources', task ':api:properties', task ':api:test', task ':api:testClasses'] test: task ':api:test' testClasses: task ':api:testClasses' testReportDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/reports/tests testReportDirName: tests testResultsDir: /Users/qianhui/Documents/Developer/gradle_project/0104/multproject/api/build/test-results testResultsDirName: test-results version: unspecified ~~~ ##8.获取构建过程中的时间信息--profile ~~~ ianhuis-Mac-mini:multproject qianhui$ gradle build --profile :shared:compileJava UP-TO-DATE :shared:processResources UP-TO-DATE :shared:classes UP-TO-DATE :shared:jar UP-TO-DATE :api:compileJava UP-TO-DATE :api:processResources UP-TO-DATE :api:classes UP-TO-DATE :api:jar UP-TO-DATE :api:assemble UP-TO-DATE :api:compileTestJava UP-TO-DATE :api:processTestResources UP-TO-DATE :api:testClasses UP-TO-DATE :api:test UP-TO-DATE :api:check UP-TO-DATE :api:build UP-TO-DATE :services:compileJava UP-TO-DATE :services:processResources UP-TO-DATE :services:classes UP-TO-DATE :services:jar UP-TO-DATE :services:assemble UP-TO-DATE :services:compileTestJava UP-TO-DATE :services:processTestResources UP-TO-DATE :services:testClasses UP-TO-DATE :services:test UP-TO-DATE :services:check UP-TO-DATE :services:build UP-TO-DATE :shared:assemble UP-TO-DATE :shared:compileTestJava UP-TO-DATE :shared:processTestResources UP-TO-DATE :shared:testClasses UP-TO-DATE :shared:test UP-TO-DATE :shared:check UP-TO-DATE :shared:build UP-TO-DATE :services:shared:compileJava UP-TO-DATE :services:shared:processResources UP-TO-DATE :services:shared:classes UP-TO-DATE :services:shared:jar UP-TO-DATE :services:shared:assemble UP-TO-DATE :services:shared:compileTestJava UP-TO-DATE :services:shared:processTestResources UP-TO-DATE :services:shared:testClasses UP-TO-DATE :services:shared:test UP-TO-DATE :services:shared:check UP-TO-DATE :services:shared:build UP-TO-DATE :services:webservice:compileJava UP-TO-DATE :services:webservice:processResources UP-TO-DATE :services:webservice:classes UP-TO-DATE :services:webservice:jar UP-TO-DATE :services:webservice:assemble UP-TO-DATE :services:webservice:compileTestJava UP-TO-DATE :services:webservice:processTestResources UP-TO-DATE :services:webservice:testClasses UP-TO-DATE :services:webservice:test UP-TO-DATE :services:webservice:check UP-TO-DATE :services:webservice:build UP-TO-DATE BUILD SUCCESSFUL Total time: 5.677 secs ~~~ 在项目的build/reports/profile下会生成一个html文件,打开 可以看到一个报告。 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-01-07_568e46666b087.jpg) ## 9.dry run 就是模拟执行过程,但是并没有执行 ~~~ qianhuis-Mac-mini:multproject qianhui$ gradle -m clean :api:clean SKIPPED :services:clean SKIPPED :shared:clean SKIPPED :services:shared:clean SKIPPED :services:webservice:clean SKIPPED BUILD SUCCESSFUL ~~~ 会发现项目目录中里的build目录并没有被删除。而且上面的输出信息也可以看出SKIPPED。
';