Maven在pom文件中下载未指定的jar包文件

8

我正在使用Maven作为项目依赖管理工具。我的项目中有一个特定版本的XML文件,但我也下载了旧版本。

在这里,我想通过以下方式下载 spring-core 5.0.8:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.8.RELEASE</version>
</dependency>

它也下载了spring-core的3.0.6.RELEASE版本。如何跳过这个依赖项?这是我的旧pom,它也下载了3.0.6版本。
这是我的pom.xml文件:
<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>com.guru.gurusahakari</groupId>
<artifactId>gurusahakari</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>



<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <tomee.version>2.0.0-SNAPSHOT</tomee.version>
    <version.shrinkwrap.resolver>2.0.0</version.shrinkwrap.resolver>
    <javaassist.version>3.21.0-GA</javaassist.version>
</properties>

<repositories>
    <repository>
        <id>apache-m2-snapshot</id>
        <name>Apache Snapshot Repository</name>
        <url>https://repository.apache.org/content/groups/snapshots</url>
    </repository>
</repositories>


<build>
    <sourceDirectory>src</sourceDirectory>
    <!-- <finalName>gurusahakari</finalName> -->
    <defaultGoal>install</defaultGoal>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>copy-test-libs</id>
                    <phase>process-test-resources</phase>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax.servlet</groupId>
                                <artifactId>jstl</artifactId>
                                <version>1.2</version>
                            </artifactItem>
                            <artifactItem>
                                <groupId>taglibs</groupId>
                                <artifactId>standard</artifactId>
                                <version>1.1.2</version>
                            </artifactItem>
                        </artifactItems>
                        <outputDirectory>
                            ${project.build.directory}/test-libs
                        </outputDirectory>
                        <stripVersion>true</stripVersion>
                    </configuration>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
                <reuseForks>false</reuseForks> <!-- otherwise openejb embedded and tomee embedded shares the same context 
                    and EJBContainer is broken -->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <configuration>
                    <wtpapplicationxml>true</wtpapplicationxml>
                    <wtpversion>2.0</wtpversion>
                </configuration>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.aries.blueprint</groupId>
            <artifactId>blueprint-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
                <scanPaths>
                    <scanPath>org.opendaylight</scanPath>
                </scanPaths>
            </configuration>
            <dependencies>
                <dependency>
                    <!-- https://stackoverflow.com/questions/38825386/blueprint-maven-plugin-runtimeexception -->
                    <groupId>org.apache.xbean</groupId>
                    <artifactId>xbean-finder-shaded</artifactId>
                    <version>4.5</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <goals>
                        <goal>blueprint-generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.openejb.maven</groupId>
            <artifactId>tomee-maven-plugin</artifactId>
            <version>2.0.0-SNAPSHOT</version>
            <configuration>
                <tomeeClassifier>plus</tomeeClassifier>
                <args>-Xmx512m -XX:PermSize=256m</args>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.7</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.9.7</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>


    <!-- validation jar -->
    <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.0.2.GA</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.bval/bval-jsr303 -->
    <dependency>
        <groupId>org.apache.bval</groupId>
        <artifactId>bval-jsr303</artifactId>
        <version>0.5</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session -->
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
        <version>1.0.0.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
    <dependency>
        <groupId>net.sf.jasperreports</groupId>
        <artifactId>jasperreports</artifactId>
        <version>6.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.8.RELEASE</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.xml.messaging.saaj/saaj-impl -->
    <dependency>
        <groupId>com.sun.xml.messaging.saaj</groupId>
        <artifactId>saaj-impl</artifactId>
        <version>1.3.18</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.11</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- jackson data biending -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.0</version>
    </dependency>

    <!-- Apache Commons FileUpload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>

    <!-- Apache Commons IO -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>


    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>
</dependencies>

这是执行mvn dependency:tree -Dverbose -Dincludes=spring-core命令的结果。

