解决gradle拥有的项目依赖项

时间:2018-03-12 20:21:36

标签: java gradle

项目结构:

root
-- 0. CAN'T add multi-project here
----\ a. ProjectCommonUtil
----\ b. ProjectWeb
----\ c. ProjectBatchUtil
----\ d. ProjectHttpClientUtil
----\ e. ProjectBatchWithMainClass
----\ lib

依赖关系:

  1. ProjectCommonUtil - >无
  2. ProjectWeb - > a,c,d
  3. ProjectBatchUtil - >无
  4. ProjectHttpClientUtil - >一个
  5. ProjectBatchWithMainClass - > b,c,d(a)
  6. 不用担心开源依赖关系,这就是我正在努力解决的问题:

    1. 能够单独构建每个项目

      1. 应在lib文件夹jar中确定依赖关系

        1. 如果缺少依赖jar,则构建依赖关系并将cc:jar转换为lib文件夹

          Ex:d取决于a。 a的jar不在lib文件夹中,所以在lib文件夹中构建一个然后放置,然后构建d

          这些是我在下面提供的build.gradle文件

    2. 上述结构中的lib文件夹包含
      1. a,b的罐子(是的,以罐子形式b / c的e),c,d,e
      2. b的Ear文件。 ProjectWeb,在b中相应的WEB-INF / lib文件夹中包含jar a,c,d。 ProjectWeb
    3. 我无法在root和现有项目之间创建一个gradle多项目,我无法创建胖罐。也许我正在尝试微观管理gradle但这个#2是我需要的,因为我需要将web ear部署到app服务器和批处理(e。),并且它依赖于运行e变体的多个位置的几个linux VM。使用不同的JVM args。

      帮助!

      TIA, 比尔

      一个。 ProjectCommonUtil build.gradle

      /*
       * This build file was generated by the Gradle 'init' task.
       *
       * This generated file contains a commented-out sample Java project to get you started.
       * For more details take a look at the Java Quickstart chapter in the Gradle
       * user guide available at https://docs.gradle.org/4.3/userguide/tutorial_java_projects.html
       */
      
      // +++++++++++++++++++++ ORDER IS IMPORTANT ++++++++++++++++
      plugins {
          id 'base'
      }
      
      
      // Apply the java plugin to add support for Java
      apply plugin: 'java'
      //apply plugin: 'checkstyle'
      apply plugin: 'jdepend'
      apply plugin: 'pmd'
      apply plugin: 'project-report'
      
      build.dependsOn('copyArcCommonProjectJar', 'copyArcCommonExternalJars')
      group "com.mmm.arc"
      version =  '2.5.ArcCommon'
      
      // In this section you declare where to find the dependencies of your project
      repositories {
          // Use 'jcenter' for resolving your dependencies.
          // You can declare any Maven/Ivy/file repository here.
          jcenter()
      }
      
      sourceCompatibility = 1.8
      targetCompatibility = 1.8   
      
      sourceSets {
          main {
               java {
                    srcDir 'src'
                    }
               }
      }
      
      // Short hand form
      //compileJava.doFirst { println 'In compileJava:BEGIN' }
      //compileJava.doLast { println 'In compileJava:END' }
      
      tasks.create(name: 'copyArcCommonProjectJar', type: Copy, dependsOn: 'jar') {
          into "../CommonJars/ArcProjectJars"
              from "${buildDir}/libs"
              include "*.jar"
      }   
      copyArcCommonProjectJar.doFirst { 
        println "${buildDir}:In copyArcCommonProjectJar:BEGIN" 
      
        // Remove existing jars only if build is necessary
        delete "../CommonJars/ArcProjectJars/${name}*.jar"
      }
      copyArcCommonProjectJar.doLast { println 'In copyArcCommonProjectJar:END' }
      //copyArcCommon.outputs.upToDateWhen { false }
      
      tasks.create(name: 'copyArcCommonExternalJars', type: Copy, dependsOn: 'dependencies') {
          into "../CommonJars"
            from configurations.runtime
          //include "*.jar"
      } 
      copyArcCommonExternalJars.doFirst { println 'In copyArcCommonExternalJars:BEGIN' }
      copyArcCommonExternalJars.doLast { println 'In copyArcCommonExternalJars:END' }
      //copyExternalJars.outputs.upToDateWhen { false }
      
      jar {
          //baseName = 'ArcCommon'
      }
      jar.doFirst { println 'In jar:BEGIN' }
      jar.doLast { println 'In jar:END' }
      //jar.outputs.upToDateWhen { false }
      
      
      // In this section you declare the dependencies for your production and test code
      dependencies {
          // The production code uses the SLF4J logging API at implementation time
          // implementation 'org.slf4j:slf4j-api:1.7.25'
          // implementation 'org.slf4j:slf4j-api:+'
      
          // Declare the dependency for your favourite test framework you want to use in your tests.
          // TestNG is also supported by the Gradle Test task. Just change the
          // testimplementation dependency to testimplementation 'org.testng:testng:6.8.1' and add
          // 'test.useTestNG()' to your build script.
      //    testimplementation 'junit:junit:4.12'
      
          // providedimplementation is for jars need to build but are provided at runtime
          // providedimplementation 
      }
      

      d。 ProjectHttpClientUtil build.gradle

      /*
       * This build file was generated by the Gradle 'init' task.
       *
       * This generated file contains a commented-out sample Java project to get you started.
       * For more details take a look at the Java Quickstart chapter in the Gradle
       * user guide available at https://docs.gradle.org/4.3/userguide/tutorial_java_projects.html
       */
      
      // +++++++++++++++++++++ ORDER IS IMPORTANT ++++++++++++++++
      plugins {
          id 'base'
      }
      
      
      // Apply the java plugin to add support for Java
      apply plugin: 'java'
      //apply plugin: 'checkstyle'
      apply plugin: 'jdepend'
      apply plugin: 'pmd'
      apply plugin: 'project-report'
      
      build.dependsOn(
          'buildArcCommon',
          'copyArcProjectJar', 'copyExternalJars'
      )
      group "com.mmm.arc"
      version =  '2.5.TekExHTTPClient'
      
      // In this section you declare where to find the dependencies of your project
      repositories {
          // Use 'jcenter' for resolving your dependencies.
          // You can declare any Maven/Ivy/file repository here.
          jcenter()
      }
      
      sourceCompatibility = 1.8
      targetCompatibility = 1.8   
      
      sourceSets {
          main {
               java {
                    srcDir 'src'
                    }
               }
      }
      
      task buildArcCommon(type: GradleBuild) {
          doFirst {
             println '---  Compiling ArcCommon ---'
              buildFile = '../ArcCommon/build.gradle/'
          }
         // tasks = ['build']
      }
      
      tasks.create(name: 'copyArcProjectJar', type: Copy, dependsOn: 'jar') {
          into "../CommonJars/ArcProjectJars"
              from "${buildDir}/libs"
              include "*.jar"
      }   
      copyArcProjectJar.doFirst { 
        println 'In copyArcCommon:BEGIN' 
      
        // Remove existing jars only if build is necessary
        delete "../CommonJars/ArcProjectJars/${name}*.jar"
      }
      copyArcProjectJar.doLast { println 'In copyArcCommon:END' }
      //copyArcCommon.outputs.upToDateWhen { false }
      
      tasks.create(name: 'copyExternalJars', type: Copy, dependsOn: 'dependencies') {
          into "../CommonJars"
            from configurations.runtime
          //include "*.jar"
      } 
      copyExternalJars.doFirst { println 'In copyExternalJars:BEGIN' }
      copyExternalJars.doLast { println 'In copyExternalJars:END' }
      //copyExternalJars.outputs.upToDateWhen { false }
      
      //See above short hand
      /*
      task myTask (  ) {
         println '+++++++++++++  Compiling ArcCommon BEGIN  ++++++++++++++++'
      }
      */
      
      // Short hand form
      /*
      implementationJava.doFirst { 
          println '+++++++++++++  Compiling TekExHTTPClient BEGIN  ++++++++++++++++'
      //  tasks = [ "buildArcCommon", "taskCopyAllTekExProjects" ]
      //  dependsOn buildArcCommon, taskCopyAllTekExProjects
      }
      
      implementationJava.doLast { println '+++++++++++++  Compiling TekExHTTPClient END  ++++++++++++++++'}
      
      //implementationJava.myTask
      
      
      
      task taskCopyAllTekExProjects(type: Copy) {
          into 'build/libs'
          include '../ArcCommon/build/libs/*.jar'
      }
      
      //taskCopyAllTekExProjects.mustRunAfter 'buildArcCommon'
      
      /*
      task test(dependsOn: [implementation, implementationTest]) {
          doLast {
              println '+++++++++++++  Running ArcCommon Unit Tests  ++++++++++++++++'
          }
      }
      */
      /*
      task dist(dependsOn: [implementation, test]) {
          doLast {
              println '+++++++++++++  Building ArcCommon Jar  ++++++++++++++++'
      
          }
      }
      */
      
      jar {
         // baseName = 'TekExHTTPClient'
      }
      
      // In this section you declare the dependencies for your production and test code
      dependencies {
          // The production code uses the SLF4J logging API at implementation time
          implementation 'org.apache.httpcomponents:httpcore:+'
          implementation 'org.apache.httpcomponents:httpclient:+'
          compile fileTree (dir: '../CommonJars/ArcProjectJars')
      
          // Declare the dependency for your favourite test framework you want to use in your tests.
          // TestNG is also supported by the Gradle Test task. Just change the
          // testimplementation dependency to testimplementation 'org.testng:testng:6.8.1' and add
          // 'test.useTestNG()' to your build script.
      //    testimplementation 'junit:junit:4.12'
      
          // providedimplementation is for jars need to build but are provided at runtime
          // providedimplementation 
      }
      

