Gradle - 将单个文件添加到JAR

时间:2021-06-06 13:29:20

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.

我的项目根目录中有“license.txt”文件。在jar-task中,我想将此文件添加到JAR文件的(根文件夹)中。

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.

但这会替换其他内容(.class文件)而不是添加文件。而且我不想将license.txt添加到resources文件夹中,因为我不想仅仅因为构建工具而更改我的项目结构。

Who can help? Thank you!

谁可以帮忙?谢谢!

2 个解决方案

#1


14  

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.

如果您将include包含在花括号中,那么您的解决方案也应该有效。

#2


6  

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

如果要添加多个文件,可以执行以下操作:

jar{
    from{
        ["aaa.txt","bbb.txt"]
    }
}

#1


14  

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.

如果您将include包含在花括号中,那么您的解决方案也应该有效。

#2


6  

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

如果要添加多个文件,可以执行以下操作:

jar{
    from{
        ["aaa.txt","bbb.txt"]
    }
}