简体   繁体   English

我如何使用远程Maven存储库

[英]How do I use a remote maven repository

I am new to Maven. 我是Maven的新手。 I am trying to use springframework in my maven project. 我正在尝试在我的Maven项目中使用springframework。 I see the spring version is too old here https://repo.maven.apache.org/maven2 , so I tried to include another repository http://repo.spring.io/release/ in my pom.xml 我在这里看到春季版本太旧了https://repo.maven.apache.org/maven2 ,所以我尝试在我的pom.xml中包含另一个存储库http://repo.spring.io/release/

    <?xml version="1.0" encoding="UTF-8"?>
<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>abc</groupId>
    <artifactId>xyz</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <repositories>
        <repository>
            <id>spring</id>
            <name>spring</name>
            <url>http://repo.spring.io/release/</url>
        </repository>
        <repository>
            <id>maven-apache</id>
            <name>maven-apache</name>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.spring-beans</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
    </dependencies> 

</project>

But when I run mvn compile , I still get following error 但是当我运行mvn compile ,仍然出现以下错误

    [WARNING] The POM for org.springframework.spring-beans:spring-beans:jar:5.0.0.RELEASE is missing, no dependency information available
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.324 s
    [INFO] Finished at: 2018-07-24T23:06:42-07:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal on project XXXX: Could not resolve dependencies for project XXXX:jar:1.0: Failure to find org.springframework.spring-beans:spring-beans:jar:5.0.0.RELEASE in http://repo.spring.io/release/ was cached in the local repository, resolution will not be reattempted until the update interval of spring has elapsed or updates are forced -> [Help 1]
    [ERROR]

Any ideas for getting this work? 任何想法得到这项工作?

Usually you should check for all the dependencies you need in the Maven Central Repository . 通常,您应该在Maven 中央存储库中检查所需的所有依赖项。 You will be able to find most of the "free" dependencies there. 您将可以在那里找到大多数“免费”依赖项。 There are only a few scenarios where you won't be able to find what you need there. 在少数情况下,您将无法在其中找到所需的内容。

After you identify what dependency you need and the version you need, you will see, on the left side, something like this: 确定所需的依赖关系和所需的版本之后,您将在左侧看到类似以下内容的信息:

Apache Maven依赖关系

The information from there can be easily copy pasted in your project. 来自那里的信息可以很容易地复制粘贴到您的项目中。 Also it is highly recommended to use the same version for all the dependencies that are related and released together by defining a variable. 另外,强烈建议对所有相关的依赖项使用相同的版本,并通过定义变量将它们一起释放。

You define a variable like this: 您可以这样定义一个变量:

<properties>
    <spring.version>5.0.7.RELEASE</spring.version> --> the version from maven central
</properties>

And then you can include the dependency like this: 然后,您可以包括这样的依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>

And if you have multiple pom.xml files, and there is a father -> child relationship, you don't need to include the dependency version in the child file, because it will be automatically inherited from the father's version. 而且,如果您有多个pom.xml文件,并且有一个父亲->子关系,则无需在子文件中包含依赖项版本,因为它将自动从父亲的版本继承而来。 You will be able to do something like this: 您将可以执行以下操作:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
</dependency>

For Spring in special, if you need more than one dependency, I would recommend you to take a look over spring-boot. 对于特殊的Spring,如果您需要多个依赖关系,我建议您看一下spring-boot。 You can find more about spring-boot here . 您可以在此处找到有关spring-boot的更多信息。 Also, when you use spring-boot usually you don't need to deal with the versions of the dependencies manually. 同样,当您使用spring-boot时,通常不需要手动处理依赖项的版本。

A quote from the link: 引用链接:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". 使用Spring Boot可以轻松地创建独立的,基于生产级别的基于Spring的应用程序,您可以“运行”它们。

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. 我们对Spring平台和第三方库持固执己见的观点,因此您可以以最小的麻烦开始使用。 Most Spring Boot applications need very little Spring configuration. 大多数Spring Boot应用程序只需要很少的Spring配置。

As a conclusion, you don't need to add another repository and the groupId of your dependency should look like this: <groupId>org.springframework</groupId> , and everything will work. 结论是,您不需要添加其他存储库,并且依赖项的groupId应该如下所示: <groupId>org.springframework</groupId> ,一切都会正常进行。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.0.0.RELEASE</version>
</dependency>

Remove spring-beans on group id 删除组ID上的四季豆

remove .spring-beans in the groupId. 删除groupId中的.spring-beans

just change your <groupId>org.springframework.spring-beans</groupId> by <groupId>org.springframework</groupId> 只需通过<groupId>org.springframework</groupId>更改您的<groupId>org.springframework.spring-beans</groupId> <groupId>org.springframework</groupId>

You should try to add a dependency like this: 您应该尝试添加以下依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM