Gradle秘籍 顶

2019-08-23 10:31:44 浏览数 (1)

  • 帮助: 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地址。

0 人点赞