简体   繁体   English

如何只将特定的jars从libs包含到gradle build中?

[英]How to include only specific jar from libs into gradle build?

I'm using gradle V4.10.2 and eclipse for building WAR files. 我正在使用gradle V4.10.2eclipse构建WAR文件。 I have some common JAR files for all the wars. 对于所有战争,我都有一些通用的JAR文件。 I need them to use only during compile time and need to exclude them while building into WAR file. 我需要它们仅在编译时使用,并且在构建到WAR文件时需要排除它们。 Also I don't want to exclude all the JAR s file. 另外,我也不想排除所有JAR文件。

I've put all the JARs in libs folder. 我已将所有JAR文件放入libs文件夹中。 In build.gradle , I have specified only the JARs to be included. build.gradle ,我仅指定要包含的JAR。 Since the exclude list is bigger then include list of JARs I'm not using exclude 由于排除列表更大,所以我不使用exclude包括JAR列表

war {
    rootSpec.include("**/test.jar")
}

The above results the WAR file without META-INF folder. 以上结果是没有META-INF文件夹的WAR文件。 Hence, webapp is not started. 因此,webapp无法启动。 I have tried adding the libs as reference lib in eclipse but that results in compilation error. 我试图在eclipse中将这些库添加为reference lib ,但这会导致编译错误。

Execution failed for task ':compileJava'.
Compilation failed; see the compiler error output for details

So I have to include all the JARs in libs folder and then need to use only required JARs in the WAR built. 因此,我必须在libs文件夹中包含所有JAR,然后在构建的WAR中仅使用所需的JAR。 How to include only specific JARs into build? 如何仅将特定的JAR纳入构建?

I strongly recommend reading about building Java projects with Gradle and in particular the part about web applications 我强烈建议您阅读有关使用Gradle构建Java项目的信息,尤其是有关Web应用程序的部分

In short you should add the JARs needed for compilation only to the compileOnly configuration and the JARs needed for compilation and at runtime in the implementation configuration. 简而言之,您应仅将编译所需的JAR添加到compileOnly配置中,并将编译所需的JAR 运行时添加到implementation配置中。 Gradle will then use all of them for compilation and only include the ones in implementation when packaging the WAR. 然后Gradle将使用它们全部进行编译,并且在打包WAR时仅将其包含在implementation

While you can do that by selecting them from your libs folder, I strongly recommend moving to libraries and its metadata sourced from a repository, private or public, so that the dependency management is done for you. 尽管您可以从libs文件夹中选择它们,但我强烈建议移至库及其元数据(来自私有或公共存储库),以便为您完成依赖项管理。

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

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