我购买了“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 的回答并观看了他提供的 url 上的示例后,我注意到我的主要 XML<dependencyManagement>缺少<dependencies>. 一旦我添加了该属性,我就不需要在子项目中提供版本号。
彼得的回答显示了实现这一目标的另一种方法。
感谢一切!
克菲尔