2013-04-24 128 views
3

我正在轉向maven多模塊構建。Maven使用eclipse構建多模塊

我的項目結構如下所示:

MyProject 
|-->MyProject-Core 
| |->pom.xml (packaging jar) 
|-->MyProject-Assets 
| |->pom.xml (packaging jar) 
|-->MyProject-Libs 
| |->pom.xml (packaging jar) 
|-->pom.xml (packaging pom, aggregator) 

MyProject是Maven的Eclipse項目和M2E建成。

我加入了MyProject的核心/ src目錄/主/ Java作爲Eclipse的源文件夾,但因爲我改變了依賴部分在父POM以dependencyManagement,我從Eclipse構建錯誤。手動調用mvn package仍然正常工作。

如何配置Eclipse來正確解析核心POM的依賴?

我仍然可以添加依賴關係父POM的範圍:提供的,但是這是最好的解決辦法?

家長-POM:

<?xml version="1.0" encoding="UTF-8"?> 
<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.example</groupId> 
    <artifactId>MyProject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>pom</packaging> 
    <dependencyManagement> 
    <dependencies> 
     <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
     <version>1.2.16</version> 
     </dependency> 
    </dependencies> 
    </dependencyManagement> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <build> 
    <pluginManagement> 
     <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>findbugs-maven-plugin</artifactId> 
      <version>2.3.3</version> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.9</version> 
     </plugin> 
     <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence 
      on the Maven build itself. --> 
     <plugin> 
      <groupId>org.eclipse.m2e</groupId> 
      <artifactId>lifecycle-mapping</artifactId> 
      <version>1.0.0</version> 
      <configuration> 
      <lifecycleMappingMetadata> 
       <pluginExecutions> 
       <pluginExecution> 
        <pluginExecutionFilter> 
        <groupId> 
         org.codehaus.mojo 
        </groupId> 
        <artifactId> 
         exec-maven-plugin 
        </artifactId> 
        <versionRange> 
         [1.2,) 
        </versionRange> 
        <goals> 
         <goal>java</goal> 
        </goals> 
        </pluginExecutionFilter> 
        <action> 
        <ignore /> 
        </action> 
       </pluginExecution> 
       </pluginExecutions> 
      </lifecycleMappingMetadata> 
      </configuration> 
     </plugin> 
     </plugins> 
    </pluginManagement> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>2.3.2</version> 
     <configuration> 
      <source>1.6</source> 
      <target>1.6</target> 
      <encoding>UTF-8</encoding> 
     </configuration> 
     </plugin> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-jar-plugin</artifactId> 
     <version>2.3.1</version> 
     <configuration> 
      <archive> 
      <index>true</index> 
      <addMavenDescriptor>false</addMavenDescriptor> 
      <manifest> 
       <packageName>com.example</packageName> 
       <addDefaultImplementationEntries>true 
       </addDefaultImplementationEntries> 
       <addDefaultSpecificationEntries>true 
       </addDefaultSpecificationEntries> 
      </manifest> 
      <manifestEntries> 
       <SVN-Revision>${workingCopyDirectory.revision}</SVN-Revision> 
      </manifestEntries> 
      <compress>false</compress> 
      </archive> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
    <modules> 
    <module>MyProject-core</module> 
    </modules> 
</project> 

核心-POM:

<?xml version="1.0"?> 
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
    <groupId>com.example</groupId> 
    <artifactId>MyProject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    </parent> 
    <groupId>com.example</groupId> 
    <artifactId>MyProject-Core</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <dependencies> 
    <dependency> 
     <groupId>log4j</groupId> 
     <artifactId>log4j</artifactId> 
    </dependency> 
    </dependencies> 

    <build> 
    <sourceDirectory>src/main/java</sourceDirectory> 
    <testSourceDirectory>src/test/java</testSourceDirectory> 
    </build> 
</project> 
+0

請發表您的pom爲pom(根)和核心爲例。 – khmarbaise 2013-04-24 07:35:18

+0

好的,我添加了我的poms的簡化版本。 – Stephan 2013-04-24 08:08:41

+0

您是否嘗試刪除並重新導入項目? – cahen 2013-04-24 18:56:00

回答

0

我嘗試了很多配置,但M2E不認子模塊作爲子模塊自己的項目或變化是沒有被父模塊項目識別。

現在我創建了幾個Eclipse項目,每個Maven項目都有一個,它工作正常。

+1

我不明白這個答案。當您導入多項目構建時,您已經獲得了幾個Eclipse項目,每個模塊一個。 – 2014-07-09 15:41:01

+2

是的,這是正確的,但我試圖使用多個Maven項目作爲一個Eclipse項目,但這是行不通的。我必須爲每個Maven項目創建一個Eclipse項目,並且工作正常。 – Stephan 2014-07-21 08:49:05