Java Gradle:如何从 WAR 中排除 JAR?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20707543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:43:56  来源:igfitidea点击:

Gradle: How to exclude JAR from a WAR?

javajargradlewar

提问by isobretatel

I have a multi-project Gradle build structure, where child project depends on a JAR, which I don't want to be in WAR file. I tried "exclude" but it does not work.

我有一个多项目 Gradle 构建结构,其中子项目依赖于一个 JAR,我不想在 WAR 文件中。我试过“排除”但它不起作用。

The main project script:

主要项目脚本:

apply plugin: 'war'
war {
    dependencies {
        runtime (project(':childProject')) {
            exclude group: 'javax.servlet.jsp', module: 'jsp-api'
        }
    }
}

The childProject script:

childProject 脚本:

apply plugin: 'java'
dependencies {
    compile 'javax.servlet.jsp:jsp-api'
}

回答by John Vint

From the Gradle documentation

来自 Gradle 文档

The War plugin adds two dependency configurations: providedCompile and providedRuntime. Those configurations have the same scope as the respective compile and runtime configurations, except that they are not added to the WAR archive.

War 插件增加了两个依赖配置:providedCompile 和providedRuntime。这些配置与各自的编译和运行时配置具有相同的范围,只是它们没有添加到 WAR 存档中。

So, in other words, adding an entry to providedCompileor providedRuntimewill cause that dependency to be excluded from the war file.

因此,换句话说,添加条目providedCompileprovidedRuntime将导致该依赖项从战争文件中排除。

  • use providedCompileif you have source that relies on some classes for compiling
  • use providedRuntimeif you use it for testing and not compiling.
  • providedCompile如果您有依赖某些类进行编译的源,请使用
  • 使用providedRuntime,如果你用它来测试,而不是进行编译。

http://www.gradle.org/docs/current/userguide/war_plugin.html

http://www.gradle.org/docs/current/userguide/war_plugin.html

Example

例子

providedCompile "javax.servlet:servlet-api:2.5"

providedCompile "javax.servlet:servlet-api:2.5"

回答by Jake W

I realised that providedCompile sometimes introduced dependencies issues for me, e.g. some classes are not found. I then figured out a more flexible solution.

我意识到providedCompile 有时会为我带来依赖问题,例如找不到某些类。然后我想出了一个更灵活的解决方案。

We can just configure required dependencies as 'compile' and then exclude specific jars from the war file by using the following war configuration:

我们可以将所需的依赖项配置为“编译”,然后使用以下 war 配置从 war 文件中排除特定的 jar:

war {
    classpath = classpath.filter { file ->
        println file.name
        (
            !file.name.startsWith('gwt-dev') &&
            !file.name.startsWith('gwt-user') &&
            !file.name.startsWith('guava-gwt') &&
            !file.name.startsWith('gwtbootstrap3') &&
            !file.name.startsWith('gwtbootstrap3-extras') &&
            !file.name.startsWith('servlet-api')
        )
    }
}

So far, I found this is the cleanest solution for me. I hope it helps.

到目前为止,我发现这对我来说是最干净的解决方案。我希望它有帮助。

回答by d-sauer

I'm doing it this way.

我就是这样做的。

war {   
    rootSpec.exclude("**/some-*.jar")
}

回答by Adrodoc55

I had the same problem but i found a generic solution based on the one of Jake W.

我遇到了同样的问题,但我找到了一个基于 Jake W.

In your child-project, without the war plugin, you add your own providedCompile and providedRuntime like this:

在您的子项目中,如果没有 war 插件,您可以像这样添加自己的providedCompile 和providedRuntime:

configurations {
  providedCompile
  providedRuntime.extendsFrom providedCompile
}

plugins.withId('java') {
  configurations {
    compile.extendsFrom providedCompile
    runtime.extendsFrom providedRuntime
  }
}

In the project with your war file you copy this anywhere you want:

在带有 war 文件的项目中,您可以将其复制到您想要的任何位置:

configurations.runtime.allDependencies.withType(ProjectDependency) { ProjectDependency dep ->
  Project proj = dep.dependencyProject
  evaluationDependsOn(proj.path)
  Configuration cfg = proj.configurations.findByName('providedRuntime')
  if (cfg != null){
    war {
      classpath -= cfg
    }
  }
}