把项目从Maven迁移到Gradle

2680阅读 0评论2015-10-13 jieforest
分类:Java

把项目从Maven迁移到Gradle

在过去几年中,Maven已成为Java及Java EE项目事实上的构建工具,没有之一。而最近两年,Gradle获得越来越多的人使用,分走了不少Maven的用户。本文就是讲述怎样把Java EE项目从Maven迁移到Grale。

Grale是一个自动化的构建工具,类似于Ant或Maven,与这两个构建工具不同的是,Gradle引入了基于Groovy语言的DSL语法来代替XML配置,因此它的配置文件是一个Groovy文件。

安装Gradle有几种方法,最佳的方法是使用sdkman工具来安装。而要安装sdkman工具,只需简单的运行:

# curl -s get.sdkman.io | bash
Thanks for using...                                                          


     SSSSSSSSSSSSSSS DDDDDDDDDDDDD       KKKKKKKKK    KKKKKKK                  
   SS:::::::::::::::SD::::::::::::DDD    K:::::::K    K:::::K                  
  S:::::SSSSSS::::::SD:::::::::::::::DD  K:::::::K    K:::::K                  
  S:::::S     SSSSSSSDDD:::::DDDDD:::::D K:::::::K   K::::::K                  
  S:::::S              D:::::D    D:::::DKK::::::K  K:::::KKK                  
  S:::::S              D:::::D     D:::::D K:::::K K:::::K                     
   S::::SSSS           D:::::D     D:::::D K::::::K:::::K                      
    SS::::::SSSSS      D:::::D     D:::::D K:::::::::::K                       
      SSS::::::::SS    D:::::D     D:::::D K:::::::::::K                       
         SSSSSS::::S   D:::::D     D:::::D K::::::K:::::K                      
              S:::::S  D:::::D     D:::::D K:::::K K:::::K                     
              S:::::S  D:::::D    D:::::DKK::::::K  K:::::KKK                  
  SSSSSSS     S:::::SDDD:::::DDDDD:::::D K:::::::K   K::::::K                  
  S::::::SSSSSS:::::SD:::::::::::::::DD  K:::::::K    K:::::K                  
  S:::::::::::::::SS D::::::::::::DDD    K:::::::K    K:::::K                  
   SSSSSSSSSSSSSSS   DDDDDDDDDDDDD       KKKKKKKKK    KKKKKKK                  


                      mmmmmmm    mmmmmmm     aaaaaaaaaaaaa  nnnn  nnnnnnnn     
                    mm:::::::m  m:::::::mm   a::::::::::::a n:::nn::::::::nn   
                   m::::::::::mm::::::::::m  aaaaaaaaa:::::an::::::::::::::nn  
                   m::::::::::::::::::::::m           a::::ann:::::::::::::::n 
                   m:::::mmm::::::mmm:::::m    aaaaaaa:::::a  n:::::nnnn:::::n 
                   m::::m   m::::m   m::::m  aa::::::::::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::m a::::aaaa::::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::ma::::a    a:::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::ma::::a    a:::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::ma:::::aaaa::::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::m a::::::::::aa:::a n::::n    n::::n 
                   mmmmmm   mmmmmm   mmmmmm  aaaaaaaaaa  aaaa nnnnnn    nnnnnn 


                                                 Now attempting installation...

Looking for a previous installation of SDKMAN...
Looking for unzip...
Not found.
================================================================================================
 Please install unzip on your system using your favourite package manager.

 Restart after installing unzip.
================================================================================================ 

说明需要unzip工具,执行命令安装它:

# apt-get install unzip 

再次安装sdkman

# curl -s get.sdkman.io | bash

