2017-09-02 66 views
0

我有两个模块,A和B,它们在同一个父项下。现在A需要B作为依赖。我不能只使用jar作为依赖类型,因为模块B使用spring-boot-maven-plugin,所以我想知道如何设置A的POM配置,使A取决于B的编译类而不是jar<Maven>如何在Maven项目中将类文件添加为依赖项(不是jar)?

- root 
    - A 
    - src   # module A's source code 
    - target 
     - classes # module A's compiled classes 
     - A.jar  # module A's compiled jar output 
    - pom.xml  # module A's mvn config 
    - B 
    - src   # module B's source code 
    - target 
     - classes # module B's compiled classes, HOW CAN I IMPORT THESE TO A? 
     - B.jar  # module B's mvn config 
    - pom.xml  # module B's mvn config 
    - pom.xml  # parent mvn config 

父MVN配置

... 
<modules> 
    <module>A</module> 
    <module>B</module> 
</modules> 
... 

模块A MVN配置

... 
<parent> 
    <!- pointed to parent -> 
</parent> 
<dependencies> 
    <dependency> 
    <groupId></groupId> 
    <artifactId>B</artifactId> 
    <scope>??????????????</scope> # WHAT SHOULD I PUT HERE? 
    <type>???????????????</type> # WHAT SHOULD I PUT HERE? 
    </dependency> 
<dependencies> 
... 
+0

你需要B的类在运行时? – nullpointer

回答

3

首先:当A依赖于某些其他模块的类,它必须依赖于罐子。你不能依赖于模块的某些部分或者仅仅依赖于类。

所以让我绘制一个解决方案:

  1. 如果(如果没有包装给定的标准,也是如此)B是的<packaging>jar</packaging>,那么你可以使用它作为依赖。您不需要范围或类型条目。如果B是某些其他包装(包括弹簧定义的包装类型,我不是专家),那么您应该定义从A到B的依赖关系。而不是定义第三个模块C ,其中包括从A和B中使用的类,并让它们都对C有依赖关系。

不要试图构造对非JAR的依赖关系。如果你需要类,用这些类定义一个模块,并在需要类时使用它。

+0

谢谢。你的第二个解决方案很有意义我测试过了! – Kim

-1

这里是关于行家依赖性范围和类型的良好信息。 https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

这应该适合你。 (未测试)

<scope>compile</scope> 
<type>jar</type> 
+0

从问题* make A取决于B的编译类而不是jar *。你的解决方案如何满足这个? – nullpointer

+0

这就是我提到编译范围的原因。如果我提供范围作为运行时,那么它将在jar上。 – nagendra547

+1

您可以测试您的代码并确认A是否在那里使用B的jar。 – nullpointer

相关问题