前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Gradle秘籍 顶

Gradle秘籍 顶

作者头像
白石
发布2019-08-23 10:31:44
3750
发布2019-08-23 10:31:44
举报
文章被收录于专栏:白石
  • 帮助: gradle -h
代码语言:javascript
复制
`-?, -h, --help`          Shows this help message.    
`-a, --no-rebuild`        Do not rebuild project dependencies.    
`-b, --build-file`        Specifies the build file.    
`-C, --cache`             Specifies how compiled build scripts should be cached. Possible values are: 'rebuild' and 'on'. Default value is 'on' [deprecated - Use '--rerun-tasks' or '--recompile-scripts' instead]    
`-c, --settings-file`     Specifies the settings file.    
`--continue`              Continues task execution after a task failure.    
`-D, --system-prop`       Set system property of the JVM (e.g. -Dmyprop=myvalue).    
`-d, --debug`             Log in debug mode (includes normal stacktrace).    
`--daemon`                Uses the Gradle daemon to run the build. Starts the daemon if not running.    
`--foreground`            Starts the Gradle daemon in the foreground. [incubating]    
`-g, --gradle-user-home`  Specifies the gradle user home directory.    
`--gui`                   Launches the Gradle GUI.    
`-I, --init-script`       Specifies an initialization script.    
`-i, --info`              Set log level to info.    
`-m, --dry-run`           Runs the builds with all task actions disabled.    
`--no-color`              Do not use color in the console output.    
`--no-daemon`             Do not use the Gradle daemon to run the build.    
`--no-opt`                Ignore any task optimization. [deprecated - Use '--rerun-tasks' instead]    
`--offline`               The build should operate without accessing network resources.    
`-P, --project-prop`      Set project property for the build script (e.g. -Pmyprop=myvalue).    
`-p, --project-dir`       Specifies the start directory for Gradle. Defaults to current directory.    
`--parallel`              Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. [incubating]    
`--parallel-threads`      Build projects in parallel, using the specified number of executor threads. [incubating]    
`--profile`               Profiles build execution time and generates a report in the <build_dir>/reports/profile directory.    
`--project-cache-dir`     Specifies the project-specific cache directory. Defaults to .gradle in the root project directory.    
`-q, --quiet`             Log errors only.    
`--recompile-scripts`     Force build script recompiling.    
`--refresh`               Refresh the state of resources of the type(s) specified. Currently only  'dependencies' is supported. [deprecated - Use '--refresh-dependencies' instead.]    
`--refresh-dependencies`  Refresh the state of dependencies.    
`--rerun-tasks`           Ignore previously cached task results.    
`-S, --full-stacktrace`   Print out the full (very verbose) stacktrace for all exceptions.    
`-s, --stacktrace`        Print out the stacktrace for all exceptions.    
`--stop`                  Stops the Gradle daemon if it is running.    
`-u, --no-search-upward`  Don't search in parent folders for a settings.gradle file.    
`-v, --version`           Print version info.    
`-x, --exclude-task`      Specify a task to be excluded from execution.    

  • 查看task帮助 执行: gradle help --task init
代码语言:javascript
复制
 Task :help
Detailed task information for init

Path
     :init

Type
     InitBuild (org.gradle.buildinit.tasks.InitBuild)

Options
     --type     Set type of build to create.
                Available values are:
                     basic
                     groovy-application
                     groovy-library
                     java-application
                     java-library
                     pom
                     scala-library

     --test-framework     Set alternative test framework to be used.
                          Available values are:
                               spock
                               testng

Description
     Initializes a new Gradle build.

Group
     Build Setup


BUILD SUCCESSFUL in 2s

  • 列出所有task: gradle tasks

  • 跳过单元测试: gradle build -x test

  • 伪执行: 这种执行用在想要知道命令行中task的执行顺序,但又不真正执行task的时候,相当于“伪执行”。使用-m参数,如:gradle -m clean compile,将显示这个两个任务执行中涉及的任务,但又不会真正去执行它们。

  • 使用自定义的Maven仓库 在build.gradle配置文件里加上
代码语言:javascript
复制
  repositories {
    maven { url "http://wjwtest:8081/nexus/content/groups/public" }
    maven { url "http://repo.springsource.org/plugins-release" }
  }

  • 初始化Java Lib项目框架 执行: gradle init --type java-library 会生成如下目录结构
代码语言:javascript
复制
/ProhectPath:
│  build.gradle
│  gradlew
│  gradlew.bat
│  settings.gradle
│
├─.gradle
│  ├─4.2.1
│  │  ├─fileChanges
│  │  │      last-build.bin
│  │  │
│  │  ├─fileHashes
│  │  │      fileHashes.bin
│  │  │      fileHashes.lock
│  │  │
│  │  └─taskHistory
│  │          fileSnapshots.bin
│  │          taskHistory.bin
│  │          taskHistory.lock
│  │
│  └─buildOutputCleanup
│          buildOutputCleanup.lock
│          cache.properties
│          outputFiles.bin
│
├─gradle
│  └─wrapper
│          gradle-wrapper.jar
│          gradle-wrapper.properties
│
└─src
    ├─main
    │  └─java
    │          Library.java
    │
    └─test
        └─java
                LibraryTest.java

  • 初始化Java APP项目框架 执行: gradle init --type java-library 会生成如下目录结构
代码语言:javascript
复制
/ProhectPath:
│  build.gradle
│  gradlew
│  gradlew.bat
│  settings.gradle
│
├─.gradle
│  ├─4.2.1
│  │  ├─fileChanges
│  │  │      last-build.bin
│  │  │
│  │  ├─fileHashes
│  │  │      fileHashes.bin
│  │  │      fileHashes.lock
│  │  │
│  │  └─taskHistory
│  │          fileSnapshots.bin
│  │          taskHistory.bin
│  │          taskHistory.lock
│  │
│  └─buildOutputCleanup
│          buildOutputCleanup.lock
│          cache.properties
│          outputFiles.bin
│
├─gradle
│  └─wrapper
│          gradle-wrapper.jar
│          gradle-wrapper.properties
│
└─src
    ├─main
    │  └─java
    │          App.java
    │
    └─test
        └─java
                AppTest.java

  • Maven项目秒变gradle项目 切换到你maven项目目录 然后执行: gradle init --type pom 你的maven项目已经秒变gradle项目

  • Gradle 如何使用Maven本地缓存库(Local Maven repository) 从Maven切换到Gradle,原有的几G的本地缓存库当然想继续使用。 如果想使用Maven本地缓存,需要定义:build.gradle 文件下定义
代码语言:javascript
复制
repositories {
    mavenLocal()
}

Gradle使用与Maven相同的策略去定位本地Maven缓存的位置。如果在settings.xml中定义了本地Maven仓库的地址,则使用该地址。 在USER_HOME/.m2下的settings.xml文件中的配置会覆盖存放在M2_HOME/conf下的settings.xml文件中的配置。 如果没有settings.xml配置文件,Gradle会使用默认的USER_HOME/.m2/repository地址。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档