【问题标题】:Project in eclipse not resolving jars present in Maven dependencyEclipse 中的项目无法解析 Maven 依赖项中存在的 jars
【发布时间】:2021-12-25 10:07:25
【问题描述】:

在使用 eclipse 和 maven 时,我变得非常困惑。我正在尝试创建一个具有如下包结构的项目:

现在在DbSIngleton.java 文件中,我引用EmbeddedDriverorg.apache.derby.jdbc 包。但它无法解决这个问题,这很奇怪,因为 jar 存在于 maven 依赖项中,如图所示。还有另一个 jar twitter4j 被引用而没有任何错误。为什么这个德比罐失败了?我错过了什么吗?

代码:

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:

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

【问题讨论】:

    标签: eclipse maven pom.xml


    【解决方案1】:

    请移除测试范围

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

    应该是

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

    Twitter 依赖项在默认范围内。因此它正在得到解决。

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2020-09-09
      • 2022-01-23
      • 2015-08-11
      • 2012-01-26
      • 1970-01-01
      相关资源
      最近更新 更多