Problems during build when referencing static lib directory gradle

怎甘沉沦 提交于 2019-12-12 00:09:40

问题


I use a build.gradle file which needs to reference some jar files which are neither available on a public nor an internal repository.

Because of that I put the jar files into a lib directory which is below my Eclipse project:

dependencies {
    ...
    compile files('lib/*.jar')
}

This works fine as long as I run or export my program from within Eclipse. But when I try gradle build gradle fails with this error:

Could not normalize path for file 'C:\Workspaces\MyProject\project\lib\*.jar'.

I changed the path to lib\*.jar, added a leading ./, removed the \*.jar but to no avail.

When I remove the compile files line from the dependencies and add this to repositories instead

repositories {
    ...
    flatDir { dirs './lib' }
}

the compiler complains about error: cannot find symbol, and all these symbols are in the jars referenced from the lib directory. When I run gradle --debug I can see from the log that the directory referenced by flatDir is being searched in:

10:02:20.587 [DEBUG] [org.gradle.api.internal.artifacts.repositories.resolver.ResourceVersionLister] using org.gradle.internal.resource.transport.file.FileResourceConnector@48499739 to list all in /C:/...../lib/ 
10:02:20.592 [DEBUG] [org.gradle.api.internal.artifacts.repositories.resolver.ResourceVersionLister] found 7 urls

7 is the count of files in the lib directory.

Why does this work in Eclipse (STS 3.7 with gradle tooling) and not from the command line ? I tried both gradle 2.2.1 and 2.5.


回答1:


You need to use a FileTree instead of a FileCollection:

compile fileTree(dir:'lib', include:'*.jar')

The reason why eclipse behaved differently than your gradle is most likely because you didn't run gradle eclipse (a task added by the eclipse plugin). Eclipse does not interact with you build.gradle at all naturally. The eclipse task updates your eclipse to be in snyc with gradle.




回答2:


I had a hard time finding this, so I will keep my own question and write down what solved the problem.

Obviously there is some kind of different behavior which makes the command line gradle hickup when combining path and wildcards.

This is the correct syntax, which works for Eclipse and gradle on the command line:

dependencies {
    compile fileTree(dir: 'lib', include: ['*.jar'])
}

I took this from another post here on stackoverflow which I didn't find during my initial research.



来源:https://stackoverflow.com/questions/31311386/problems-during-build-when-referencing-static-lib-directory-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!