gradle windows下安装使用 以及 gradle项目与maven项目互转

gradle使用

前提条件

安装jvm,并配置好了java环境变量

安装步骤

1.下载gradle 2.3版本

2.将压缩包解压到某个目录如 e:/gradle

添加GRADLE_HOME 地址 

添加PATH环境变量:%GRADLE_HOME%\bin;

验证是否安装成功

cmd中输入 gradle -version

Gradle 2.3

Build time: 2015-02-16 05:09:33 UTC 
Build number: none 
Revision: 586be72bf6e3df1ee7676d1f2a3afd9157341274

Groovy: 2.3.9 
Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 
JVM: 1.7.0_17 (Oracle Corporation 23.7-b01) 
OS: Windows 7 6.1 amd64




maven to gradle

在maven项目根目录下执行命令:

gradle init --type pom
当然你得先下载Gradle,配置完环境变量。


gradle to maven 

gradle.build必须如下配置:
[java]  view plain  copy
  1. apply plugin: 'java'  
  2. apply plugin: 'maven'  
  3.    
  4. group = 'xxx.xxx'  
  5. version = '1.0-SNAPSHOT'  
  6.    
  7. dependencies {  
  8. compile 'commons-lang:commons-lang:2.3'  
  9. }  
在Gradle项目根目录下执行 gradle install,我们会发现根目录的build文件夹下生成了一个poms文件夹,里面包含我们需要的pom文件。
注意使用阿里云的jar 资源地址 给一个例子如下
build.gradle:
信息如下内容:
apply plugin: 'java'
apply plugin: 'maven'

group = 'org.demo.shiro'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.7
task writeNewPom << {
    pom {
        project {
            inceptionYear '2008'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("$buildDir/../pom.xml")
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'war'
apply plugin: 'jetty'
sourceCompatibility = 1.7
version = '1.0'
description='shiro和spring演示'
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'utf-8'


//构建工程目录布局
task createJavaProject << {
    sourceSets*.java.srcDirs*.each { it.mkdirs() }
    sourceSets*.resources.srcDirs*.each { it.mkdirs()}
}

task createWebProject(dependsOn: 'createJavaProject') << {
   // def webApp =file(projectDir.path+"src/main/webapp");
   // println "webapp.path:"+webApp.path;
  //  webApp.mkdirs()

}

repositories {
    maven {
      url "http://maven.aliyun.com/nexus/content/groups/public/"
    }
}
eclipse {
    classpath {
      defaultOutputDir = file("build/eclipse")
    }
}
//源码定义
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
            srcDir 'src/test/java'
        }
        resources {
            srcDir 'src/main/resources'
            srcDir 'src/test/resources'
        }
    }
}
dependencies {
    //spring
    compile  'org.springframework:spring-beans:3.0.4.RELEASE'
    compile  'org.springframework:spring-core:3.0.4.RELEASE'
    compile  'org.springframework:spring-web:3.0.4.RELEASE'
    compile  'org.springframework:spring-webmvc:3.0.4.RELEASE'
    compile  'org.springframework:spring-context:3.0.4.RELEASE'
    compile  'org.springframework:spring-context-support:3.0.4.RELEASE'
    //json
    compile   'org.codehaus.jackson:jackson-core-lgpl:1.8.1'
    compile   'org.codehaus.jackson:jackson-mapper-lgpl:1.8.1'
    compile   'commons-fileupload:commons-fileupload:1.3.1'
    compile   'commons-io:commons-io:2.4'
    compile   'org.apache.commons:commons-lang3:3.4'
    //shiro
    compile  'org.apache.shiro:shiro-core:1.2.3'
    compile  'org.apache.shiro:shiro-web:1.2.3'
    compile  'org.apache.shiro:shiro-spring:1.2.3'
    //j2ee
    compile 'javax.servlet:javax.servlet-api:3.1.0'
    //log
    compile  "org.slf4j:slf4j-log4j12:1.7.5"
    //jedis 
    compile  "redis.clients:jedis:2.1.0"
    testCompile 'junit:junit:4.11'
}

 
clean{
   println "删除构建目录"
   delete "$project.buildDir"
}


jettyRun {
    httpPort = 9080
    contextPath = ''
    //按下enter键,加载eclipse的class
    reload = 'manual'
    classpath = files(eclipse.classpath.defaultOutputDir) + classpath
   // classpath.each{
       println "classPath:$it.path";
    //}
    print "webapp.path:$war.archivePath.path"
    print "webAppSourceDirectory:$webAppSourceDirectory.path"
    
        
}

task copyResources(type:Copy)  {
    from("$project.buildDir/resources/main")
    into("$webAppDir/WEB-INF/")
}


另外一篇 :

写这个博客的动机是,网上有很多错误的方法,我希望给大家一个引导,节省时间。


以CAS-4.2.7项目为例,这是一个gradle构建的项目,我们希望把它转为maven项目,方法很简单,如下:

找到一个个子项目目录下的build.gradle文件,在文件开头添加以下内容:

apply plugin: 'java'
apply plugin: 'maven'

group = 'com.wonhigh'
version = '4.2.7-RELEASE'
sourceCompatibility = 1.7

task writeNewPom << {
    pom {
        project {
            inceptionYear '2008'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("$buildDir/../pom.xml")
}

以下两句必要时加入:

dependencies {  
    compile 'commons-lang:commons-lang:2.4'  

当然,你可以根据自己公司的实际情况修改,很简单,似曾相识,对,就是maven的pom文件内容。


在这个项目下,运行:gradle install,记住,是install命令,网上很多都说是gradle build,是错误的。

命令执行后,项目下会出现\build\poms目录,把里面的pom-default.xml改成pom.xml(不改工程导入不了),剪贴到子项目根目录下,任务完成。

对于CAS这个项目,有很多子项目,怎么办呢?

有个简单点的办法,需要在每个子项目下都修改build.gradle,然后到CAS根目录下执行:gradlew install -x test,这样一次行就把所有子项目下的pom文件都生成了,但是还是得手工一个一个贴到根下。

如果不想一个一个pom文件剪贴到项目根目录下,则在CAS根目录下运行:gradlew writeNewPom,命令执行完后,各子项目下均有pom文件了。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值