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.
The main project script:
apply plugin: 'war'
war {
dependencies {
runtime (project(':childProject')) {
exclude group: 'javax.servlet.jsp', module: 'jsp-api'
}
}
}
The childProject script:
apply plugin: 'java'
dependencies {
compile 'javax.servlet.jsp:jsp-api'
}
Transitive dependencyA variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.
From the Gradle documentation
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.
So, in other words, adding an entry to providedCompile
or providedRuntime
will cause that dependency to be excluded from the war file.
providedCompile
if you have source that relies on some classes for compilingprovidedRuntime
if you use it for testing and not compiling.http://www.gradle.org/docs/current/userguide/war_plugin.html
Example
providedCompile "javax.servlet:servlet-api:2.5"
I'm doing it this way.
war {
rootSpec.exclude("**/some-*.jar")
}
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.
We can just configure required dependencies as 'compile' and then exclude specific jars from the war file by using the following war configuration:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With