对于多模块maven项目,Maven依赖关系不会进入eclipse

时间:2017-09-04 12:14:16

标签: java eclipse maven pom.xml

我有一个多模块maven项目。请在下面找到父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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rohit.patterns</groupId>
    <artifactId>design-patterns</artifactId>
    <version>1.0.1</version>
    <packaging>pom</packaging>

    <name>React.js Blank Project (from https://github.com/making/maven-reactjs-blank)</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <slf4j.version>1.7.21</slf4j.version>
        <logback.version>1.1.7</logback.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <id>gulp</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <echo message="Gulp!" />
                                <exec executable="gulp">
                                    <arg value="build" />
                                </exec>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/META-INF/resources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>dest</directory>
                                    <filtering>false</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <modules>
        <module>cache</module>
    </modules>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback.version}</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>${logback.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</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.rohit.patterns</groupId>
        <artifactId>design-patterns</artifactId>
        <version>1.0.1</version>
    </parent>
    <groupId>com.rohit.patterns</groupId>
    <artifactId>cache</artifactId>
    <version>1.0.1</version>
    <name>cache</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

我已经在父级中声明了从slf4j的Logging,但是在eclipse中没有生成相同的maven依赖项,因此我无法在代码中使用日志记录。任何人都可以让我知道这里有什么问题。

1 个答案:

答案 0 :(得分:1)

在父pom中使用<dependencyManagement>允许通过子模块设置可用的依赖关系 这是一件好事,因为它集中了它们并指定了一个且只有一个版本 但要使用它们,子模块必须在<dependencies>标记的<build>标记中声明它们。

您可以通过这种方式修改子pom:

<dependencies>
...
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>    
    </dependency>
...
</dependencies>

如果您认为应该在任何子模块中添加这些依赖项,请在父pom声明中优先于dependencies而不是dependencyManagement

<dependencies>
...      
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.version}</version>
  </dependency>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
  </dependency>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>${logback.version}</version>
  </dependency>
...
</dependencies>

现在,您不需要在子模块中声明任何这些,因为这些依赖项已经包含在内。