Thanks for using...                                                          


     SSSSSSSSSSSSSSS DDDDDDDDDDDDD       KKKKKKKKK    KKKKKKK                  
   SS:::::::::::::::SD::::::::::::DDD    K:::::::K    K:::::K                  
  S:::::SSSSSS::::::SD:::::::::::::::DD  K:::::::K    K:::::K                  
  S:::::S     SSSSSSSDDD:::::DDDDD:::::D K:::::::K   K::::::K                  
  S:::::S              D:::::D    D:::::DKK::::::K  K:::::KKK                  
  S:::::S              D:::::D     D:::::D K:::::K K:::::K                     
   S::::SSSS           D:::::D     D:::::D K::::::K:::::K                      
    SS::::::SSSSS      D:::::D     D:::::D K:::::::::::K                       
      SSS::::::::SS    D:::::D     D:::::D K:::::::::::K                       
         SSSSSS::::S   D:::::D     D:::::D K::::::K:::::K                      
              S:::::S  D:::::D     D:::::D K:::::K K:::::K                     
              S:::::S  D:::::D    D:::::DKK::::::K  K:::::KKK                  
  SSSSSSS     S:::::SDDD:::::DDDDD:::::D K:::::::K   K::::::K                  
  S::::::SSSSSS:::::SD:::::::::::::::DD  K:::::::K    K:::::K                  
  S:::::::::::::::SS D::::::::::::DDD    K:::::::K    K:::::K                  
   SSSSSSSSSSSSSSS   DDDDDDDDDDDDD       KKKKKKKKK    KKKKKKK                  


                      mmmmmmm    mmmmmmm     aaaaaaaaaaaaa  nnnn  nnnnnnnn     
                    mm:::::::m  m:::::::mm   a::::::::::::a n:::nn::::::::nn   
                   m::::::::::mm::::::::::m  aaaaaaaaa:::::an::::::::::::::nn  
                   m::::::::::::::::::::::m           a::::ann:::::::::::::::n 
                   m:::::mmm::::::mmm:::::m    aaaaaaa:::::a  n:::::nnnn:::::n 
                   m::::m   m::::m   m::::m  aa::::::::::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::m a::::aaaa::::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::ma::::a    a:::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::ma::::a    a:::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::ma:::::aaaa::::::a  n::::n    n::::n 
                   m::::m   m::::m   m::::m a::::::::::aa:::a n::::n    n::::n 
                   mmmmmm   mmmmmm   mmmmmm  aaaaaaaaaa  aaaa nnnnnn    nnnnnn 


                                                 Now attempting installation...

Looking for a previous installation of SDKMAN...
Looking for unzip...
Looking for curl...
Looking for sed...
Installing SDKMAN scripts...
Create distribution directories...
Create candidate directories...
Created for asciidoctorj: /root/.sdkman/asciidoctorj
Created for crash: /root/.sdkman/crash
Created for gaiden: /root/.sdkman/gaiden
Created for glide: /root/.sdkman/glide
Created for gradle: /root/.sdkman/gradle
Created for grails: /root/.sdkman/grails
Created for griffon: /root/.sdkman/griffon
Created for groovy: /root/.sdkman/groovy
Created for groovyserv: /root/.sdkman/groovyserv
Created for jbake: /root/.sdkman/jbake
Created for jbossforge: /root/.sdkman/jbossforge
Created for lazybones: /root/.sdkman/lazybones
Created for springboot: /root/.sdkman/springboot
Prime the config file...
Download script archive...
Extract script archive...
Install scripts...
Attempt update of bash profiles...
Updated existing /root/.profile
Updated existing /root/.bashrc
Attempt update of zsh profiles...
Created and initialised /root/.zshrc



All done!


Please open a new terminal, or run the following in the existing one:

    source "/root/.sdkman/bin/sdkman-init.sh"

Then issue the following command:

    sdk help

Enjoy!!! 

安装完成后需要初始化sdkman:

# source "$HOME/.sdkman/bin/sdkman-init.sh" 

一旦sdkman可用,那么安装Gradle就极其简单了:

# sdk install gradle
==== BROADCAST =================================================================
* 30/09/15: Gradle 2.8-rc-1 released on SDKMAN! #gradle
* 30/09/15: SDKMAN! 3.0.5 released. Fixes issue #363. 
* 25/09/15: Grails 3.0.8 released on SDKMAN! #grailsfw
================================================================================

Downloading: gradle 2.7

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0   354    0     0     28      0 --:--:--  0:00:12 --:--:--    83
100 42.7M  100 42.7M    0     0  87103      0  0:08:34  0:08:34 --:--:--  113k

Installing: gradle 2.7
Done installing!

Do you want gradle 2.7 to be set as default? (Y/n): y

Setting gradle 2.7 as default. 

下面可以开始创建构建脚本。首先要做的是创建一个名为settings.gradle的文件,设置项目名。

rootProject.name = 'my-javaee' 

此文件适用于多模块的项目。

下面需要创建一个名为build.gradle的配置文件,它负责管理所有的构建过程。

apply plugin: 'war'
group = 'org.superbiz'
version = '1.0-SNAPSHOT'
description = "My JavaEE project"
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
    mavenCentral()        
}
dependencies {
    providedCompile group: 'javax', name: 'javaee-api', version:'7.0'
} 

注意,第一行指示构建的是一个Web项目(WAR包)。接着是项目的属性,如项目组group、版本version、描述description、Java编译器的版本、软件仓库。只有最后一行的依赖关系是必须的,它提供了构建的处理范围。

要注意,如果使用的是war插件,那么providedCompile仅有一个是有效的。如果使用的是像java这样的插件,那么需要自己实现这个函数。

可以看出,Gradle的配置文件更加精炼,没有冗余的信息。想深入Gradle就继续吧!

上一篇:Spring 4.2框架中注释驱动的事件监听器详解
下一篇:Tomcat 8在CentOS 7的守护进程设置