eclipse - Eclipse 和 Maven 多模块项目的依赖性问题

标签 eclipse maven m2eclipse maven-3 m2e

我有一个多模块 Maven 项目,由三个子模块组成:webservicedomain。我使用 m2e 并成功将 Maven 项目导入到 Eclipse 中。

此外,服务项目位于web的java构建路径和中> 项目位于服务的java构建路径中。

这很奇怪,因为显示编译错误,这似乎表明例如服务看不到。但是,当我在 Eclipse 中单击错误的类时,我可以导航到 domain 类。

此外,我可以将 web 项目添加到 Tomcat 服务器实例,但 web 项目不包含其他两个项目。

有人可以帮忙吗?

web pom.xml:

<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>
    <parent>
        <groupId>com.bignibou</groupId>
        <artifactId>bignibou</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.bignibou</groupId>
    <artifactId>bignibou-web</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>bignibou-web</name>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.tml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <!--
                <version>2.0.2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${basedir}/WebContent</directory>
                        </resource>
                    </webResources>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <warSourceExcludes>WebContent/WEB-INF/lib/*.jar</warSourceExcludes>
                    <archiveClasses>false</archiveClasses>
                </configuration>
                -->
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.bignibou</groupId>
            <artifactId>bignibou-domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.bignibou</groupId>
            <artifactId>bignibou-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tapestry</groupId>
            <artifactId>tapestry-core</artifactId>
            <version>5.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tapestry</groupId>
            <artifactId>tapestry-beanvalidator</artifactId>
            <version>5.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tapestry</groupId>
            <artifactId>tapestry-spring</artifactId>
            <version>5.2.6</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tapestry</groupId>
            <artifactId>tapestry-test</artifactId>
            <version>5.2.6</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>testng</artifactId>
                    <groupId>org.testng</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.0.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>2.5.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- <dependency> <groupId>com.formos.tapestry</groupId> <artifactId>tapestry-testify</artifactId> 
            <version>1.0.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.formos.tapestry</groupId> 
            <artifactId>tapestry-xpath</artifactId> <version>1.0.1</version> <scope>test</scope> 
            </dependency> -->
    </dependencies>
</project>

服务pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.bignibou</groupId>
        <artifactId>bignibou</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.bignibou</groupId>
    <artifactId>bignibou-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>bignibou-service</name>

    <!-- Shared version number properties -->
    <properties>
        <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <!--
            Core utilities used by other modules.
            Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Expression Language (depends on spring-core)
            Define this if you use Spring Expression APIs (org.springframework.expression.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Bean Factory and JavaBeans utilities (depends on spring-core)
            Define this if you use Spring Bean APIs (org.springframework.beans.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans)
            Define this if you use Spring AOP APIs (org.springframework.aop.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans)
            This is the central artifact for Spring's Dependency Injection Container and is generally always defined
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration
            Define this if you need any of these integrations
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context)
            Define this if you use Spring Transactions or DAO Exception Hierarchy
            (org.springframework.transaction.*/org.springframework.dao.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx)
            Define this if you use Spring's JdbcTemplate API (org.springframework.jdbc.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis.
            (depends on spring-core, spring-beans, spring-context, spring-tx)
            Define this if you need ORM (org.springframework.orm.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans.
            (depends on spring-core, spring-beans, spring-context)
            Define this if you need OXM (org.springframework.oxm.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Web application development utilities applicable to both Servlet and Portlet Environments
            (depends on spring-core, spring-beans, spring-context)
            Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Support for testing Spring applications with tools such as JUnit and TestNG
            This artifact is generally always defined with a 'test' scope for the integration testing framework and unit testing stubs
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>com.bignibou</groupId>
            <artifactId>bignibou-domain</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

域名pom.xlm

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.bignibou</groupId>
        <artifactId>bignibou</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>com.bignibou</groupId>
    <artifactId>bignibou-domain</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>bignibou-domain</name>

    <dependencies>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.2.0.Final</version>
        </dependency>
         <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>1.7.1</version>
        </dependency>
        <!--
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.3-Final</version>
        </dependency>
        -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>
    <repositories>
        <repository>
            <id>eclipselink</id>
            <url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo/</url>
        </repository>
    </repositories>
</project>              

super pom.xlm:

<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.bignibou</groupId>
    <artifactId>bignibou</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>bignibou super pom</name>
    <modules>
        <module>domain</module>
        <module>service</module>
        <module>web</module>
    </modules>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

最佳答案

在 super pom.xml 的依赖管理标签内定义域和服务模块。所以基本上在 super pom 中添加以下定义:

     <dependencyManagement>
        <dependencies>
           <dependency>
              <groupId>com.bignibou</groupId>
              <artifactId>bignibou-domain</artifactId>
              <version>1.0-SNAPSHOT</version>
           </dependency>
           <dependency>
              <groupId>com.bignibou</groupId>
              <artifactId>bignibou-service</artifactId>
              <version>1.0-SNAPSHOT</version>
           </dependency>
        <dependencies>
     </dependencyManagement>

将这些定义添加到 super pom 后,您还将有机会从模块 pom 中删除这些依赖项的版本号。

关于eclipse - Eclipse 和 Maven 多模块项目的依赖性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8234708/

相关文章:

java - Vaadin Spring PLupload-Addon 上传文件失败

maven - 如何强制 Sonatype Nexus 更新?

eclipse - Eclipse中使用Hadoop MapReduce报错

java - 使用MyEclipse GUI将图片放入java程序中

java - eclipse helios for maven项目中Sonar插件安装问题

java - java -cp 与 java -jar 之间的区别

android - Eclipse 在 BIN 文件夹中安装 .APK?不会出现?

java - 以编程方式配置的嵌入式 Tomcat 8 应用程序

java - Eclipse:如何在应用程序库中搜索字符串?

java - 从 eclipse 运行时,Maven 不会在项目目录中查找 web.xml