简体   繁体   English

解决gradle拥有的项目依赖项

[英]Resolving gradle owned projects dependencies

Project structure: 项目结构:

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

Dependencies: 依存关系:

  1. ProjectCommonUtil -> none ProjectCommonUtil->无
  2. ProjectWeb -> a, c, d ProjectWeb-> a,c,d
  3. ProjectBatchUtil -> none ProjectBatchUtil->无
  4. ProjectHttpClientUtil -> a ProjectHttpClientUtil->一个
  5. ProjectBatchWithMainClass -> b, c, d(a) ProjectBatchWithMainClass-> b,c,d(a)

Without worrying about open source dependencies yet, here is what I am struggling with: 无需担心开源依赖关系,这就是我正在努力解决的问题:

  1. Ability to build each project separately 能够分别构建每个项目

    1. Dependencies should be determined in lib folder jars 依赖关系应在lib文件夹jar中确定

      1. If dependency jar is missing, build dependency and cc: jar to lib folder 如果缺少依赖项jar,则将依赖项和cc:jar构建到lib文件夹

        Ex: d depends on a. 例如:d取决于a。 a's jar is not in lib folder so build a then place in lib folder, then build d a的jar不在lib文件夹中,因此在lib文件夹中构建一个then位置,然后构建d

        These are the included build.gradle files I have provided below 这些是我在下面提供的包含的build.gradle文件

  2. The lib folder in the above structure contains 上述结构中的lib文件夹包含
    1. Jars of a,b (yes, in jar form b/c of e), c, d, e a,b的罐子(是,以e的罐子形式b / c),c,d,e
    2. An Ear file of b. 耳锉b。 ProjectWeb, containing jars a, c, d in the appropriate WEB-INF/lib folder in b. ProjectWeb,在b中相应的WEB-INF / lib文件夹中包含jars,a,c和d。 ProjectWeb ProjectWeb

I cannot create a gradle multi-project in between root and the existing projects and I cannot create fat jars. 我无法在root和现有项目之间创建gradle多项目,也无法创建胖罐。 Maybe I'm trying to micromanage gradle but this #2 is what I need because I need to deploy the web ear to an app server and the batch (e.) and it's dependencies to several linux VMs in multiple locations running variations of e. 也许我正在尝试微管理gradle,但这是我需要的#2,因为我需要将Web Ear部署到应用服务器和批处理(e。),并且它依赖于运行e的多个位置的多个linux VM。 with different JVM args. 具有不同的JVM参数。

Help! 救命!

TIA, Bill TIA,比尔

a. 一种。 ProjectCommonUtil build.gradle 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. d。 ProjectHttpClientUtil build.gradle 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 
}

You are definitely fighting Gradle and having to compensate for things it will handle for you if you amend your setup a bit. 您肯定在与Gradle战斗,如果您稍稍修改一下设置,就必须补偿它会为您处理的事情。

The multi project definition can perfectly live at the same level as the projects it aggregates. 多项目定义可以完美地与它聚合的项目处于同一级别。 Simply add a new directory with a build.gradle and a settings.gradle , and reference the different modules you need to integrate: 只需添加一个带有build.gradlesettings.gradle的新目录,然后引用您需要集成的不同模块:

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

and then you can express dependencies between the projects, add packaging tasks that do the necessary copies to the lib folder, etc ... 然后您可以表达项目之间的依赖关系,将执行必要复制的打包任务添加到lib文件夹中,等等。

includeFlat may be another option as Louis suggests. 正如路易斯建议的那样,includeFlat可能是另一个选择。 This is how I solved my issue. 这就是我解决问题的方式。

ArcGradle ProjectCommon lib\\ArcProjectJars

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

ArcGradle build.gradle 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")
    }
}

And my ArcCommon settings.gradle is empty and the build.gradle only contains dependencies{} 我的ArcCommon settings.gradle为空,而build.gradle仅包含依赖项{}

I could add rootProject.name = 'ArcCommon' to settings.gradle but it's inferred from the folder name 我可以将rootProject.name = 'ArcCommon'添加到settings.gradle,但这是从文件夹名称推断出来的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM