• gradle多模块开发(转)


    参考文档:gradle的官方userguide.pdf文档的chapter 55和chapter 56.
    gradle的多模块或项目开发一定不会比maven差,在我看来!大的项目分成多个模块来开发是常事.下文就介绍一下怎么用gradle开发多模块项目.对于gradle,在Eclipse和IDEA开者之间,毫无疑问选择IDEA作为IDE.
    testweb是一个简单例子,项目只分成了core和web两个模块.其中core模块是放一些基本的或公共的java类,web模块放的是web Controller,配置,页面.所以最终打包项目时,core应打成一个jar包,而web模块引用(依赖)core模块,对于web的java类也打起一个jar包,这两个jar包最后是放在lib包下面再打成war包.项目的主要结构如下:
    testweb
      core
        src
          main
            java
          test
            java
            resources
        build.gradle
      web
        src
          main
            java
            resources
          test
            java
        build.gradle
      build.gradle
      settings.gradle
    core主要使用spring+spring data jpa(hibernate实现)+mysql


    一.根据上面的项目结构,新建必要的目录和文件.
    1.settings.gradle.只有一个模块来说,此文件是可选的.对于多模块,此文件是必须的.

    [plain] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. include 'core','web'  

    2.这里将core和web模块的gradle配置也放到了顶层的build.gradle

    build.gradle

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. allprojects {  
    2.     apply plugin: 'java'  
    3.     group = 'org.exam'  
    4.     version = '1.0'  
    5.     sourceCompatibility = 1.8  
    6.     targetCompatibility = 1.8  
    7. }  
    8. subprojects {  
    9.     ext {  
    10.         slf4jVersion = '1.7.7'  
    11.         springVersion = '4.2.1.RELEASE'  
    12.         hibernateVersion = '4.3.1.Final'  
    13.     }  
    14.     [compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'  
    15.     repositories {  
    16.         mavenCentral()  
    17.     }  
    18.     configurations {  
    19.         //compile.exclude module: 'commons-logging'  
    20.         all*.exclude module: 'commons-logging'  
    21.     }  
    22.     dependencies {  
    23.         compile(  
    24.                 "org.slf4j:jcl-over-slf4j:${slf4jVersion}",  
    25.                 "org.slf4j:slf4j-log4j12:${slf4jVersion}",  
    26.                 "org.springframework:spring-context:$springVersion",  
    27.                 "org.springframework:spring-orm:$springVersion",  
    28.                 "org.springframework:spring-tx:$springVersion",  
    29.                 "org.springframework.data:spring-data-jpa:1.5.2.RELEASE",  
    30.                 "org.hibernate:hibernate-entitymanager:$hibernateVersion",  
    31.                 "c3p0:c3p0:0.9.1.2",  
    32.                 "mysql:mysql-connector-java:5.1.26",  
    33.                 "commons-fileupload:commons-fileupload:1.3.1",  
    34.                 "com.fasterxml.jackson.core:jackson-databind:2.3.1"  
    35.         )  
    36.         testCompile(  
    37.                 "org.springframework:spring-test:$springVersion",  
    38.                 "junit:junit:4.11"  
    39.         )  
    40.     }  
    41. }  
    42. project(':core') {  
    43.   
    44. }  
    45. project(':web') {  
    46.     apply plugin: "war"  
    47.     dependencies {  
    48.         compile project(":core")  
    49.         compile(  
    50.                 "org.springframework:spring-webmvc:$springVersion",  
    51.                 "org.apache.taglibs:taglibs-standard-impl:1.2.1"  
    52.         )  
    53.         providedCompile(  
    54.                 "javax.servlet:javax.servlet-api:3.1.0",  
    55.                 "javax.servlet.jsp:jsp-api:2.2.1-b03",  
    56.                 "javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"  
    57.         )  
    58.     }  
    59.     processResources{  
    60.         /* 从'$projectDir/src/main/product'目录下复制文件到'WEB-INF/classes'目录下覆盖原有同名文件*/  
    61.         from("$projectDir/src/main/product")  
    62.     }  
    63.   
    64.     /*自定义任务用于将当前子项目的java类打成jar包,此jar包不包含resources下的文件*/  
    65.     def jarArchiveName="${project.name}-${version}.jar"  
    66.     task jarWithoutResources(type: Jar) {  
    67.         from sourceSets.main.output.classesDir  
    68.         archiveName jarArchiveName  
    69.     }  
    70.   
    71.     /*重写war任务:*/  
    72.     war {  
    73.         dependsOn jarWithoutResources  
    74.         /* classpath排除sourceSets.main.output.classesDir目录,加入jarWithoutResources打出来的jar包 */  
    75.         classpath = classpath.minus(files(sourceSets.main.output.classesDir)).plus(files("$buildDir/$libsDirName/$jarArchiveName"))  
    76.     }  
    77.     /*打印编译运行类路径*/  
    78.     task jarPath << {  
    79.         println configurations.compile.asPath  
    80.     }  
    81. }  
    82.   
    83. /*从子项目拷贝War任务生成的压缩包到根项目的build/explodedDist目录*/  
    84. task explodedDist(type: Copy) {  
    85.     into "$buildDir/explodedDist"  
    86.     subprojects {  
    87.         from tasks.withType(War)  
    88.     }  
    89. }  


    此项目包括core和web两个模块,其中core为普通java模块,web为web模块,并且web依赖core.


    a.打包web时,会先将websrcmain esources下的文件复制到webuild esourcesmain目录,然后复制websrcmainproduct下的文件到webuild esourcesmain目录来覆盖同名文件.
    b.编译websrcmainjava下的java文件到webuildclassesmain目录,然后将webuildclassesmain的文件打成jar包.
    c.将所需依赖,包括core-${version}.jar和web-${version}.jar复制到war包下的WEB-INFlib目录.将websrcmainproduct下的文件复制到WEB-INFclasses目录

    二.将项目导入IDEA去开发
    三.测试.
    1.先测试core模块.主要参考coresrc estjavacomexam epositoryUserRepositoryTest.java.
    2.再测试web模块.
    a.先用junit测试controller.主要参考websrc estjavacomexamwebUserControllerTest.java,
    b.参考<<配置简单的嵌入式jetty>>测试(可选)
    c.打再成war包,部署到tomcat或jetty测试.


    源码:http://download.csdn.net/detail/xiejx618/7736387

    http://blog.csdn.net/xiejx618/article/details/38469865

  • 相关阅读:
    Django(app的概念、ORM介绍及编码错误问题)
    Django(完整的登录示例、render字符串替换和redirect跳转)
    Construct Binary Tree from Preorder and Inorder Traversal
    Single Number II
    Single Number
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Binary Tree Zigzag Level Order Traversal
    Recover Binary Search Tree
    Add Binary
  • 原文地址:https://www.cnblogs.com/softidea/p/5416740.html
Copyright © 2020-2023  润新知