使用Eclipse进行Maven多模块构建

3
我将转换为Maven多模块构建。
我的项目结构如下:
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是一个由M2E构建的Maven Eclipse项目。

我将MyProject-Core/src/main/java添加为Eclipse源文件夹,但自从我将parent pom中的dependency部分改为dependencyManagement后,我就从Eclipse中得到了构建错误。手动调用mvn package仍然可以正常工作。

我该如何配置Eclipse以正确解决core pom的依赖关系?

我仍然可以将依赖项添加到parent pom中并将其范围设置为provided,但这是最好的解决方案吗?

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

请将您的pom文件(根目录)和核心模块的pom文件发布出来,以供参考。 - khmarbaise
好的,我添加了简化版本的POM。 - Stephan
你尝试过移除并重新导入该项目吗? - cahen
这并不能解决问题。 - Stephan
1个回答

0

我尝试了很多配置,但M2E无法将子模块识别为自己的项目,或者对子模块的更改未被父模块项目识别。

现在我创建了几个Eclipse项目,每个Maven项目一个,它们可以正常工作。


1
我不理解这个答案。当您导入多项目构建时,您已经获得了多个Eclipse项目,每个模块一个。 - Robin Green
2
是的,没错。但我尝试将多个Maven项目作为一个Eclipse项目使用,但这并不起作用。我必须为每个Maven项目创建一个Eclipse项目,这样就可以正常工作了。 - Stephan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接