[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.guru.gurusahakari:gurusahakari:war:0.0.1-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: junit:junit:jar -> duplicate declaration of version 3.8.1 @ line 284, column 15
[WARNING]  
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
 [WARNING] 
 [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
 [WARNING] 
  [INFO]                                                                         
   [INFO] ------------------------------------------------------------------------
   [INFO] Building gurusahakari 0.0.1-SNAPSHOT
   [INFO] ------------------------------------------------------------------------
    [INFO] 
   [INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ gurusahakari ---
     [INFO] ------------------------------------------------------------------------
   [INFO] BUILD SUCCESS
   [INFO] ------------------------------------------------------------------------
   [INFO] Total time: 5.987 s
   [INFO] Finished at: 2018-08-15T09:59:09+05:45
    [INFO] Final Memory: 11M/158M
    [INFO] ------------------------------------------------------------------------

没有机会。 - Sumesh TG
显然,是的,我也很困惑。可能是其他进程触发了下载。 - parlad
但这不会影响您的项目编译,对吧? - Sumesh TG
当然。而且,我对编译也不感兴趣。 - parlad
当你运行mvn dependency:tree时,所显示的使用版本是哪个版本? - Daniel Scott
显示剩余2条评论
6个回答

5
为了更有自信,我需要查看你的pom.xml文件。
但是,有一种官方的处理方式可以应对这种情况。只需在您的pom中添加如下标签:
<!-- Your actual dependencies -->
<dependencies>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
   </dependency>
</dependencies>

<!-- Constraints you put on your direct and transitive dependencies -->
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>5.0.8.RELEASE</version>
      </dependency>
   </dependencies>
</dependencyManagement>

这将覆盖依赖项,使用指定版本。

现在,Maven 中没有 JAR 文件。你知道是什么原因吗? - parlad
整个项目中都出现了错误。它不接受自动装配和控制器等注释。 - parlad
似乎内部存在依赖关系,无法导入 <dependencyManagement></dependencyManagement>。当进入 <dependencyManagement></dependencyManagement> 时会出现错误。 - parlad
如果您分享您的pom.xml文件,我可以尝试帮助您解决这个问题。 - Alessandro Oliveira
@parladneupane 在<dependencyManagement>中的依赖关系并不构成对该工件的实际依赖。这意味着您必须在a)您的<dependencies>列表和b)<dependencyManagement>中都包含该依赖项。 - Vlastimil Ovčáčík
显示剩余3条评论

2
我猜您需要了解为什么spring-core的3.0.6版本被拉取下来。
可能是因为有一些传递性依赖关系 - 请尝试使用以下命令查找:
mvn dependency:tree -Dverbose -Dincludes=spring-core 一旦您找到了它,您可以使用“Dependency Exclusions”来防止其被下载,请参考此链接:
Dependency Exclusions 例如:
<dependency>
    <groupId>org.xyz</groupId>
    <artifactId>xyz</artifactId>
    <version>${xyz.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

其中xyz是具有旧版Spring版本的传递依赖项的工件。


我应该在Spring Core 4.3.8中添加<optional>false</optional>吗? - parlad
不确定您是否需要它,请查看我在帖子中提供的示例 - 您是否发现了3.0.6上的传递依赖项? - user1373164
我实际上并没有看到任何特定的东西。我发布了mvn dependency:tree -Dverbose -Dincludes=spring-cor结果。 - parlad

0
Try like this
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
it should work fine                                                                     

请具体说明版本,最新版本通常可以解决大部分问题。 - BHARATHWAJ
他无法删除不存在的东西。 - Aniruddha Kalburgi

0
我在开发过程中遇到了这个错误多次。 我通过下载jar并添加外部文件来解决它,以下是详细步骤:
步骤:
1.在Google上搜索“*.jar的maven依赖项”或直接前往maven依赖项网站并搜索jar(用你需要的jar名称替换*)
2.从maven依赖项中下载jar 3.右键单击项目,然后单击属性
4.选择左侧的“Java Build Path”,然后选择“Libraries”选项卡。现在,点击“添加外部JARS...”按钮
5.找到并选择您下载的jar文件,然后单击“打开”
6.最后,单击“确定”以关闭对话框。如果您打开项目文件夹时看到名为“Referenced Libraries”的项目,并展开此项目时看到列出您的jar文件,则说明一切都进行得很好。

0

我曾经遇到过一些类似于Maven的奇怪情况,我解决了这个问题,删除了存储库文件夹(%user-folder%/.m2/repository),然后再次执行clean install。有时,具有错误或过时pom.xml文件的项目工件的先前版本可能会导致Maven依赖关系分辨率中出现不必要的行为。

如果您正在使用像IntelliJ IDEA这样的IDE(也许Eclipse也是如此),请记住它维护自己的工件缓存。因此,您还可以尝试从命令提示符中执行clean install以丢弃不必要的内容。


0

解决这类问题的简单方法是使用像Eclipse这样的图形化工具。(即)

enter image description here

  • 打开依赖管理的水龙头。

enter image description here

  • 过滤、分析和检查依赖关系不适用

Dependency Management

  • 移除您不需要的依赖关系。

enter image description here

这将为您节省大量的谷歌搜索和时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接