gradle管理web项目时经常需要在编译期间引用servlet-api,但又不希望将其一起发布,解决办法:
1. 引用war插件,插件中提供了providedCompile依赖
apply plugin: 'war'
2. 使用providedCompile引入servlet-api
dependencies{
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1' }
3. 生成idea工程
gradle idea
顺便吐槽一下cloudera的hive-jdbc,居然以compile依赖的方式引入了jasper包,与tomcat7版本冲突,系统无法启动。解决过程如下:
1. gradle查看依赖关系,检查是谁引入了jasper
gradle dependencies --configuration compile
2. 发现是hive-jdbc间接引用了jasper
+--- org.apache.hive:hive-jdbc:1.1.0-cdh5.13.0
| +--- org.apache.hive:hive-common:1.1.0-cdh5.13.0
| | +--- org.apache.hive:hive-shims:1.1.0-cdh5.13.0
...
| +--- org.apache.hive:hive-service:1.1.0-cdh5.13.0
| | +--- org.apache.hive:hive-metastore:1.1.0-cdh5.13.0
| | | +--- org.apache.hive:hive-serde:1.1.0-cdh5.13.0
| | | | +--- org.apache.hive:hive-common:1.1.0-cdh5.13.0 (*)
...
| | +--- org.eclipse.jetty.aggregate:jetty-all:7.6.0.v20120127 (*)
| | +--- tomcat:jasper-compiler:5.5.23
| | | +--- javax.servlet:jsp-api:2.0
| | | | \--- javax.servlet:servlet-api:2.4 -> 2.5
| | | \--- ant:ant:1.6.5
| | +--- tomcat:jasper-runtime:5.5.23
| | | +--- javax.servlet:servlet-api:2.4 -> 2.5
| | | \--- commons-el:commons-el:1.0
| | | \--- commons-logging:commons-logging:1.0.3 -> 1.2
| | +--- javax.servlet.jsp:jsp-api:2.1
3. 手动将jasper-runtime和jasper-compiler改为provided依赖
// 解决hive2 jdbc间接依赖jasper的问题,改为provided
providedCompile group: 'tomcat', name: 'jasper-compiler', version: '5+'
providedCompile group: 'tomcat', name: 'jasper-runtime', version: '5+'
gradle查询依赖关系:
gradle dependencyInsight --dependency slf4j-api
http://www.mkyong.com/gradle/gradle-display-project-dependency/
https://docs.gradle.org/current/userguide/userguide_single.html#sec:listing_dependencies