gradle配置用例(转载)

plugins {
    id 'java'
}
//相当于maven的groupid
group 'com.cyjz'
//相当于maven的version
version '1.0-SNAPSHOT'
//maven中的artifactId相当于name,显示在settings.gradle里面

//配置所有项目公共信息
allprojects{
    //插件的类型,可以添加war
    apply plugin:'java'
    sourceCompatibility = 1.8
    //项目发布到仓库供其他人使用
    //3步,1.添加发布插件
//    apply plugin:'maven-publish'
//    //2.添加发布url
//    //3.执行发布任务
//    publishing{
//        publishions{
//            myPublish(MavenPubliscation){
//                from compenents.java
//            }
//        }
//        repositories{
//            maven{
//                name "myRepo"
//                url ""
//            }
//        }
//    }
}
//配置子项目内容
subprojects{
    repositories {
        //指定阿里云镜像
        maven{
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        mavenCentral()
    }
//依赖,类似于maven的dependencies
    dependencies {
        //对子项目依赖的写法
//    compile project(":model")
//    compile 'org.hibernate:hibernate-core:3.6.3.Final'
        compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
        testCompile group: 'junit', name: 'junit', version: '4.12'
//    //解决冲突方式1,排除传递性依赖
//    compile('org.hibernate:hibernate-core:3.6.3.Final'){
//        exclude group:'org.slf4j',module:'slf4j-api'
//    }
//    //解决冲突方式2,强制指定一个版本
//    configurations.all{
//        resolutionStrategy{
//            force 'org.slf4j:slf4j-api:1.7.24'
//        }
//    }
    }
}
//发现版本冲突就构建失败,不要默认去选择最高版本的策略
configurations.all{
    resolutionStrategy{
        failOnVersionConflict()
    }
}
    //解决冲突方式2,强制指定一个版本
configurations.all{
    resolutionStrategy{
        force 'org.slf4j:slf4j-api:1.7.24'
    }
}

repositories {
    //指定阿里云镜像
    maven{
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    mavenCentral()
}

dependencies {
    //对子项目依赖的写法
//    compile project(":model")
//    compile 'org.hibernate:hibernate-core:3.6.3.Final'
//    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    testCompile group: 'junit', name: 'junit', version: '4.12'
//    //解决冲突方式1,排除传递性依赖
//    compile('org.hibernate:hibernate-core:3.6.3.Final'){
//        exclude group:'org.slf4j',module:'slf4j-api'
//    }
//    //解决冲突方式2,强制指定一个版本
//    configurations.all{
//        resolutionStrategy{
//            force 'org.slf4j:slf4j-api:1.7.24'
//        }
//    }
}
//创建文件夹目录的闭包
def createDir = {
    path ->
        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdirs()
        }
}
//初始化生成项目的任务
task makeJavaDir(){
    def paths = ['src/test/test','src/test/web']
    doFirst{
        paths.forEach(createDir)
    }
}

task makeWebDir(){
    dependsOn 'makeJavaDir'
    def paths = ['src/main/webapp','src/test/webapp']
    doFirst{
        paths.forEach(createDir)
    }
}
//4个配置要求
//1.所有项目中应用java插件
//2.web子项目打包成war
//3.所有项目添加logback日志功能
//4.统一配置group和version
plugins {
    id 'maven-publish'
}


dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile('org.springframework:spring-context:5.0.9.RELEASE'){
        exclude group:'commons-logging',module:'commons-logging'
    }
    compile 'org.springframework:spring-web:5.0.9.RELEASE'
    compile 'org.springframework:spring-webmvc:5.0.9.RELEASE'
    compile 'org.codehaus.janino:janino:3.0.10'
    compile 'commons-lang:commons-lang:2.6'
    compile 'commons-codec:commons-codec:1.11'
    compile 'com.alibaba:fastjson:1.2.49'
    compile 'io.netty:netty-all:4.1.29.Final'
    compile ('com.alibaba:dubbo:2.6.2'){
        exclude group: 'org.springframework', module: 'spring'
        exclude group: 'org.jboss.netty', module: 'netty'
    }
    compile 'org.apache.zookeeper:zookeeper:3.4.9'
    compile 'org.apache.curator:curator-framework:2.12.0'
}

publishing {
    publications {
        maven(MavenPublication) {
            from(components.java)
        }
    }
}
//设置资源编译的编码
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}
//资源目录的设置
sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
        resources {
            srcDir 'src/main/java/resources'
        }
    }
    test {
        java {
            srcDir 'test/java'
        }
        resources {
            srcDir 'test/resources'
        }
    }
}
//设置主程序入口
def mainClassName = "com.cyjz.server.Main"

//在某些场合,我们不需要依赖和src打在一个jar包,我们希望有个lib,然后我们的jar运行时,自动去找依赖jar。这样,就可以不用插件了
task copyDependencies(type: Copy) {
    from configurations.runtime
    into 'build/libs/lib'
}
jar.dependsOn(copyDependencies)

jar {
    manifest {
        attributes "Main-Class": "$mainClassName"
        attributes "Implementation-Title": project.name
    }
    //设置打包依赖的jar
//    from {
//        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
//    }

    if (!configurations.runtime.isEmpty()) {
        manifest.attributes('Class-Path': '. lib/' + configurations.runtime.collect { it.name }.join(' lib/'))
    }
    //将静态资源打包出来
    processResources {
        from('src/main/java/resources'){
            include '**/*.*'
        }
    }
}
 

gradle.properties示例
group = 'com.cyjz'
version = '1.0-SNAPSHOT'
settings.gradle示例
rootProject.name = 'todoGradle'
include 'model'
include 'repository'
include 'web'

 

posted on 2021-01-19 15:04  AlphaGo1号  阅读(129)  评论(0编辑  收藏  举报

导航