簡體   English   中英

Maven:如何在兩個不同的模塊下有一個同名的模塊?

[英]Maven: How can I have a module with same name under two different modules?

考慮這種結構

project
       pom.xml
       component1/
                 pom.xml
                 persistence/pom.xml
                 business/pom.xml
                 rest/pom.xml
       component2/
                 pom.xml
                 persistence/pom.xml
                 business/pom.xml
                 rest/pom.xml

當我嘗試執行此操作時,我收到NullPointerException

[ERROR] Internal error: java.lang.NullPointerException -> [Help 1]
org.apache.maven.InternalErrorException: Internal error: java.lang.NullPointerException
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:167)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.NullPointerException
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:270)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    ... 11 more
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/InternalErrorException

我可以沒有這樣的結構嗎? 我正在使用Maven 3.2.1

這就是component2/pom.xml外觀

<parent>
    <groupId>com.org</groupId>
    <artifactId>component2</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>persistence</artifactId>
<version>1.0-SNAPSHOT</version>

這與component1/pom.xml相同, 除了父級

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.org</groupId>
    <artifactId>component1</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>persistence</artifactId>
<packaging>pom</packaging>

當我嘗試使用其他模塊名稱時,它可以工作

作為@ jigar-喬希已經指示,行家需要的方式來區分persistence的模塊component1persistence的模塊component2 它的方法是通過groupIdartifactId的組合。 對於每個persistence模塊(或與此相關的businessrest ,此組合都必須是唯一的。

因此,每個persistence需要具有唯一的groupId (例如com.org.component1com.org.component2 )或唯一的artifactIdcomponent1-persistencecomponent2-persistence )。

不僅Maven獲取會與項目的實際分層相混淆,而且總有一天您會迷失於兩個模塊,並且您將不知道這是哪個模塊。

因此,正如@Raghuram指出的那樣,每個模塊都需要一個唯一的標識符,因此,需要具有唯一的groupId或唯一的artifcatId (在您的項目范圍內,甚至尊重其他工件,否則您將產生奇怪的沖突)。

這是出於唯一性目的,但應遵循一些約定,您將自動擺脫由模塊/項目命名引起的任何麻煩:

  • goupId :項目的通用唯一標識符 通常,使用完全合格的程序包名稱將其與具有類似名稱的其他項目區分開(例如org.apache.maven)。

  • artifactId :此工件的標識符, 在由組ID給出的組是唯一的

只需遵循這些原則,您甚至將不安全,但會產生遵守練習規則的軟件包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM