使用Maven管理的第三方依赖关系的传递性。

6

我正在开发一款使用Cassandra NoSQL数据库的应用程序,并添加了Web界面。我有两个项目:cassandra-access(该项目是数据访问层)和web(该项目是Web应用程序)。

场景很简单。 Cassandra-access依赖于hector.jar,但maven仓库中没有这个依赖项。因此,我通过mvn install:install-file将此依赖项添加到本地仓库,并在父POM中列出我的仓库:

 <repositories>
      <repository>
        <id>loc</id>
        <url>file://${basedir}/../mvn-local-repository</url>
    </repository>
</repositories>

在Web项目的pom文件中,我添加了对Cassandra-access的依赖。但是当我从数据库读取hello world并启动Web应用程序时,我遇到了classNotFound异常,就好像hector.jar不在类路径上一样。当我编写mvn clean install时,Web项目的生成war包不会将hector.jar包含在WEB-INF/lib中。这进一步证实了我的理论。
如何实现war包获取所有传递依赖项?我认为所有作用域为compile(默认值)的依赖项都会被复制。
Web项目的pom文件:
  <dependency>
      <groupId>net.product</groupId>
      <artifactId>cassandra-access</artifactId>
      <version>1.0-SNAPSHOT</version>
  </dependency>

Cassandra-access pom:

 <dependency>
   <groupId>me.prettyprint</groupId>
   <artifactId>hector</artifactId>
   <version>0.7.0</version>
 </dependency>

我发现上述文本同样有效。我在POM中只有警告,这导致maven根本不包括传递依赖项。调试此类情况的最佳方法是使用mvn dependency:tree -X。 - Filip Nguyen
1个回答

1

这可能不是最优解决方案,但对我来说有效: 将Hector jar放入Cassandra访问的lib目录中。 在Cassandra-access pom中添加:

<dependency>    
    <groupId>%HECTOR_JAR_GROUP_ID%</groupId>  
    <artifactId>%HECTOR_JAR_ARTIFACT_ID%</artifactId>  
    <version>%HECTOR_JAR_VERSION%</version>  
    <scope>system</scope>  
        <systemPath>${basedir}/lib/%HECTOR_JAR_NAME%</systemPath>  
</dependency>  

然后添加以下插件:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-dependency-plugin</artifactId>  
    <executions>  
        <execution>  
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接