简体   繁体   English

Eclipse 中的项目无法解析 Maven 依赖项中存在的 jars

[英]Project in eclipse not resolving jars present in Maven dependency

I become pretty much confused while working with eclipse and maven.在使用 eclipse 和 maven 时,我变得非常困惑。 I am trying to create a project which has the package structure as below:我正在尝试创建一个具有如下包结构的项目:

在此处输入图片说明

Now in DbSIngleton.java file I am referencing EmbeddedDriver class org.apache.derby.jdbc package.现在在DbSIngleton.java文件中,我引用了EmbeddedDriverorg.apache.derby.jdbc包。 But it is unable to resolve that, which is strange as the jar is present in the maven dependencies as shown in the image.但是它无法解决这个问题,这很奇怪,因为 jar 存在于 maven 依赖项中,如图所示。 Also there is another jar twitter4j which is getting referenced without any error.还有另一个 jar twitter4j ,它被引用而没有任何错误。 Why is it failing for this derby jar?为什么这个德比罐子失败了? AM I missing something?我错过了什么吗?

The code:编码:

private DbSingleton() {
    
    try {
        DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
    } catch (SQLException e) {
        e.printStackTrace();
    }
    
    if(conn != null) {
        throw new RuntimeException("Use getConnection() method to create");
    }
    
    if(instance != null) {
        throw new RuntimeException("Use getInstance() method to create");
    }
}  

pom.xml: 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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
   <groupId>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
 
   <name>demo</name>
   <url>http://maven.apache.org</url>
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
 
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
       <scope>test</scope>
     </dependency>
      <dependency>
            <groupId>org.twitter4j</groupId>
            <artifactId>twitter4j-core</artifactId>
            <version>[4.0,)</version>
      </dependency>
 <!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
 <dependency>
     <groupId>org.apache.derby</groupId>
     <artifactId>derby</artifactId>
     <version>10.11.1.1</version>
     <scope>test</scope>
 </dependency>
 
 
   </dependencies>
 </project>

Please remove the test scope请删除测试范围

<dependency>
     <groupId>org.apache.derby</groupId>
     <artifactId>derby</artifactId>
     <version>10.11.1.1</version>
     <scope>test</scope>
 </dependency>

should be应该

<dependency>
     <groupId>org.apache.derby</groupId>
     <artifactId>derby</artifactId>
     <version>10.11.1.1</version>
 </dependency>

Twitter dependency is in the default scope. Twitter 依赖项在默认范围内。 Hence it is getting resolved.因此它正在得到解决。

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

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