【问题标题】:Transitive Maven dependency shows up in dependency:tree but not in lib directory传递 Maven 依赖项显示在依赖项中:树但不在 lib 目录中
【发布时间】:2014-05-23 22:38:05
【问题描述】:

我正在使用 Maven 3.0.3 构建一个大型 java 项目。我的 pom.xml 有一个依赖于spring-context(编译范围)的父级,而后者又依赖于spring-expression(也在编译范围内)。当我用mvn:dependencyTree 创建一个dependency tree 时,一切看起来都很好,spring-expression 库在编译范围内变成了传递依赖项。但是,如果我使用mvn help:effective-pom 构建effective pom,则缺少spring-expression 的条目。此外,如果我构建项目,spring-expression 的库不包含在 lib 目录中,而 spring-context 的库包含。

直到最近库 包含在构建中,现在其中一个依赖项似乎发生了变化。但是 spring-context 仍然在编译范围内并显示在依赖关系树中,所以我不明白为什么这应该改变行为。

另外,更新 Maven(这似乎解决了一个可能相关的问题here)很遗憾不是一个选项。我通过添加spring-expression 作为对我的项目的直接而不是传递依赖项暂时解决了这个问题,但我不想用应该已经存在的东西堵塞pom。有什么想法吗?

更新: 越来越奇怪了……在尝试提出一个最小的示例时,我发现了一个 - 有点。这是我想出的:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.test.project</groupId>
  <artifactId>test-project</artifactId>
  <packaging>war</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>Test Project</name>

  <properties>
    <spring.version>3.2.4.RELEASE</spring.version>
  </properties>

  <dependencies>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
</project>

它的工作原理是传递依赖spring-expression 显示在mvn:dependencyTree 的输出中:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Test Project 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ test-project ---
[INFO] org.test.project:test-project:war:0.1-SNAPSHOT
[INFO] \- org.springframework:spring-context:jar:3.2.4.RELEASE:runtime
[INFO]    +- org.springframework:spring-aop:jar:3.2.4.RELEASE:runtime
[INFO]    |  \- aopalliance:aopalliance:jar:1.0:runtime
[INFO]    +- org.springframework:spring-beans:jar:3.2.4.RELEASE:runtime
[INFO]    +- org.springframework:spring-core:jar:3.2.4.RELEASE:runtime
[INFO]    |  \- commons-logging:commons-logging:jar:1.1.1:runtime
[INFO]    \- org.springframework:spring-expression:jar:3.2.4.RELEASE:runtime
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.922s
[INFO] Finished at: Thu Apr 10 16:17:42 CEST 2014
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------

而不是在有效的pom中:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.test.project</groupId>
  <artifactId>test-project</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Test Project</name>
  <distributionManagement>
    <repository>
      <id>internal_repo</id>
      <name>Repository for internal artefacts</name>
      <url>http://internal/url</url>
    </repository>
    <snapshotRepository>
      <id>internal_repo</id>
      <name>Repository for internal artefacts</name>
      <url>http://internal/url</url>
    </snapshotRepository>
  </distributionManagement>
  <properties>
    <downloadJavadocs>true</downloadJavadocs>
    <downloadSources>true</downloadSources>
    <spring.version>3.2.4.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>central</id>
      <url>http://internal/url</url>
    </repository>
    <repository>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>maven3</id>
      <url>http://internal/url</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>central</id>
      <url>http://internal/url</url>
    </pluginRepository>
    <pluginRepository>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>maven3</id>
      <url>http://internal/url</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>c:\projects\test-project\src\main\java</sourceDirectory>
    <scriptSourceDirectory>c:\projects\test-project\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>c:\projects\test-project\src\test\java</testSourceDirectory>
    <outputDirectory>c:\projects\test-project\target\classes</outputDirectory>
    <testOutputDirectory>c:\projects\test-project\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>c:\projects\test-project\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>c:\projects\test-project\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>c:\projects\test-project\target</directory>
    <finalName>test-project-0.1-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.7.2</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
          <execution>
            <id>default-war</id>
            <phase>package</phase>
            <goals>
              <goal>war</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>2.0.1</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>c:\projects\test-project\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>c:\projects\test-project\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>c:\projects\test-project\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>c:\projects\test-project\target\site</outputDirectory>
  </reporting>
