将多个项目本身添加为另一个项目中的依赖项

时间:2018-07-26 09:08:35

标签: java eclipse gradle build.gradle multi-project

经过大量搜索后,这是在此处提出它的最后一个选择!在eclipse中,我正在使用Gradle设计这样的项目结构,如下所示...

Algorithms              //Parent Project
    -SubModuleCore      //Child Project for common utilities & dependencies
        -build.gradle
    -SubModuleOne       //Child project for any operation
        -build.gradle   //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
    -SubModuleTwo       //Child project for another operation
        -build.gradle   //Added 'SubModuleCore' as a dependency like compile project(':SubModuleCore')
    -build.gradle
    -settings.gradle

Services                //Stand-Alone project
    -build.gradle       //Here I want to add 'Algorithms' as a single dependency
    -settings.gradle

如上所述,项目结构在Eclipse工作区中是相同的。我可以生成 Algorithms 项目的单个 .jar 。所以问题是我想将此 Algorithms 项目作为单个依赖项添加到项目 Services 中,例如compile project(':Algorithms')。但是蚀只说“闭嘴!”。我不想将其发布在Maven Central / jitpack等地方。相反,我想在本地进行发布。我正在尝试这种方式...

服务/build.gradle

apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
project.webAppDirName = 'WebContent'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'lib', include: ['*.jar'])
    compile project(':Algorithms')
}

Services / settings.gradle

rootProject.name = 'Services'
include 'Algorithms'
project(':Algorithms').projectDir = new File(settingsDir, '../Algorithms')

算法/build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = "1.8";
targetCompatibility = "1.8";

subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'

    sourceCompatibility = "1.8";
    targetCompatibility = "1.8";

    buildscript {
       dependencies {
          repositories {
            jcenter()
             mavenCentral()
          }
        }
    }

    repositories {
        jcenter()
        mavenCentral()
    }

}
subprojects.each { subproject -> 
   evaluationDependsOn(subproject.path) 
}
jar.dependsOn subprojects.tasks['classes']
jar {
  baseName = 'algorithms'
  subprojects.each { subproject ->
    from subproject.sourceSets.main.output.classesDir
  }
  from files('resources/log4j2.xml')
  from files('resources/application.properties')
}

算法/settings.gradle

rootProject.name = 'Algorithms'
include ':SubModuleCore', ':SubModuleOne', ':SubModuleTwo'

我尝试了SO的几种解决方案,但仍然没有成功。有人请帮助我,我在这里被卡住了。看来我已经很接近了,但是不知道缺少了什么!

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用includeBuild功能。

在Services / settings.gradle中声明包含的内部版本

rootProject.name='Services'

includeBuild '../Algorithms'

,然后使用

表达依赖性
compile "${project.group}:${project.name}"

其中项目组并从Algorithms项目中命名。