【问题标题】:maven version dependency injection to sub modulesmaven 版本依赖注入到子模块
【发布时间】:2012-08-02 08:47:39
【问题描述】:

我购买了“Apache Maven 3 Cookbook”,并且正在尝试学习如何将依赖项添加到我的 maven 项目中。

我的主要项目 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>net.srirangan.packt.maven</groupId>
<artifactId>TestModularApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>TestModularApp</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<modules>
<module>ChildProject</module>
<module>MyWebApp</module>
</modules>
</project>

正如您在此处看到的,此项目中的打包设置为“pom”,因为这是父项目..(这就是我所知道的 :))

然后我用以下 pom.xml 创建了一个子项目:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>net.srirangan.packt.maven</groupId>
    <artifactId>TestModularApp</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>com.xpogames.childproject</groupId>
  <artifactId>ChildProject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>ChildProject</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>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
  </dependencies>
</project>

正如您在此处看到的,我没有向 mysql-connect-java 依赖项添加版本属性,因为它应该从父项目继承版本。但由于某种原因,它没有。

当我在该项目上运行 mvn compile 时,我收到一个错误,即缺少 dependencies.dependency.version 属性。

任何想法我做错了什么?如何解决我不需要在子项目中指定版本的问题?

谢谢!

更新

在观看了 Peter Lawrey 的回答并观看了他提供的网址上的示例之后 我注意到我的主要 XML 缺少 &lt;dependencies&gt; 周围的属性 &lt;dependencyManagement&gt;。一旦我添加了该属性,我就不需要在子项目中提供版本号了。

彼得的回答显示了实现这一目标的另一种方法。

感谢一切!

冷杉

【问题讨论】:

    标签: java maven-3 dependency-management


    【解决方案1】:

    您从父 pom 的 dependencyManagement 部分中的依赖项继承版本,并有选择地将这些版本包含在子 pom 中。

    在您的情况下,您不需要再次提及依赖项,因为它已经包含在父项中。 (对于 JUnit 来说)

    顺便说一句:我使用的 JUnit 4.10 没有发现任何向后兼容性问题。

    【讨论】:

    • 非常感谢。通过查看您提供的 url,我看到通过排除我可以排除我不想使用该依赖项的项目。问题已解决:)
    猜你喜欢
    • 2013-08-01
    • 2014-09-09
    • 2019-07-23
    • 2016-09-18
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    相关资源
    最近更新 更多