我有一个包含两个子项目的项目:一个依赖项 jar 和一个 webapp。我正在使用 eclipse-wtp 插件:
allprojects {
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
}
依赖项会生成一些资源(例如,通过下载和解压缩 tarball)。依赖项有一个块来配置生成的资源目录作为输出:
def baseGenDir = "${buildDir}/generated-resources/main"
sourceSets {
main {
output.dir(baseGenDir, builtBy: 'unzipDependency')
}
}
webapp 依赖于依赖:
dependencies {
compile project(':dependency')
}
如果 I ./gradlew build
,则 webapp 的战争包含一个 dependency-0.0.1-SNAPSHOT.jar,其中包含生成的文件。
但是如果我在 Eclipse 中将 webapp 发布到 Tomcat,我会得到~/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/webapp/WEB-INF/lib/dependency.jar
它不包含生成的文件。它有我的 .java 源文件的 .class 文件,但没有来自 build/generated-resources/main 的资源。尝试加载资源失败 - 它们在 webapp 的类路径中不可用。
如何将生成的资源放入 Eclipse 为 Tomcat 生成的 jar 中?
示例项目:https ://github.com/ms1111/test-gen-resource
2018-02-12 更新
目前无法让 Buildship 运行任务来自动生成资源(#265、#266)。
以下块将告诉 Buildship 发布生成的资源:
eclipse {
wtp {
component {
// Make sure generated resources
// get included in the jars published to Eclipse's Tomcat
resource sourcePath: "build/generated-resources/main", deployPath: '/'
}
}
}
但是资源必须在 Buildship 生成时已经存在.settings/org.eclipse.wst.common.component
,所以需要一个舞蹈:
- 导入项目
- 运行完整的 gradle 构建
- 为具有生成源的任何模块刷新 Gradle
- 清理 Tomcat 服务器