在我的项目中,有一个 spring XML 配置,它利用 ehcache 来缓存方法返回。
xml 片段:
http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
<ehcache:config configLocation="classpath:ehcache.xml"/>
<ehcache:proxy id="explainRetriever" refId="explainRetrieverImpl">
<ehcache:caching methodName="get*" cacheName="explainCache"/>
</ehcache:proxy>
但在运行时,服务器提示找不到 http://www.springmodules.org/schema/ehcache 的定义 ,我知道我必须将“spring-modules-cache.jar”添加到WEB-INF/lib目录中。
但是我的项目是由maven维护的,如果我在运行时依赖中添加“spring-modules-cache”,它会给我的WAR文件带来很多依赖,使我的WAR充满很多不必要的jar。我只需要其中一个声明,而不是它的所有依赖项...
有什么方法可以告诉maven不要将其依赖项包含到项目中吗?
或者...另一种方式,打包WAR时,只需将另一个准备好的spring-modules-cache.jar复制到WEB-INF/lib中,该怎么做?谢谢!
最佳答案
如上所述,您可以使用排除机制,但这基本上意味着排除依赖项的 pom 中声明的每个运行时依赖项,这不仅很痛苦,而且很脆弱,因为您必须不断跟踪依赖项的 pom 才能保留列表最新的排除项。
另一种方法是将依赖项标记为 <scope>provided</scope>
然后使用 dependency:copy
将依赖项自己复制到“target”下的war构建目录中插件目标。默认情况下内置 war
${project.build.directory}/${project.build.finalName}
。请参阅war plugin了解详情。
例如
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.springmodules</groupId>
<artifactId>spring-modules-cache</artifactId>
<version>0.8</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
关于maven-2 - 我只需要一个依赖模块,但不需要它的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2954029/