</project>

然而当我构建项目时,lib 目录确实在这个例子中包含spring-expression。这怎么可能?

【问题讨论】:

  • 如何生成“lib 目录”?阴影插件,应用程序插件或其他什么?问题不在于传递依赖,问题在于生成“lib目录”的进程(或其配置)。
  • 发布您的pom.xml(s),以便人们可以看到发生了什么。展示你的树。
  • @OlegEstekhin 我不是 Maven 专家,但据我所知,lib 目录是由 maven-war-plugin 构建的。
  • @carlspring 我无法发布 pom(因为这是一个闭源项目),但我会尝试提出一个演示问题的最小示例。
  • 好吧,无论是否封闭源代码,您都需要提供一些可以使用的东西。 :)

标签: java maven dependencies maven-3 pom.xml


【解决方案1】:

我正在使用 maven 3.1.1 运行,并且依赖项 spring-expression 已正确添加到 WEB-INF/lib。有效的 pom 不包含spring-expression,但这是正常的,因为有效的 pom 不解析传递依赖。

有效的 pom 只是一个合并的 xml 文件,当前 pom 加上它的所有父级合并在一起。只有在构建了有效的 pom 之后,才会应用 maven 依赖解析机制。

这就是为什么有效的 pom 只包含 spring-context 依赖的原因,因为那是 pom.xml 中唯一指定的东西。如果父 pom 中存在父依赖项,它们也会出现在有效 pom 中。

这是我的 mvn 依赖输出:树:

INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-project ---
[INFO] org.test.project:test-project:war:0.1-SNAPSHOT
[INFO] \- org.springframework:spring-context:jar:3.2.4.RELEASE:runtime
[INFO]    +- org.springframework:spring-aop:jar:3.2.4.RELEASE:runtime
[INFO]    |  \- aopalliance:aopalliance:jar:1.0:runtime
[INFO]    +- org.springframework:spring-beans:jar:3.2.4.RELEASE:runtime
[INFO]    +- org.springframework:spring-core:jar:3.2.4.RELEASE:runtime
[INFO]    |  \- commons-logging:commons-logging:jar:1.1.1:runtime
[INFO]    \- org.springframework:spring-expression:jar:3.2.4.RELEASE:runtime

这些是我的 WEB-INF/lib 内容:

aopalliance-1.0.jar
commons-logging-1.1.1.jar
spring-aop-3.2.4.RELEASE.jar
spring-beans-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar

所以问题似乎是对有效 pom 功能的一些误解,以及由于 maven 错误导致 WEB-INF lib 中缺少依赖项spring-expression:这个问题可以通过版本升级或对 pom 的硬编码依赖作为解决方法来解决如你所见。

【讨论】:

  • 好吧,看来我确实误解了有效的pom机制。谢谢你的解释。但是,我仍然不知道为什么不再提取 lib;依赖项中肯定发生了一些变化,但我不知道为什么它会停止拉动spring-expression。我会努力寻找更多的信息来提供,以便找到原因......
  • 也许你的 maven 存储库中有些东西搞砸了。您是否尝试过删除 .m2/org/springframework 并进行干净运行?
  • @Wouter 是的,我试过了。没用但是我现在不能再重现这个问题了……嗯,它有效。如果我再次偶然发现这一点,我会尝试找出当时发生的事情。
  • 可能是缓存问题。如果您再次遇到类似的事情,请尝试 mvn -U。
【解决方案2】:

当您构建大型 java 项目时,Lib spring-expression 不在 lib 目录中,但在您的最小示例中。而且我注意到你使用的是maven 3.0.3,这个问题是由这个错误MNG-5121引起的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 2021-09-27
    • 2013-02-17
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    相关资源
    最近更新 更多