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

时间:2016-05-17 16:29:22

标签: java plugins gradle war

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

project/build.gradle
 --src/main/java
 --src/main/res

我想要做的是为project/src/main/java

中的所有java文件创建一个jar

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/'
        }
    }
}

我尝试了各种方法来排除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')
    ...
}

使用 classpath

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

使用('war')

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

直接'排除':

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

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

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

How to Exclude directory and its contents in 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

7 个答案:

答案 0 :(得分:3)

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

war {
    rootSpec.exclude('**/com/xxxxx/web/client/')
}

答案 1 :(得分:1)

我查看了文章,这段代码有效:

  from 'src/main/webapp/WEB-INF/classes'
  exclude '**/*.class'

答案 2 :(得分:0)

你对大规模的反复试验是正确的。这对我有用:

战争{

...

/* We bundle as a jar so that we can sign the jar.
 * So, we exclude our separate class files.
 */
rootSpec.exclude('**/com/**')

// include our jar file
classpath fileTree(dir:"${project.buildDir}/libs/",include:project.YourJarFileNm)

...

}

答案 3 :(得分:0)

from("./"){
        includes = [
            "a.log",
             "b.properties","src/**"             
        ]

        into 'WEB-INF/'
          }
       excludes = [
            "WEB-INF/misc/**"             
        ]

答案 4 :(得分:0)

这对我来说可以从WEB-INF中排除classes文件夹

war{
  classpath = fileTree('*').exclude('**/classes')
}

我从http://gradle.1045684.n5.nabble.com/Exclude-properties-file-from-war-td3365147.html那里得到了

答案 5 :(得分:0)

您可以尝试一下。

war {
    enabled = true
    classpath = classpath - sourceSets.main.output
    from (jar) {
        into 'WEB-INF/classes'
    }
}

https://discuss.gradle.org/t/war-task-should-allow-bundling-classes-into-jar/491

答案 6 :(得分:-2)

这对我有用:

war {
        dependsOn jar
        archiveName = 'hello.war'
        classpath = jar
    }