2 个答案:

答案 0 :(得分:0)

如果你稍微修改一下设置,你肯定会和Gradle打架,不得不赔偿它会为你处理的事情。

多项目定义可以完美地与其聚合的项目处于同一级别。只需添加一个包含build.gradlesettings.gradle的新目录,并引用您需要集成的不同模块:

    includeFlat 'a'
    includeFlat 'b'
    includeFlat 'c'
    includeFlat 'd'
    includeFlat 'e'

然后你可以表达项目之间的依赖关系,添加执行必要副本的打包任务到lib文件夹等......

答案 1 :(得分:0)

如路易斯建议的那样,includeFlat可能是另一种选择。这就是我解决问题的方法。

ArcGradle ProjectCommon lib\ArcProjectJars

ArcGradle settings.gradle
rootProject.name = 'ArcGradle' include ':ProjectCommon' project(':ProjectCommon').projectDir = file('../ProjectCommon')

ArcGradle build.gradle

subprojects {
    // +++++++++++++++++++++ ORDER IS IMPORTANT ++++++++++++++++
    apply plugin: 'java'
    group "com.mmm.arc"
    version =  '2.5.0'
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    build.dependsOn('copyProjectJar')
    clean.dependsOn('deleteProjectJar')

    // In this section you declare where to find the dependencies of your project
    repositories {
        // Use 'jcenter' for resolving your dependencies.
        // You can declare any Maven/Ivy/file repository here.
        jcenter()
    }

    ext {
        arcProjectJarDir = "${rootDir}${File.separator}CommonJars${File.separator}ArcProjectJars"
    } 

    tasks.create(name: 'copyProjectJar', type: Copy) {
        into "${arcProjectJarDir}"
            from "/build/libs"
            include "*.jar"
    }   
    copyProjectJar.doFirst { 
        println "${project.name}:In copyProjectJar:BEGIN" 
    }
    copyProjectJar.doLast { println 'In copyProjectJar:END' }
    copyProjectJar.dependsOn('jar','deleteProjectJar')
    tasks.create(name: 'deleteProjectJar', type:Delete) {
        // Remove existing jars only if build is necessary
        delete fileTree(dir: "${arcProjectJarDir}", include: "${project.name}*.jar")
    }
}

我的ArcCommon settings.gradle为空,build.gradle只包含依赖项{}

我可以将rootProject.name = 'ArcCommon'添加到settings.gradle,但是从文件夹名称中推断出