Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Add single file to JAR

I have the file "license.txt" in the root directory of my project. In the jar-task, I want to add this file to the (root folder of the) JAR file.

I tried

jar {
  from '.' include 'license.txt'
}

but this replaces the other content (.class files) instead of adding a file. And I do not want to add the license.txt to the resources folder, because I do not want to change my project structure just because of the build tool.

Who can help? Thank you!

like image 876
Andi Avatar asked Sep 13 '12 16:09

Andi


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.

What is flatDir in gradle?

repositories { flatDir { dirs("lib") } flatDir { dirs("lib1", "lib2") } } This adds repositories which look into one or more directories for finding dependencies. This type of repository does not support any meta-data formats like Ivy XML or Maven POM files.

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.


2 Answers

To add a single file, you can simply do:

jar {
    from "license.txt"
}

Your solution should also work if you scoped your include to your from by enclosing it in curly braces.

like image 187
Peter Niederwieser Avatar answered Sep 17 '22 17:09

Peter Niederwieser


If you would like to add multiple files, you can do:

jar{
    from{
        ["aaa.txt","bbb.txt"]
    }
}
like image 35
Reed Grey Avatar answered Sep 21 '22 17:09

Reed Grey