用maven管理庫依賴,有個好處就是連同庫的依賴的全部jar文件一起下載,免去手工添加的麻煩,但同時也帶來了同一個jar會被下載了不同版本的問題,好在pom的配置里面允許用<exclusion>來排除一些不需要同時下載的依賴jar 。
比如配置struts-core,它會同時下載javassist和asm相關的jar,但版本又不夠新,這時可以排除它們:
<!-- Struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> <exclusions> <exclusion> <groupId>javassist</groupId> <artifactId>javassist</artifactId> </exclusion> <exclusion> <!-- we prefer our explicit version, though it should be the same --> <groupId>asm</groupId> <artifactId>asm</artifactId> </exclusion> <exclusion> <!-- we prefer our explicit version, though it should be the same --> <groupId>asm</groupId> <artifactId>asm-commons</artifactId> </exclusion> <exclusion> <!-- we prefer our explicit version, though it should be the same --> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> </exclusion> <exclusion> <!-- we prefer our explicit version, though it should be the same --> <groupId>org.ow2.asm</groupId> <artifactId>asm-commons</artifactId> </exclusion> </exclusions> </dependency>
