简体   繁体   English

Gradle 包含目录下的所有文件(“递归”)

[英]Gradle include all files under a directory ('recursively')

So I've been making a Java project using Gradle ( GitHub ), but it uses/needs JNI (natives).所以我一直在使用 Gradle ( GitHub ) 制作一个 Java 项目,但它使用/需要 JNI (natives)。

I was first using Makefiles to compile and link the C++ code, but then I found out how to compile and link C++ natives using Gradle, so I got all of that working.我首先使用 Makefiles 来编译和链接 C++ 代码,但后来我发现了如何使用 Gradle 编译和链接 C++ 本机,所以我完成了所有这些工作。 But now I am stuck, because I can't find a way to include all natives, on the same level (base) inside of the JAR file.但是现在我被困住了,因为我找不到JAR 文件的同一级别(基础)上包含所有本地人的方法。 NOTE: I don't need help with compiling the natives, only with including/packaging them.注意:我不需要帮助编译本机,只需要包含/打包它们。

EDIT: I just commited the changes to GitHub.编辑:我刚刚将更改提交到 GitHub。

This is my directory structure:这是我的目录结构:

[Project Directory]
 - build.gradle
 - src/
 - win32/ (the only native library that i currently have)
     - cpp/
 - main/
     - java/
 - build/
   - libs/ (here is the JAR and the natives)
     - win32/ (the natives)
       - shared/ (the dynamic link libraries, i only want these)
          - x64/ (i want to include both x64 and x86)
             - mylibrary.dll (just the DLLs should be included)
             - mylibrary.ext
             - mylibrary.lib
          - x86/

So there are a few criteria:所以有几个标准:

  • I only want the DLL files, none of the .ext and .lib stuff.我只想要 DLL 文件,没有.ext.lib的东西。
  • I want to be able to dynamically change the amount of libraries and the names of the natives.我希望能够动态更改库的数量和本地人的名称。

What I have tried:我试过的:

My first attempt was just looping through all folders.我的第一次尝试只是遍历所有文件夹。 I didn't have to use recursion because the depth of the file structure is fixed, so it will never be further from or closer to the build/libs directory.我不必使用递归,因为文件结构的深度是固定的,所以它永远不会离build/libs目录更远或更近。 This is how I tried coding it:这就是我尝试编码的方式:

sourceSets {
    main {
        resources {
            srcDirs "src/main/resources"

            // include natives
            String libfp = "${buildDir}/libs/"
            File   libf  = new File(libfp);
            if (!libf.exists())
                libf.mkdir();

            FileFilter isDir = f -> f.isDirectory();
            FileFilter isDll = f -> f.getAbsolutePath().endsWith(".dll");

            for (File file : libf.listFiles(isDir)) { // iterate "libs" to find all libraries
                // enter "shared"
                File filen = new File(file.getAbsolutePath() + "/shared/"); 
                for (File file1 : filen.listFiles(isDir)) { // iterate over "shared" to find all platforms
                    for (File file2 : file1.listFiles(isDll)) { // get all dlls
                        include(file2.getAbsolutePath())
                    }
                }
            }
        }
    }
}

This worked, except from the including itself.这有效,除了包括本身。 I don't know if I understand how this works correctly, but the include function didn't seem to add anything to the resources.我不知道我是否理解它是如何正常工作的,但include函数似乎没有向资源添加任何内容。

Then, I checked the documentation and found it was a pattern based function, so I tried making a simple include call with the pattern I thought would work:然后,我检查了文档,发现它是一个基于模式的函数,所以我尝试使用我认为可行的模式进行简单include调用:

include "/build/libs/**/*.dll"
// I also tried the following:
include "/build/libs/**.dll"
include "/build/libs/*.dll"

But that didn't seem to work too.但这似乎也不起作用。 So I think I am just misunderstanding how the include function works.所以我想我只是误解了include函数的工作原理。

Just use只需使用

include '/build/libs/**'

will work.将工作。 Thanks.谢谢。

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

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