简体   繁体   中英

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

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

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 .

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")

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM