How to depend on this maven project

偶尔善良 提交于 2020-01-17 09:48:08

问题


My projects consists of three sub-projects, and my parent pom looks like:

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

Now my project needs to dependent this project below, which comprises three subprojects, with a parent pom. In particular, it already has a parent as below: 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>

My question is, how can I declare the dependency in my parent pom file? I can add another module to my parent pom:

<module>sphinx4</module>

But since this library already defined its own parent "oss-parent", then how can I make my parent pom as its parent?

What's the right way for my project to depend on this project? Thank you.

EDITTED: My 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>

回答1:


If your pom.xml has something like the following, it should work:

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



回答2:


No you don't add modules to your pom which refer to other peoples modules.

When executing mvn install and or mvn deploy it will copy the artifacts defined in the pom.xml files into the local or a remote repository. So hopefully the project you want to depend on is available in maven central.

what I would suggest is to add a dependency management section into your parent 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>

in the pom.xml of one of your own modules add that dependency you need into the dependencies section: Note the version is now defined in the parent.

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

I would recommend to not use -SNAPSHOT versions of other people applications - it often causes build failures depending on when the snapshot was created and when maven retrieves it.

If sphinx is not in a repository you first need to do a mvn install locally

And I would recommend the maven tutorials:

  • https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
  • https://maven.apache.org/guides/getting-started/index.html
  • http://books.sonatype.com/mvnref-book/reference/

they explain quite a lot as well :)



来源:https://stackoverflow.com/questions/32218858/how-to-depend-on-this-maven-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!