简体   繁体   中英

How do I use a remote maven repository

I am new to Maven. I am trying to use springframework in my maven project. 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

    <?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

    [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 . 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. 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. You can find more about spring-boot here . Also, when you use spring-boot usually you don't need to deal with the versions of the dependencies manually.

A quote from the link:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

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.

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

Remove spring-beans on group id

remove .spring-beans in the groupId.

just change your <groupId>org.springframework.spring-beans</groupId> by <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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