Gradle - 我可以在项目依赖项中包含任务的输出 [英] Gradle - can I include task's output in project dependencies
问题描述
我有一个任务从这些源生成java源文件和一组jar文件(比如项目 a
)。我想将这些jar导出到依赖项目(比如项目 b
)。所以这里大概是我现在所拥有的:
// a.gradle
配置{
generatedJars
}
任务generateJars(类型:JavaExec){
//生成罐子...
outputs.files + = //在这里附加生成的jar文件
}
依赖项{
generatedJars generateJars.outputs.files
}
//b.gradle
依赖关系{
项目(路径:':a',配置:'generatedJars')
}
它工作正常,但添加 generateJars.outputs.files
作为依赖项当没有生成jar时,不告诉gradle它必须运行 generateJars
任务。我曾尝试将任务本身添加为依赖项,希望它能以与将工件/压缩任务添加到工件配置时相同的方式工作(例如 artifacts {myJarTask}
),但它会抛出一个错误,告诉我我无法做到这一点。当然,在:b
开始评估之前,我可以在构建过程的某个位置注入 generateJars
任务,但那样笨拙而脆弱,所以我想避免它。
我觉得我应该将生成的jar添加到 artifacts {...} $该项目的c $ c>,但我不确定如何使它们对依赖项目可见。有没有更好的方法来实现这一点?
依赖项目(项目 b
)需要安装IntelliJ IDEA模块类路径指向项目 a
的生成罐子。 (伪代码):
// b.gradle
idea {
module {
scopes.COMPILE.plus + = project(path:':a',configuration:'generatedJars')。files
}
}
到目前为止,我尝试在 任何帮助将不胜感激! 首先,你需要一个单独的配置吗?也就是说,你是否有上添加一个项目依赖项:a
's
generatedJars
在:b
中,但Idea插件只是添加模块 :a
作为模块依赖关系,并假定它将其生成的jar(这可能是一个正确的假设)导出,因此不会将生成的jar添加到:b $ c
a
应该 看到生成的Jars的客户?如果没有,您可以将生成的Jars添加到存档
配置中,这将简化操作。依赖关系
块):
artifacts {
generatedJars generateJars
}
确保 generateJars
任务在需要时自动运行。
+ =
在 outputs.files
之后,尽管它可能没有区别。第四,为什么你需要一个 JavaExec
任务来生成Jars?你还需要添加必要的输入。 第五,IDEA没有与Gradle的项目配置依赖关系相对应的概念。您可以将生成的源添加到某个源集并让Gradle生成它们吗? IDEA模块完全依赖于另一个模块,或根本不依赖。您有两种选择:使用模块依赖关系,并将生成的源文件作为依赖模块的源文件夹(最好在Gradle和IDEA构建中),或将生成的Jars作为外部依赖项传递给IDEA。在任何一种情况下,您都应该添加一个来自 ideaModule
的任务依赖关系到相应的生成任务。如果这仍然不能达到令人满意的IDEA设置,您可以考虑将生成的JAR生成为单独的子项目。 I have a task that generates java sources and a set of jars from these sources (say, project a
). I would like to export these jars to dependent projects (say, project b
). So here's roughly what I have right now:
//a.gradle
configurations{
generatedJars
}
task generateJars(type: JavaExec) {
//generate jars ...
outputs.files += //append generated jars here
}
dependencies{
generatedJars generateJars.outputs.files
}
//b.gradle
dependencies{
project(path: ':a', configuration: 'generatedJars')
}
It works OK, except that adding generateJars.outputs.files
as a dependency does not tell gradle that it has to run generateJars
task when there are no jars generated yet. I have tried adding the task itself as a dependency hoping that it would work in the same way as it does when you add a jar/zip task to an artifact configuration (e.g. artifacts{ myJarTask }
), but it throws an error telling me that I cannot do that. Of course I can inject the generateJars
task somewhere in the build process before :b
starts evaluating, but that's clumsy and brittle, so I would like to avoid it.
I feel like I should be adding the generated jars to artifacts{ ... }
of the project, but I am not sure how to make them then visible to dependent projects. Is there a better way of achieving this?
Dependent projects (project b
) will need to do setup IntelliJ IDEA module classpath to point to project a
's generated jars. Something rather like this (pseudo-code):
//b.gradle
idea{
module{
scopes.COMPILE.plus += project(path: ':a', configuration: 'generatedJars').files
}
}
So far I have tried simply adding a project dependecy on :a
's generatedJars
in :b
, but Idea plugin simply adds module :a
as a module-dependency and assumes that it exports its generated jars (which is probably a correct assumption), therefore not adding the generated jars to :b
's classpath.
Any help would be greatly appreciated!
First, do you need a separate configuration? That is, do you have clients of a
that should not see the generated Jars? If not, you can add the generated Jars to the archives
configuration, which will simplify things.
Second, the correct way to add the generated Jars to the configuration is (instead of the dependencies
block):
artifacts {
generatedJars generateJars
}
This should make sure that the generateJars
task gets run automatically when needed.
Third, I'd omit the +=
after outputs.files
, although it might not make a difference. You should also add the necessary inputs.
Fourth, why do you need a JavaExec
task to generate the Jars? Can you instead add the generated sources to some source set and let Gradle build them?
Fifth, IDEA doesn't have a concept corresponding to Gradle's project configuration dependencies. Either an IDEA module fully depends on another module, or not at all. You have two options: either use a module dependency and make the generated sources a source folder of the depended-on module (preferably both in the Gradle and the IDEA build), or pass the generated Jars as external dependencies to IDEA. In either case, you should probably add a task dependency from ideaModule
to the appropriate generation task. If this still doesn't lead to a satisfactory IDEA setup, you could think about moving the generation of the Jars into a separate subproject.
这篇关于Gradle - 我可以在项目依赖项中包含任务的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!