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是一个 Maven Eclipse 项目,由 M2E 构建。

我将 MyProject-Core/src/main/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>
4

1 回答 1

0

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

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

于 2013-05-02T11:43:20.047 回答