maven-2 - 我只需要一个依赖模块,但不需要它的依赖项

标签 maven-2 maven

在我的项目中,有一个 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/

相关文章:

java - JSP 中的 EL 停止评估

java - JBoss 上的热部署失败 - "Delete method not implemented"

maven-2 - Mac OS X Mavericks 上我的 m2 文件夹在哪里

java - 以批处理模式发布 - 将依赖项版本更改为 SNAPSHOTS

maven-surefire-report-plugin 没有获取其配置

java - 如何修复 Java 中的插件依赖错误

javascript - 用于分析 javascript 代码质量的 Maven 插件

java - 如何在我家附近找到更近的maven存储库

java - 应用程序版本未显示在 Spring Boot banner.txt 中

Maven war 在两个地方有 META-INF 文件夹