gradle - 如何从战争中排除 WEB-INF/类

py *_*y y 3 java plugins war gradle

我有一个相当简单的要求。我有以下项目布局:

project/build.gradle
 --src/main/java
 --src/main/res
Run Code Online (Sandbox Code Playgroud)

我想做的是为所有的java文件创建一个jar project/src/main/java

war中包含这个jar文件,然后从war文件中排除WEB-INF/classes

我的build.gradle样子如下:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'jetty'
apply plugin: 'war'

dependencies {
..
    compile 'com.fasterxml.jackson.core:jackson-core:2.3.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.0'
..
}

jar {
    archiveName = 'hello.jar'
}

war {
    dependsOn jar
    archiveName = 'hello.war'
    classpath fileTree(dir:'build/libs/',include:'*.jar')
    classpath configurations.compile
    webInf {
        from ('src/main/res') {
            include '**/*.*'
            into 'resources/'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了各种排除方法,WEB-INF/classes包括以下片段:

使用rootSpec.exclude

war {
    dependsOn jar
    archiveName = 'hello.war'
    classpath fileTree(dir:'build/libs/',include:'*.jar')
    classpath configurations.compile
    rootSpec.exclude ('WEB-INF/classes')
    ...
}
Run Code Online (Sandbox Code Playgroud)

使用类路径

war {
    dependsOn jar
    archiveName = 'hello.war'
    classpath fileTree(dir:'build/libs/',include:'*.jar')
    classpath configurations.compile
    classpath fileTree(dir:'build/classes/', exclude:'*.class')
    ...
}
Run Code Online (Sandbox Code Playgroud)

使用自('war')

war {
    dependsOn jar
    archiveName = 'hello.war'
    classpath fileTree(dir:'build/libs/',include:'*.jar')
    classpath configurations.compile
    from ('war') {
       exclude('WEB-INF/classes')
    }
}
Run Code Online (Sandbox Code Playgroud)

做一个直接的“排除”:

war {
    dependsOn jar
    archiveName = 'hello.war'
    classpath fileTree(dir:'build/libs/',include:'*.jar')
    classpath configurations.compile
    exclude('WEB-INF/classes')
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是我在网上找到的所有建议 - 但它们似乎都不起作用。任何想法我做错了什么。

以下是我找到的一些文章和代码的链接: Gradle: War Task has Conflicting Includes/Excludes

如何在gradle war中排除目录及其内容

https://github.com/veny/smevente/blob/master/build.gradle

https://discuss.gradle.org/t/how-can-i-exlude-files-from-war-when-using-war-plugin-against-default-or-webappdirname-location/7366/6

gradle这个版本是2.12

Hri*_*nov 5

以下是对我有用的内容,但您仍会在 war 包中看到一个(无害的)空包:

war {
    rootSpec.exclude('**/com/xxxxx/web/client/')
}
Run Code Online (Sandbox Code Playgroud)

  • 遇到了同样的问题,这对我也有用...放置 `exclude 'blabla'` 和 `rootSpec.exclude('blabla')` 之间有什么区别? (2认同)