2011-05-04 67 views
2

隨着maven2eclipse插件的問題是零文檔。它給我一個選擇,當我右鍵點擊Eclipse中「啓用依賴管理」這個項目我嘗試過,但給了我一個相當稀疏的pom.xml除了以下控制檯輸出:Maven 2 Eclipse

04/05/11 14:57:54 BST: Generating sources /BenCode/pom.xml 
04/05/11 14:57:54 BST: Build error for /BenCode/pom.xml; org.apache.maven.plugin.PluginResolutionException: Plugin org.apache.maven.plugins:maven-resources-plugin:2.4.3 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.4.3 
04/05/11 14:57:54 BST: Failed to determine compiler source setting, assuming default 
04/05/11 14:57:54 BST: Failed to determine compiler target setting, assuming default 
04/05/11 14:57:54 BST: Failed to determine compiler source setting, assuming default 
04/05/11 14:57:54 BST: Failed to determine compiler target setting, assuming default 
04/05/11 14:57:54 BST: Failed to determine compiler test inclusions, assuming defaults 
04/05/11 14:57:54 BST: Failed to determine compiler test exclusions, assuming defaults 
04/05/11 14:57:54 BST: Failed to determine compiler inclusions, assuming defaults 
04/05/11 14:57:54 BST: Failed to determine compiler exclusions, assuming defaults 
04/05/11 14:57:54 BST: Refreshing [/BenCode/pom.xml] 

具有不使用Maven的前試圖弄清楚做什麼是一個難題。我的印象是,maven應該使用該信息掃描類路徑依賴關係來管理項目,但沒有這樣的運氣:(

回答

5

Maven將只管理POM中指定的依賴關係(以及任何傳遞依賴關係(即依賴關係)直接定義) 如下面將範圍添加的JUnit的依賴測試

<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- ... --> 
</dependencies> 

,如果你對上pom.xml中單擊您可以選擇的Maven - >添加依賴 - 這將搜索默認的存儲庫。

也讀Introduction to the Dependency Mechanism

0

首先,我應該說,雖然我使用m2eclipse插件的Eclipse,我只使用「啓用依賴管理」功能;我在pom.xml文本編輯器中手工編寫的所有其他東西。這只是個人喜好,而不是一個規則。

關於maven的一點需要記住的是,它基於「約定優於配置」的基礎上工作 - 這基本上意味着除非你明確地告訴它,否則有很多假設。

第一個假設是,你的目錄結構與以下內容:

Project Dir 
    | 
    |- src 
    | |- main 
    | | |- java 
    | | |- resources 
    | | 
    | |- test 
    | | |- java 
    | | |- resources 
    | | 
    | |- site 
    | 
    |- pom.xml 

(更多關於目錄結構的詳細信息可以在Introduction to the Standard Directory Layout找到)

如果您的項目不符合這個結構,那麼你需要讓Maven知道,例如:

<build> 
    <directory>target</directory> 
    <outputDirectory>target/classes</outputDirectory> 
    <testOutputDirectory>target/test-classes</testOutputDirectory> 
    <sourceDirectory>src/main/java</sourceDirectory> 
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory> 
    <testSourceDirectory>src/test/java</testSourceDirectory> 
    <resources> 
     <resource> 
     <directory>src/main/resources</directory> 
     </resource> 
    </resources> 
    <testResources> 
     <testResource> 
     <directory>src/test/resources</directory> 
     </testResource> 
    </testResources> 
    </build> 

下一部分要擔心的是依賴關係......這就是Maven非常強大的地方,但是隻有當你真正知道你的依賴關係在哪裏的時候,對於剛開始使用Maven的人來說,這可能會讓人感到困惑(它讓我感到沮喪!)。這裏有一對夫婦加依賴(JUnit的 - 只是在測試階段使用 - 和log4j的)

<dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8.2</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.16</version> 
    </dependency> 
    </dependencies> 

的Maven應該足夠聰明,以確定指定的依賴等的依賴,所以你應該只需要引用您直接使用的依賴關係。

如果在Maven使用的存儲庫中找不到所需的依賴關係,則需要添加其他存儲庫 - 在這種情況下,Google是您的朋友。

這可能應該解決您的初始問題。

根據開發團隊的規模,你可以可能找到值得設置本地資源庫管理器並讓Maven使用本地資源回收作爲所有依賴項的來源,並且它會下載任何不存儲在內的任何資源,但這是另一種蠕蟲,因爲當你更熟悉Maven本身時。