Question

I am moving to maven multi-module build.

My project structure looks as follows:

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

The MyProject is an Maven Eclipse project and built by M2E.

I added the MyProject-Core/src/main/java as Eclipse source folder but since I changed the dependency section in the parent pom to dependencyManagement, I got build errors from Eclipse. Manually invoking mvn package still works fine.

How can I configure Eclipse to resolve the dependencies of the core pom correctly?

I could still add the dependencies to the parent pom as scope:provided, but is this the best solution?

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>

Core-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>
Was it helpful?

Solution

I tried a lot of configurations but M2E does not recognize sub-modules as own projects or changes in sub-modules were not recognized by parent module projects.

Now I created several Eclipse projects, one for each Maven project and it works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top