Gradle -> How to omit some jars from WEB-INF/lib
Asked Answered
B

2

9

I have a subproject with a war spec that looks like this:

war {
    from('resources')  {
        include '*.properties'
        into 'WEB-INF/classes/'
    }    
    webXml = file('src/main/webapp/WEB-INF/web.xml')
}

Works great. Creates a single, fat war file that is deployable to Tomcat. Problem is, when deploying to TomEE and JBoss, I run into conflicts (ie with Javax Servlet, Jersey, etc). So I want to exclude a set of jars from being war'd. I looked at the Gradle war documentation, and it looks like I need to use excludes for this. I tried it two different ways, and the jars are not excluded from the war:

war {

    // copy properties file in classes so that 
    // they may be loaded from classpath
    from('resources')  {
        include '*.properties'
        into 'WEB-INF/classes/'
    }

    // specify web xml
    webXml = file('src/main/webapp/WEB-INF/web.xml')

    // remove jars that conflict with TomEE
    exclude '**/javax.inject-1.jar'
    exclude '**/javax.servlet-2.5.0.v201103041518.jar'
    exclude '**/servlet-api-2.5.jar'
    exclude '**/validation-api-1.0.0.GA.jar'

}

This is in a subproject (karyon-examples) within the NetFlix/karyon project hosted on github. The dependencies in the subproject look like this:

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.0'
    runtime 'org.slf4j:slf4j-simple:1.7.0'
    compile project(':karyon-extensions')
    compile project(':karyon-admin-web')
}

And I want to avoid editing things like compile versus runtime dependencies, especially in other files and subprojects. In fact the jars I am trying to exclude above are benign when running with jetty and regular tomcat.

I simply want to exclude these jars without complicating the build scripts. What am I missing?

Thanks

Baht answered 5/4, 2013 at 19:29 Comment(0)
D
9

the obvious way to go is to move the dependencies you don't want to have in your war from the compile configuration to providedCompile and from runtime to providedRuntime. the provided configurations are added to your build when the war plugin is applied.

One other note on your snippet above: I think the exclude does not work because you referencing the target path in your exclude statements instead you should just reference it by /servlet-api-2.5.jar.

Dilly answered 5/4, 2013 at 20:33 Comment(1)
@rob -- providedRuntime is probably the way to go. It just means the library won't be added to the war file. You might be able to use exclude but unfortunately it is undocumented and very confusing on exactly how to do it.Psych
C
0

you can this way

dependencies {
  compile fileTree(dir: 'WebContent/WEB-INF/lib', include: ['*.jar'])
  compile("org.apache.tomcat:tomcat-jni:8.0.37")
  providedRuntime("org.apache.tomcat:tomcat-jni:8.0.37")

}

Capful answered 28/10, 2016 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.