我在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 将充当 Controller ,将带有参数的项目 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
}
}
}
}
有没有办法在项目 C 的资源文件夹中仅包含项目 B 的 zip
编辑2: 我现在可以从项目 B 获取发行版 zip,但只需将其放在项目 C 的类路径中即可。
项目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')
}
最佳答案
您需要在编译函数中添加依赖项,如下所示。
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 after
和 should run after
当您使用“必须在之后运行”排序规则时,您指定只要任务 A 和任务 B 都将运行,任务 B 必须始终在任务 A 之后运行。这表示为taskB.mustRunAfter(taskA)。
更多信息关注官方documentation它解释得很好并且易于使用
关于java - 添加另一个项目 jar 作为资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61786610/