如何依赖这个maven项目

How to depend on this maven project

我的项目由三个子项目组成,我的父 pom 如下所示:

<groupId>com.bwort.core</groupId>
<artifactId>bwort</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>

<name>bwort</name>

<modules>
  <module>proj1</module>
  <module>proj2</module>
  <module>proj3</module>             
</modules>

现在我的项目需要依赖下面这个项目,它包括三个子项目,父pom.特别是,它已经有一个父级如下: https://github.com/cmusphinx/sphinx4/blob/master/pom.xml

  <parent>
    <groupId>org.sonatype.oss</groupId>
    <artifactId>oss-parent</artifactId>
    <version>7</version>
  </parent>

  <groupId>edu.cmu.sphinx</groupId>
  <artifactId>sphinx4-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

我的问题是,如何在父 pom 文件中声明依赖项?我可以将另一个模块添加到我的父 pom:

<module>sphinx4</module>

但是既然这个库已经定义了它自己的父类"oss-parent",那么我怎样才能让我的父类pom成为它的父类呢?

我的项目依赖这个项目的正确方式是什么?谢谢。

已编辑:我的 pom.xml

 <project >
  <modelVersion>4.0.0</modelVersion>

   <parent>
     <groupId>com.bwort.core</groupId>
     <artifactId>bwort</artifactId>
     <version>0.0.1-SNAPSHOT</version>
   </parent>

    <artifactId>wikipedia</artifactId>
    <packaging>jar</packaging>


  <repositories>
       <repository>
           <id>snapshots-repo</id>
           <url>https://oss.sonatype.org/content/repositories/snapshots</url>
           <releases><enabled>false</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
       </repository>
    </repositories>

  <dependencies>

    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-core</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-data</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

 </dependencies>   

</project>

不,您不会将引用其他人模块的模块添加到您的 pom。

当执行 mvn install 和/或 mvn deploy 时,它会将 pom.xml 文件中定义的工件复制到本地或远程存储库中。所以希望你要依赖的项目在maven central中可用。

我建议在你的父级中添加一个依赖管理部分 pom.xml:

<properties>
  <sphinx.version>1.0-SNAPSHOT</sphinx>
</properties>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>edu.cmu.sphinx</groupId>
      <artifactId>sphinx4-module1</artifactId>
      <version>${sphinx.version}</version>
    </dependency>
    <dependency>
      <groupId>edu.cmu.sphinx</groupId>
      <artifactId>sphinx4-module2</artifactId>
      <version>${sphinx.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

在您自己的模块之一的 pom.xml 中,将您需要的依赖项添加到依赖项部分: 请注意,该版本现在已在父级中定义。

  <dependencies>
    <dependency>
      <groupId>edu.cmu.sphinx</groupId>
      <artifactId>sphinx4-module2</artifactId>
    </dependency>
  </dependencies>

我建议不要使用其他人应用程序的 -SNAPSHOT 版本 - 它通常会导致构建失败,具体取决于创建快照的时间以及 Maven 检索快照的时间。

如果 sphinx 不在存储库中,您首先需要在本地执行 mvn install

我会推荐 Maven 教程:

他们也解释了很多:)

如果您的 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>my-app</name>
    <url>http://maven.apache.org</url>
    <repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>edu.cmu.sphinx</groupId>
            <artifactId>sphinx4-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>edu.cmu.sphinx</groupId>
            <artifactId>sphinx4-data</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>