Gradle构建之初步入门

使用gradle的目的:

1、代替Maven来管理依赖包。
2、通过配置任务取代之前一步步敲npm、bower、gulp等命令来实现的安装前端依赖环境。
3、构建项目


实现方法:
1、关于包依赖还是蛮容易实现的,百度(都说不习惯用谷歌的程序员不是好的程序员,但是,还是习惯性先百度再谷歌。事实再次证明,要想找你想要的东西,而且这个东西又不是那么被大众所熟悉,那么还是从谷歌上找比较靠谱。习惯百度这是个坏毛病,以后得改)一大堆例子。
Maven的pom.xml是这样的:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
    </dependencies>

Gradle在build.gradle配置文件中是这样的。

dependencies {
    compile group: 'junit', name: 'junit', version:'4.10'
    compile group: 'org.springframework', name: 'spring-core', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context-support', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-aop', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version:'4.1.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version:'4.1.6.RELEASE'
}
论代码的简洁性,Gradle完胜。

后来又百度了一个命令,非常有用,那就是如果你之前用的是Maven项目,那么怎么转换Gradle项目呢?当然没必要去一点点写啦。

gradle init --type pom
一行命令搞定。借鉴  http://blog.csdn.net/earbao/article/details/41550387

补充:当时有个小东西令人纠结了半天,就是这些包导入之后,发现跟自己新建个Gradle项目时导入的包显示形式不一样,当时直觉是哪里配置有问题,为了解决这个问题找了好久解决方法,后来才知道,刷新一下就可以了,如下图所示。心里顿时万马奔腾。。。。。不过还好,总算是包正常了。

 

2、关于用Gradle的配置来代替在Terminal端执行命令来完成安装前端js包的问题

这个问题绕了好久。一开始是想实现直接在配置文件里写任务,来执行命令的,但是奈何初始Gradle,对对方的命令一窍不通。网上的案例也是寥寥,只有个windows命令和Linux下的命令。搞了好久,才在https://gist.github.com/spikeheap/8558786#file-build-gradle这个上面看到有关执行npm命令的方法。

一开始还走了个弯路,因为我们要执行这些命令是在子文件夹下的,但是在跟目录的build.gradle中怎么写Task路径都不通过。

后来,终于解决那个路径问题了,方法很简单,我们要实现子目录下的命令执行,那么就直接在子目录的那个文件夹里新建个build.gradle的配置文件就Ok了。各司其职。

至于命令格式,参考如下(其实为什么这么写我也不是特别懂):

import org.gradle.api.tasks.Exec

defaultTasks 'bower'

// Get the path for the locally installed binaries
task npmBin << {
    new ByteArrayOutputStream().withStream { os ->
        def result = exec {
            executable = 'npm'
            args = ['bin']
            standardOutput = os
        }
        ext.binPath = os.toString().trim() + "/"
    }
}

// Install packages from package.json
task npm(type: Exec) {
    description = "Grab NodeJS dependencies (from package.json)"
    commandLine = ["npm", "install"]
    inputs.file "package.json"
    outputs.dir "node_modules"
    tasks.npmBin.execute()

}

3、构建

第二个问题暂时性解决了,但是构建时我想先执行子目录下的任务,再打包构建。直接dependsOn?在跟目录下执行gradle tasks --all,很遗憾,只能看到跟目录下的任务。后来,通过对http://www.blogjava.net/wldandan/archive/2012/07/12/382792.html这篇文章详细解读,在跟目录的setting.gradle中配置include:‘子文件夹’,再次查看task,果然有了子目录下的任务。再次依赖,问题便迎刃而解。

构建代码:

/*构建项目*/
task explodedWar(dependsOn:'子文件夹:任务名', type: Sync)  {
    into "构建到指定路径"
    with war
}

war {
    from('xxx') {
        into('/')
    }
}

end:现在项目大体上已经搭建起来了,但是Gradle貌似还有很多东西,以后还会多多关注,多多学习。有不足之处,欢迎提出宝贵意见。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值