================================================================================================= 环境初始化: sudo apt-get install gradle 建立项目文件夹 mkdir gs-gradle 建立项目文件结构 cd gs-gradle mkdir initial mkdir complete mkdir test 建立程序文件结构 cd initial mkdir -p src/main/java/hello 建立gradle配置文件 nano build.gradle ------------------------------------------------------------------- build.gradle apply plugin: ‘java‘ apply plugin: ‘eclipse‘ apply plugin: ‘application‘ mainClassName = ‘hello.HelloWorld‘ // tag::repositories[] repositories { mavenLocal() mavenCentral() } // end::repositories[] // tag::jar[] jar { baseName = ‘gs-gradle‘ version = ‘0.1.0‘ } // end::jar[] // tag::dependencies[] dependencies { compile "joda-time:joda-time:2.2" } // end::dependencies[] // tag::wrapper[] task wrapper(type: Wrapper) { gradleVersion = ‘1.11‘ } // end::wrapper[] 查看gradle当前可用的任务 gradle tasks 编译组建项目 gradle build 说明: 该命令在 build.gradle 所在文件夹下执行 编译后的结果会出现在下面三个文件夹里: build/classes. The project’s compiled .class files. build/reports. Reports produced by the build (such as test reports). build/libs. Assembled project libraries (usually JAR and/or WAR files). ================================================================================================= 扩展一(设置项目目录结构): mkdir initial mkdir complete mkdir test ================================================================================================= 扩展二(设置程序目录结构): cd initial mkdir -p src/main/java/hello ================================================================================================= 扩展三(建立gradle配置文件): cd initial nano build.gradle -------------------------------------------------------------- build.gradle内容: apply plugin: ‘java‘ #配置程序支持插件 ------------------------------------------------------------------- 扩展一(添加功能插件): ------------------------------------------------------------------------------ 扩展一(添加java插件): ---------------------------------------------------------------------------- 扩展一(添加java通用插件): apply plugin: ‘java‘ #配置程序支持插件 ---------------------------------------------------------------------------- 扩展二(添加java可执行jar插件): apply plugin: ‘application‘ mainClassName = ‘hello.HelloWorld‘ ---------------------------------------------------------------------- 使用例子: $ ./gradlew run :compileJava UP-TO-DATE :processResources UP-TO-DATE :classes UP-TO-DATE :run The current local time is: 16:16:20.544 Hello world! BUILD SUCCESSFUL Total time: 3.798 secs ------------------------------------------------------------------- 扩展二(添加eclipse支持插件): apply plugin: ‘eclipse‘ ---------------------------------------------------------------------- 扩展二(添加库源): -------------------------------------------------------------------- 语法: repositories { mavenLocal() mavenCentral() } --------------------------------------------------------------------- 说明: The repositories block indicates that the build should resolve its dependencies from the Maven Central repository. Gradle leans heavily on many conventions and facilities established by the Maven build tool, including the option of using Maven Central as a source of library dependencies. -------------------------------------------------------------------------- 扩展三(添加依赖库): ------------------------------------------------------------------------ 语法: dependencies { compile "joda-time:joda-time:2.2" } -------------------------------------------------------------------------- 说明: With the dependencies block, you declare a single dependency for Joda Time. Specifically, you’re asking for (reading right to left) version 2.2 of the joda-time library, in the joda-time group. Another thing to note about this dependency is that it is a compile dependency, indicating that it should be available during compile-time (and if you were building a WAR file, included in the /WEB-INF/libs folder of the WAR). Other notable types of dependencies include: providedCompile. Required dependencies for compiling the project code, but that will be provided at runtime by a container running the code (for example, the Java Servlet API). testCompile. Dependencies used for compiling and running tests, but not required for building or running the project’s runtime code. ------------------------------------------------------------------------------ 扩展一(定义运行时依赖): ------------------------------------------------------------------- 语法: compile "joda-time:joda-time:2.2" ------------------------------------------------------------------------ 说明: 定义在部署运行时所需的依赖 ---------------------------------------------------------------------------------------- 扩展二(定义开发临时依赖): ---------------------------------------------------------------------- 语法: providedCompile "joda-time:joda-time:2.2" ------------------------------------------------------------------------- 说明: 定义运行环境已提供,但开发时需要的依赖 Required dependencies for compiling the project code, but that will be provided at runtime by a container running the code (for example, the Java Servlet API). --------------------------------------------------------------------------------------------- 扩展三(定义测试时依赖): -------------------------------------------------------------------------- 语法: testCompile "joda-time:joda-time:2.2" ----------------------------------------------------------------------------- 说明: 定义在测试时所用的依赖库 Dependencies used for compiling and running tests, but not required for building or running the project’s runtime code. ------------------------------------------------------------------------------- 扩展四(设置项目jar包的名称和版本): ------------------------------------------------------------------------------ 语法: jar { baseName = ‘gs-gradle‘ version = ‘0.1.0‘ } ------------------------------------------------------------------------------- 说明: The jar block specifies how the JAR file will be named. In this case, it will render gs-gradle-0.1.0.jar. ------------------------------------------------------------------------------------ 扩展五(配置生成自动建立脚本): -------------------------------------------------------------------------- 语法: task wrapper(type: Wrapper) { gradleVersion = ‘1.11‘ } 注: 该配置添加到 build.gradle 文件结尾 然后运行: gradle wrapper --------------------------------------------------------------------------- 说明: The Gradle Wrapper is the preferred way of starting a Gradle build. It consists of a batch script for Windows and a shell script for OS X and Linux. These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system. To make this possible, add the following block to the bottom of your build.gradle. ------------------------------------------------------------------------------- 扩展一(自动建立脚本目录结构): └── initial └── gradlew └── gradlew.bat └── gradle └── wrapper └── gradle-wrapper.jar └── gradle-wrapper.properties --------------------------------------------------------------------------------------- 扩展二(自动建立脚本的使用): ./gradlew build --------------------------------------------------------------------------------------- 扩展六(编写注释): // tag::wrapper[] task wrapper(type: Wrapper) { gradleVersion = ‘1.11‘ } // end::wrapper[] ================================================================================================= 扩展四(gradle命令工具): --------------------------------------------------------------------------------------- 扩展一(查看gradle当前可执行的任务): cd 项目根目录(initial的父目录) gradle tasks --------------------------------------------------------------------------------------- 扩展二(编译组建项目): gradle build ----------------------------------------------------------------- 说明: 该命令在 build.gradle 所在文件夹下执行 编译后的结果会出现在下面三个文件夹里: build/classes. The project’s compiled .class files. build/reports. Reports produced by the build (such as test reports). build/libs. Assembled project libraries (usually JAR and/or WAR files). 用过的依赖库出现在: build/dependency_cache 文件夹中 ----------------------------------------------------------------------- 扩展一(生成的目录结构): build ├── classes │ └── main │ └── hello │ ├── Greeter.class │ └── HelloWorld.class ├── dependency-cache ├── libs │ └── gs-gradle-0.1.0.jar └── tmp └── jar └── MANIFEST.MF ------------------------------------------------------------------------------------ 扩展三(生成自动建立脚本): -------------------------------------------------------------------------- 语法: task wrapper(type: Wrapper) { gradleVersion = ‘1.11‘ } 注: 该配置添加到 build.gradle 文件结尾 然后运行: gradle wrapper --------------------------------------------------------------------------- 说明: The Gradle Wrapper is the preferred way of starting a Gradle build. It consists of a batch script for Windows and a shell script for OS X and Linux. These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system. To make this possible, add the following block to the bottom of your build.gradle. ------------------------------------------------------------------------------- 扩展一(自动建立脚本目录结构): └── initial └── gradlew └── gradlew.bat └── gradle └── wrapper └── gradle-wrapper.jar └── gradle-wrapper.properties --------------------------------------------------------------------------------------- 扩展二(自动建立脚本的使用): ./gradlew build
时间: 2024-10-12 14:19:40