简体   繁体   English

Gradle编译生成的java文件

[英]Gradle compile generated java file

I'm trying to convert a big project to use gradle from the current state which uses makefiles . 我正在尝试将一个大项目转换为使用当前使用makefiles状态的gradle

I have hit a wall though at some point. 我在某个时候碰到了一堵墙。 Suppose we have the following directory tree (a lot of stuff omitted for simplicity): 假设我们有以下目录树(为简单起见省略了很多东西):

root
|-- src
|    +-- main
|         +-- java
|              +-- com
|                  +-- app
|                      |-- a
|                      |-- b
|                      +-- c
+-- build.gradle

Now lets suppose that in package com.app.c exists a java file with a main class which is being used to generate a new java file under the same directory which then should be compiled as well. 现在让我们假设com.app.c包中存在一个带有主类的java文件,该主文件用于在同一目录下生成一个新的java文件,然后编译该目录。 Package com.app.c has dependencies on the other packages as well. com.app.c也依赖于其他包。

With a makefile you can do something like the following (a simple algorithm): 使用makefile,您可以执行以下操作(一个简单的算法):

  1. Compile packages `com.app.a` and `com.app.c`. 编译包`com.app.a`和`com.app.c`。
  2. Move to the directory of package `com.app.b` and compile only the files required to create the generated file. 移动到包com.app.b`包的目录,只编译创建生成文件所需的文件。
  3. Create the generated file by running the main function in the file used for generation. 通过在用于生成的文件中运行main函数来创建生成的文件。
  4. Compile the whole `com.app.b` package which now includes the generated file. 编译整个`com.app.b`包,现在包含生成的文件。

Using gradle though I cannot do something like that (or at least I don't think I can). 使用gradle虽然我不能做那样的事情(或者至少我认为我不能)。

What I thought of doing was create a JavaExec task that will run the file that produces the generated java file and make it run after compileJava and also finalized with compileJava. 我想做的是创建一个JavaExec任务,它将运行生成生成的java文件的文件,并使其在compileJava之后运行,并使用compileJava完成。 But obviously that is a circular dependency between the tasks and a dead end. 但显然这是任务与死胡同之间的循环依赖。

Has anyone ever done or met something similar? 有没有人做过或遇到类似的东西? If anyone could help I would appreciate it. 如果有人可以提供帮助,我会很感激。

Note that I cannot easily move the file generation out of that file as it has some deep dependencies... 请注意,我无法轻松地将文件生成移出该文件,因为它具有一些深度依赖性...

It looks like I didn't search that good. 看起来我没有那么好搜索。

Using this solution works in this case as well. 在这种情况下,使用解决方案也适用。

The difference is that you have to have the following configuration: 不同之处在于您必须具有以下配置:

task generateFile(type: JavaExec) { ... }

task compileGeneratedFile(type: JavaCompile) { ... }

generateFile.mustRunAfter compileJava
generateFile.finalizedBy compileGeneratedFile
generateFile.onlyIf { !file("path/to/file").exists() }

This seems to do the trick! 这似乎可以解决问题! Just posting the answer so that anyone with the same issue would find it easier. 只需发布答案,以便任何有相同问题的人都会发现它更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM