项目自动构建工具gradle使用

=================================================================================================
环境初始化:
    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

项目自动构建工具gradle使用的相关文章

项目自动构建工具对比(Maven、Gradle、Ant)

Java世界中主要有三大构建工具:Ant.Maven和Gradle.经过几年的发展,Ant几乎销声匿迹.Maven也日薄西山,而Gradle的发展则如日中天. Maven的主要功能主要分为5点,分别是依赖管理系统.多模块构建.一致的项目结构.一致的构建模型和插件机制.我们可以从这五个方面来分析一下Gradle比起Maven的先进之处. 依赖管理系统 Maven为Java世界引入了一个新的依赖管理系统.在Java世界中,可以用groupId.artifactId.version组成的Coordin

项目自动化建构工具gradle 入门5——在intellij中做一个gradle的web工程

之前的几个小节,都是纯手工建文件夹,纯手工拷贝war包,或者纯手工解压个zip看看结果,,,,这还是我写了玩的helloWorld.若是玩大工程.几十个人的团队协同开发时,这么玩,,,,暴躁的程序员估计血压爆表了. 对于大点的java web项目,我们需要用到Tomcat 9.0 M10,gradle 3.2.1,IntelliJ 来搞定这一档子事. 1.软件下载安装,见<项目自动化建构工具gradle 入门0--环境 & 废话>. 2.打开IntelliJ,File -> Ne

项目自动化建构工具gradle 入门4——javaWeb在浏览器中显示helloWorld

在java应用中,其实做的最多的还是java web应用.所以现在我们做的就是用gradle构建一个简单的web项目,简单点,直接上代码吧. 1.进入目录D:\work\gradle\web,新建文件build.gradle,键入内容: 1 apply plugin: 'war' // 引入war插件, 2 3 repositories { // 从哪里找jar包 4 flatDir { 5 // 先看下build.gradle文件所在目录下的 libs目录中有没有 6 dirs 'libs'

项目自动化建构工具gradle 入门0——环境 &amp; 废话

gradle 是一个项目自动化构建工具.同类的产品还有ant ,maven等等.相比之下我更喜欢gradle,它语法简洁.兼容maven.ide集成很好. 学习使用gradle最快的方式是看文档,而且下载到的gradle文档中也包含了所有的文档.但是对于初学者而言或者是对只需要简单实用gradle的人来说,其实没必要去看那么多的文档. 在学习的过程中,我看过一些其他博友的文章,必须承认他们确实比我厉害比我有条例,在文章一开始有很多的铺垫和论述,大多从原理开始且用例子结束.而且很多都是在Mac 或

项目管理及自动构建工具Maven

项目管理及自动构建工具Maven 一.Maven安装.目录结构.cmd命令1.下载安装apache-maven-3.2.3-bin.zip下载:http://maven.apache.org/download.cgi 安装:解压,配置环境变量M2_HOME=D:\Idea\config\apache-maven-3.2.3Path+=D:\Idea\config\apache-maven-3.2.3\bin 通过执行 mvn -v 可以查看当前版本号 C:\Users\yuki>mvn -v A

自动构建工具Gulp

摘要:  gulp与grunt一样,都是自动构建工具.和grunt相比它更突出一个流的概念,任务是一个接一个执行的.今天就分享如何用gulp做自动化. 安装: gulp也是基于node环境,所以安装gulp之前你需要安装node.js.  npm install -g gulp 只要在命令窗口中执行上面一行命令就完成安装,这样安装的是全局安装.在项目中一般是通过package.json中的devDependencies属性来安装.如下: { "name": "",

前端项目自动化构建工具——Webpack入门教程

参考资料:https://www.webpackjs.com/(中文文档)   https://www.webpackjs.com/(官方文档) 首先有必要说明一下,本文侧重讲解webpack基本配置属性,不附带实例,将会以通俗易懂的形式地讲解:如若需要实例进行相关练习,可将本文作为理论基础: Webpack是前端项目自动化构建工具,本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构

java实现的一个maven多模块项目自动生成工具

平时在做spring mvc web新项目时,都需要自己去搭建spring mvc的项目框架,包括基本pom 依赖引入,基本配置文件(web.xml,spring-mvc.xml,数据库配置文件等等),基础工具类引入.实际上对于所有spring mvc web项目,这些基础的配置和基础类都是通用的,都是可以复用,真正需要改变的无非是我们具体的业务逻辑.所以我们可以把这些通用的东西都做成基础模板,通过指定项目的groupId.artifactId.version就可以通过代码自动生成spring

项目构建工具gradle

1.安装 https://gradle.org/install 2.构建一个项目 https://guides.gradle.org/creating-new-gradle-builds/ 3.build 存在的项目 https://guides.gradle.org/using-an-existing-gradle-build/ 用户手册 https://docs.gradle.org/current/userguide/userguide.html