添加另一个项目jar作为资源

问题描述 投票:0回答:1

我在gradle有一个java多项目,它的结构是这样的。

root
+ Project A
+ Project B
\ Project C

还有一些依赖关系

Project A
\ Other stuff (normal 'api' dependency)

Project B
+ Project A (normal 'api' dependency)
\ Other stuff (normal 'api' dependency)

Project C
+ Project A (normal 'api' dependency)
+ Project B (need jar)
\ Other stuff (normal 'api' dependency)

项目C需要能够在一个单独的JVM中运行项目B的组装版本。以后的目标是让项目B和C在不同的机器上运行,项目C将作为控制器,将带参数的项目B部署到类似AWS的地方。但现在,我需要能够在本地测试它。

编辑:我在根项目中有这段代码,用来获取所有子项目的分发zip。

subprojects.each { subproject ->
  evaluationDependsOn(subproject.path)
}
task multiprojectJar(type: Copy,dependsOn: subprojects.assemble) {
  into 'localDeploy'
  subprojects.each { subproject ->
    into(""){
      from subproject.configurations.archives.artifacts.files.findAll{ file ->
        file.name.substring(file.name.lastIndexOf('.')+1) == 'zip'
      }.collect { file ->
        zipTree(file)// if folder in zip needed
        //file //if zip needed
      }
    }
  }
}

有没有办法只把项目B的zip包含在项目C的资源文件夹里呢?

编辑2:我现在能够从项目B中获取发行版的zip,但只要把它放在项目C的classpath中就可以了。

项目B的build.gradle。

configurations {
    assembledZip{
        canBeConsumed = true
        canBeResolved = false
    }
}
dependencies {
    api project(':projectA')
}
artifacts {
    assembledZip(
        configurations.archives.artifacts.files.findAll{ file ->
            file.name.substring(file.name.lastIndexOf('.')+1) == 'zip'
        }.collect { file ->
            file
        }
    )
}

C项目build. gradle:

dependencies {
    api project(':projectA')
    runtimeOnly project(path: ':projectB', configuration: 'assembledZip')
}
java gradle
1个回答
0
投票

你需要在编译函数中添加依赖关系,如下所示。

task compilecom_ofss_ob_infra(type: JavaCompile) {
        mustRunAfter ':compileproject_A:jarPojectA'
        source = fileTree(dir: '/path/to/src', include: '**/*.java')
        destinationDir = file('/path/to/buildspace')
        options.fork = 'true'
        options.forkOptions.with {
            memoryMaximumSize = '2048m'
        }
        dependencies {
            classpath = fileTree(include: ['**/*.jar'], dir: '/path/to/projectA.jar')

        }   
    }

为了处理执行顺序,你可以使用 must run aftershould run after 当你使用 "必须在后面运行 "的排序规则时,你指定任务B必须总是在任务A之后运行,无论何时任务A和任务B都会被运行。这表示为 taskB.mustRunAfter(taskA)。

更多信息请关注官方 文件 它解释得很清楚,使用起来也很简单

© www.soinside.com 2019 - 2024. All rights reserved.