Intellij idea中gradle设置多源文件目录

问题描述

Intellij idea开发工具来开发Java工程的时候,通常会使用ant、maven、groovy、gradle等工具来管理项目依赖。我在工程中使用了较新的gradle作为管理工具。但是在运行build.gradle的build任务时,通常需要将多个目录设置为源代码文件目录。否则一旦build完了以后,某些目录中的源代码会被忽视,而认为不是源代码。

比如说如下图所示:
未被归类为源文件的java代码情况

  • 该图中wsdc-api未被mark为源文件目录,因此该目录下所有java文件都未被识别为java源代码,因此这些代码文件左下角出现了红色的 “J”.

目标

Intellij idea项目结构

  • src/main/java 为默认的源文件目录
  • src/main/thrift 也希望成为gradle可识别的源文件目录
  • src/main/thrift-java 也希望成为gradle可识别的源文件目录

    解决方案

// # build.gradle #
buildscript {
    repositories {
        maven {
            url "http://repo.maven.apache.org/maven2/"
        }
        mavenCentral()
    }

    dependencies {
        classpath group: 'co.tomlee.gradle.plugins', name: 'gradle-thrift-plugin', version: '0.0.6'
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.10.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'thrift'
apply plugin: 'spring-boot'
apply plugin: 'war'

sourceCompatibility = 1.7

version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.apache.thrift:libthrift:0.9.2'
    compile 'org.springframework:spring-orm:4.1.4.RELEASE'
    compile('org.springframework.boot:spring-boot-starter-web:1.2.1.RELEASE')
    compile('org.springframework.boot:spring-boot-starter-data-jpa:1.2.2.RELEASE')
    //compile('org.springframework.boot:spring-boot-starter-actuator:1.2.2.RELEASE')
    compile('org.springframework:spring-jdbc:4.1.5.RELEASE')
    //compile('com.h2database:h2:1.4.185')

    // mysql
    compile('mysql:mysql-connector-java:5.1.34')
    // hibernate
    compile('org.hibernate:hibernate-entitymanager:4.3.8.Final')

    compile('org.projectreactor.spring:reactor-spring-context:1.1.3.RELEASE')
    compile('org.springframework:spring-web:4.1.5.RELEASE')

    compile('org.springframework.amqp:spring-rabbit:1.4.2.RELEASE')
    compile('org.springframework.amqp:spring-amqp:1.4.2.RELEASE')
    compile group: 'org.springframework.data', name: 'spring-data-mongodb', version: '1.6.1.RELEASE'

    compile "org.springframework.data:spring-data-rest-webmvc:2.2.1.RELEASE"
    compile("com.fasterxml.jackson.core:jackson-databind:2.5.0")
    compile("com.fasterxml.jackson.core:jackson-annotations:2.5.0")
    compile("com.fasterxml.jackson.core:jackson-core:2.5.0")
    //compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.7.Final'

    compile('org.springframework:spring-test:4.1.5.RELEASE')

    testCompile group: 'junit', name: 'junit', version: '4.11'
}


generateThriftSource {
    //
    // The output directory (optional)
    //
    out file('src/main/thrift-java')

    verbose false
    debug false
    strict true

    //
    // Modify the include path (optional)
    //
    // path file('vendor/thrift')

    //
    // Set the thrift executable (optional)
    //
    executable 'C:\\Program Files (x86)\\Java\\jdk1.7.0_40\\bin\\thrift.exe'

    generators {
        //
        // --gen java:hashcode,beans
        //
        java {
            //
            // Options passed to the `java` generator
            //
            option 'hashcode'
            option 'beans'
        }

    }
}

// here demonstrates how to add multiple srcDirs into project
sourceSets {
    main {
        java {
            srcDirs = ['src/main/java', 'src/main/thrift-java']
        }
        resources {
            srcDirs = ['src/main/resources']
        }
    }
    test {
        java {
            srcDirs = ['src/test/java', 'src/test/thrift']
        }
        resources {
            srcDirs = ['src/test/resources']
        }
    }
}

jar {
    // set the main class entry for the executable jar
    manifest {
        attributes 'Main-Class': 'calculator.CalculatorClient',
                'Implementation-Title': 'Gradle quickStart'
    }

    // this will add all the dependent jars into the generated-jar package
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值