Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy resource files in classes folder with gradle?

Enviroment

I am using a third party lib which requires bytecode instrumentation. The tool which does the bytecode instrumentation requires some description files and those files have to be in the same folder structure like the compiled .class files. Those files are only necessary at compiletime.

Problem

I thought gradle would place all files (resource and class) temporarily within the same folder and then create a jar from that folder. But it seems that gradle have two different places for resources and class files before assembling a jar. Like mentioned before the third party tool for code instrumentation requires the description files in the same folderstructure like the class files.

Question

Simply: How can I solve this Problem?

Ideas

  1. Place the description files with in src/main/java. Not very "clean" but could be a solution. Sadly gradle ignores those files. I tried to include them somehow but didn't get it working yet.
  2. Temporarily copy the description files to the right place. Also not a very "clean" way
like image 479
Jan Avatar asked Mar 02 '17 10:03

Jan


People also ask

What is Ziptree in Gradle?

A FileTree represents a hierarchy of files. It extends FileCollection to add hierarchy query and manipulation methods. You typically use a FileTree to represent files to copy or the contents of an archive. You can obtain a FileTree instance using Project.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

Where are Gradle dependencies stored?

The Gradle dependency cache consists of two storage types located under GRADLE_USER_HOME/caches : A file-based store of downloaded artifacts, including binaries like jars as well as raw downloaded meta-data like POM files and Ivy files.

What is Gradle build directory?

The build directory of this project into which Gradle generates all build artifacts. 4. Contains the JAR file and configuration of the Gradle Wrapper. 5. Project-specific Gradle configuration properties.

How to copy files in Gradle?

The basic process of copying files in Gradle is a simple one: 1 Define a task of type Copy 2 Specify which files (and potentially directories) to copy 3 Specify a destination for the copied files

What is Gradle and how does it work?

Almost every Gradle build interacts with files in some way: think source files, file dependencies, reports and so on. That’s why Gradle comes with a comprehensive API that makes it simple to perform the file operations you need.

Why do some Gradle tasks need to create directories?

Many tasks need to create directories to store the files they generate, which is why Gradle automatically manages this aspect of tasks when they explicitly define file and directory outputs. You can learn about this feature in the incremental build section of the user manual.

What is an archive in Gradle?

You can learn more about this in the section on child specifications. From the perspective of Gradle, packing files into an archive is effectively a copy in which the destination is the archive file rather than a directory on the file system. This means that creating archives looks a lot like copying, with all of the same features!


Video Answer


1 Answers

You can redirect the resources output dir by:

sourceSets {
  main {
    output.resourcesDir = "build/classes/main"
  }
  test {
    output.resourcesDir = "build/classes/test"
  }
}

then the folder with class files (build/classes/main) will also contain your resources.

like image 94
František Hartman Avatar answered Sep 23 '22 14:09

František Hartman