Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including generated code in JAR with Gradle

Tags:

gradle

I wrote a simple gradle task to generate thrift files:

task generateThrift << {
  thriftFiles = fileTree(dir: 'src/main/thrift').matching { include '**/*.thrift' }
  exec {
    executable = 'thrift'
    args = ['--gen', 'java:hashcode', '-o', '/tmp', thriftFiles.collect { relativePath(it) }.join(",") ]
  }
}

This works fine for me. What I want to do is hook it into the build process so the stubs are including in my JAR file. I'm having trouble finding a good example of where to hook this in, and where to write the files out to so that they are included in my JAR. What's the best way to do this or a project that has an example?

like image 575
Eric Hauser Avatar asked Mar 16 '11 19:03

Eric Hauser


People also ask

How do I add a dependency in gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

How do I create a Gradle project in IntelliJ jar?

Create an executable JAR file gradle file to open it in the editor. in the editor to load the changes to your project. In the Gradle tool window, open the project's node, then the Tasks node and double-click the build task to run it. IntelliJ IDEA creates the build directory that contains our JAR file.


1 Answers

I suggest to write the files to a subdirectory of the build output directory, say thrift-stubs. Then you can include them in the Jar like so:

jar {
  from "$buildDir/thrift-stubs"
}
like image 156
Peter Niederwieser Avatar answered Oct 12 '22 18:10

Peter Niederwieser