简体   繁体   English

向Maven项目添加库

[英]Adding library to Maven project

I have created simple Maven Java project in Eclipse. 我已经在Eclipse中创建了简单的Maven Java项目。

在此处输入图片说明

In package src/main/java I have create simple tst class and planing to use log4j there: 在包src/main/java我创建了一个简单的tst类,并打算在那里使用log4j

import java.util.Scanner;
import org.apache.log4j.Logger;

public class tst {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("hello");
        Scanner scan=new Scanner(System.in);
    }
}

I have added log4j in dependencies: 我在依赖项中添加了log4j:

在此处输入图片说明

I expect Maven will download log4j jar for my project. 我希望Maven将为我的项目下载log4j jar。 Instead of that I have error Missing artifact log4j:log4j:jar:1.2 . 而不是我有错误Missing artifact log4j:log4j:jar:1.2

How to fix this problem? 如何解决这个问题? Is it possible to know what versions are available in log4j project? 是否可能知道log4j项目中可用的版本?

mvnrepository.com says that the project Apache Log4j was move to Log4j Core . mvnrepository.com说,项目Apache Log4j已移至Log4j Core So try to update the artifact with this one: 因此,尝试使用此工具更新工件:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

When you add a library with Maven, always check the correct version in mvnrepository.com 使用Maven添加库时,请始终在mvnrepository.com中检查正确的版本。

And if you still have the error, try to update maven dependencies. 并且如果仍然有错误,请尝试更新Maven依赖项。 Right click on the project and select 右键单击该项目,然后选择

Maven -> Update Project

Sometimes when you update project libraries with Maven, Eclipse isn't able to download it directly and you have to force the refresh of the dependencies. 有时,当您使用Maven更新项目库时,Eclipse无法直接下载它,因此您必须强制刷新依赖项。

The problem is simply that there is no log4j in version 1.2. 问题很简单,因为在1.2版中没有log4j。

Try version 1.2.17. 试用1.2.17版。

Just follow the path you chose, but use 1.2.17 instead of 1.2. 只需遵循您选择的路径,但使用1.2.17而不是1.2。

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

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