问题
I have a fairly simple requirement. I have the following project layout:
project/build.gradle
--src/main/java
--src/main/res
What I would like to do is create a jar for all the java files in project/src/main/java
Include this jar file in the war and then exclude the WEB-INF/classes
from the war file
My build.gradle
look as follows:
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/'
}
}
}
I have tried various things to exclude WEB-INF/classes
including the following snippets:
Using rootSpec.exclude:
war {
dependsOn jar
archiveName = 'hello.war'
classpath fileTree(dir:'build/libs/',include:'*.jar')
classpath configurations.compile
rootSpec.exclude ('WEB-INF/classes')
...
}
Using classpath:
war {
dependsOn jar
archiveName = 'hello.war'
classpath fileTree(dir:'build/libs/',include:'*.jar')
classpath configurations.compile
classpath fileTree(dir:'build/classes/', exclude:'*.class')
...
}
Using from ('war')
:
war {
dependsOn jar
archiveName = 'hello.war'
classpath fileTree(dir:'build/libs/',include:'*.jar')
classpath configurations.compile
from ('war') {
exclude('WEB-INF/classes')
}
}
Doing a direct 'exclude':
war {
dependsOn jar
archiveName = 'hello.war'
classpath fileTree(dir:'build/libs/',include:'*.jar')
classpath configurations.compile
exclude('WEB-INF/classes')
...
}
Which were all the suggestions I found online - but none of them seems to work. Any ideas what I am doing wrong.
Here are links to some of the articles and code I found: 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
The gradle version is 2.12.
回答1:
Below is what worked for me, but you will still see an (harmless) empty package in the war bundle:
war {
rootSpec.exclude('**/com/xxxxx/web/client/')
}
回答2:
I looked to the articles and this code works:
from 'src/main/webapp/WEB-INF/classes'
exclude '**/*.class'
回答3:
You're right about the massive trial and error. This worked for me:
war {
...
/* 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)
...
}
回答4:
from("./"){
includes = [
"a.log",
"b.properties","src/**"
]
into 'WEB-INF/'
}
excludes = [
"WEB-INF/misc/**"
]
回答5:
This worked for me to exclude the classes folder from WEB-INF
war{
classpath = fileTree('*').exclude('**/classes')
}
I got this from http://gradle.1045684.n5.nabble.com/Exclude-properties-file-from-war-td3365147.html
回答6:
you can try this.
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
回答7:
This works for me:
war {
dependsOn jar
archiveName = 'hello.war'
classpath = jar
}
来源:https://stackoverflow.com/questions/37281759/gradle-how-to-exclude-web-inf-classes-from-war