微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Groovy 生成gradle 多项目层级脚本1.0

/**
*@author  tangmi
*@date    2016-05-29
*@desc    Gradle项目生成模板
*@version 1.0
**/
  class Module{
    String name
    String packageType // '','war','jar'
    List<String> dependencies
    
    Module(name,packageType,dependencies){
        this.name=name
        this.packageType=packageType
        this.dependencies=dependencies
    }
  }    
  
  
  def workSpace='/Users/tangdu/git/apigateway';
  println "-----------${workSpace}----------------->"
  
  def prj_name='apigateway'
  def projects=[
      'biz':[
        ["${prj_name}-facade",'jar',[]] as Module,["${prj_name}-biz",["${prj_name}-common","${prj_name}-dao","${prj_name}-model"]] as Module,["${prj_name}-integration",["${prj_name}-facade"]] as Module
      ],'tools':[
        ["${prj_name}-common",[]]as Module,["${prj_name}-deploy",["${prj_name}-facade","${prj_name}-biz","${prj_name}-common","${prj_name}-model"]] as Module
      ],'dao':[
        ["${prj_name}-dao",["${prj_name}-model"]] as Module
      ],'model':[
        ["${prj_name}-model",[]] as Module
      ],'web':[
        ["${prj_name}-web",'',"${prj_name}-model"]]as Module
      ]
  ]
  def _build_tpl="""
apply plugin: 'java'
apply plugin: 'eclipse'

subprojects{
    apply plugin: "java"
    apply plugin: "eclipse"
    //apply plugin: 'findbugs'
    //apply plugin: 'pmd'
    //apply plugin: 'checkstyle'
    
    group ="com.tdu"
    version = "1.0.0"
    
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
    
    repositories {
     mavenLocal()
     mavenCentral()
    }
    
    configurations {
        compile.exclude group: "org.springframework.boot",module: "spring-boot-starter-tomcat"
        compile.exclude group: "org.codehaus.woodstox",module: "wstx-asl"
        compile.exclude group: "commons-fileupload"
    }
    
    dependencies {
      compile(
    
      )
      //依赖maven中不存在的jar
      //ext.jarTree = filetree(dir: 'libs',include: '**/*.jar')
      testCompile ()
    }
    
    //源代码打包任务
    task sourceJar (type : Jar) {
        classifier = "sources"
        from sourceSets.main.allSource
    }
    
    //Java 文档打包任务
    task javadocJar (type: Jar,dependsOn: javadoc) {
        classifier = "javadoc"
        from javadoc.destinationDir
    }
    
    //findbugs {
    //    ignoreFailures = true
    //    sourceSets = [sourceSets.main]
    //}
    
    //pmd {
    //    ignoreFailures = true
    //   sourceSets = [sourceSets.main]
    //}
    
    eclipse{
        jdt{
          sourceCompatibility = 1.7
          targetCompatibility = 1.7
        }
    }
    
    uploadArchives{
        repositories {
          flatDir{
            dirs 'repos'
          }
        }
    }
}
  """

  def initSettingTpl={
    def _bf=new StringBuffer()
    projects.each{
      def module_dir=it.key
      _bf.append("\n//${module_dir}")
      it.value.each{
          def module=it
          def _settings_tpl="""
include(':${module.name}')
project(':${module.name}').projectDir = new File(settingsDir,'${module_dir}/${module.name}')"""
          _bf.append(_settings_tpl)
      }
    }
    return _bf.toString()
  }

  println 'step1:begin-----------init settring.gradle----------------->'
  def _tpl_str=initSettingTpl()
  def _fs=new File("${workSpace}/settings.gradle").withPrintWriter{
    it.println(_tpl_str)
  }
  println 'step1:end-------------init settring.gradle----------------->'

  println 'step2:begin-----------init gradle.properties----------------->'
  def _fg=new File("${workSpace}/gradle.properties").withPrintWriter{
    
  }
  println 'step2:end-------------init gradle.properties----------------->'


  println 'step3:begin-----------init build.properties----------------->'
  def _fb=new File("${workSpace}/build.gradle").withPrintWriter{
    it.println(_build_tpl)
  }
  println 'step3:end-------------init build.properties----------------->'

  println 'step4:begin-----------init eclispe.sh----------------->'
  def _fe=new File("${workSpace}/eclipse.sh").withPrintWriter{
    it.println('gradle cleanEclipse eclipse -x test')
  }
  println 'step4:end-------------init eclipse.sh----------------->'


  //sourceSets*.java.srcDirs*.each { it.mkdirs() }
  //sourceSets*.resources.srcDirs*.each { it.mkdirs() }

  def initProjectDirFuc={
    projects.each{f->
      def module_dir=f.key;
      f.value.each{m->
        def root="${workSpace}/${module_dir}/${m.name}"
        new File("${root}/src/main/java").mkdirs()
        new File("${root}/src/main/resources").mkdirs()
        new File("${root}/src/test/java").mkdirs()
        new File("${root}/src/test/resources").mkdirs()
        //dependency
        def des=new StringBuffer()
        m.dependencies.each{
            def child_d="""
                    project(":${it}"),"""
            des.append(child_d)
        }
        
        def packageType={type->
            if(type==''){
                return ""
            }
            return """apply plugin: \"${type}\""""
        }
        def child="""      
            dependencies {
                compile(
                    ${des.toString()}
                )
            }
        """
        new File("${root}/build.gradle").withPrintWriter{
            it.println(child)
        }
        
      }
    }
  }
  println 'step6:begin-----------init projectDir----------------->'
  initProjectDirFuc()
  println 'step6:end-------------init projectDir----------------->'

  println '--------------successed---------------->'

生成项目结构方式:

1 gradle-template插件

2 自建

3 脚本

如何使用:

1 workSpace 是您项目根目录,需要在脚本中修改.

2 运行脚本。需要groovy环境以及gradle环境

结果如下:

3 只是方便自己使用,不喜勿喷。thks

备注:此项目结构面向SOA的结构分包,很多细节还没写完。先上做备注吧。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