来自父 POM 的 Maven 3 依赖项对 Eclipse 中的子项不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13894701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven 3 dependencies from parent POM not available to children in eclipse
提问by Jazzepi
I have a maven 3 project in eclipse who's structure is as follows. The app project serves as the parent project to all the other apps. I have included the pom.xml from the app project (first pom), and the pom.xml from the app-web project. (second pom)
我在 Eclipse 中有一个 maven 3 项目,其结构如下。应用程序项目充当所有其他应用程序的父项目。我已经包含了来自应用程序项目(第一个 pom)的 pom.xml 和来自 app-web 项目的 pom.xml。(第二个pom)
app
app-ear
app-ejg
app-web
app
app-ear
app-ejg
app-web
The problem I'm having is that I can see the dependencies from the parent in the effective pom of the app-web/pom.xml but my java classes in the app-web project cannot import from these dependencies. I get "The import com.fasterxml cannot be resolved". I have this problem for the app-ejb project as well. I don't think this is just an "eclipse error" either. If I try to mvn clean install from the app project I get compilation errors complaining about the same thing.
我遇到的问题是我可以在 app-web/pom.xml 的有效 pom 中看到来自父级的依赖项,但我在 app-web 项目中的 java 类无法从这些依赖项中导入。我得到“无法解析导入 com.fasterxml”。我的 app-ejb 项目也有这个问题。我也不认为这只是一个“日食错误”。如果我尝试从应用程序项目中 mvn clean install 我得到编译错误抱怨同样的事情。
By the way the only change I've made is moving the dependencies up out of the pom.xml of the children and into the parent. I wanted to centralize things as much as possible and keep my project config as DRY as possible. Before moving the dependencies up the hierarchy things worked fine. I am using Eclipse Juno for Java EE developers with m2e, m2e-wtp, and WTP patches installed.
顺便说一下,我所做的唯一更改是将依赖项从子项的 pom.xml 移到父项中。我想尽可能地将事情集中起来,并使我的项目配置尽可能保持干燥。在将依赖项向上移动层次结构之前,一切正常。我正在为 Java EE 开发人员使用 Eclipse Juno,并安装了 m2e、m2e-wtp 和 WTP 补丁。
app/pom.xml
应用程序/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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boardgamebuilder</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>app application</name>
<modules>
<module>app-ejb</module>
<module>app-web</module>
<module>app-ear</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java-version>1.6</java-version>
<org.slf4j-version>1.7.2</org.slf4j-version>
<com.fasterxml.Hymanson-version>2.1.1</com.fasterxml.Hymanson-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- JSON handler -->
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-annotations</artifactId>
<version>${com.fasterxml.Hymanson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>${com.fasterxml.Hymanson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>${com.fasterxml.Hymanson-version}</version>
</dependency>
<!-- RESTful servlet -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.16</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.7</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- Misc -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<!-- Define the version of the EJB jar so that we don't need to repeat
ourselves in every module -->
<dependency>
<groupId>com.boardgamebuilder</groupId>
<artifactId>app-ejb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<!-- Define the version of the WAR so that we don't need to repeat ourselves
in every module -->
<dependency>
<groupId>com.boardgamebuilder</groupId>
<artifactId>app-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation
processors -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
app-web/pom.xml
app-web/pom.xml
http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0
http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0
<parent>
<artifactId>app</artifactId>
<groupId>com.boardgamebuilder</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>app-web</artifactId>
<packaging>war</packaging>
<name>app Web module</name>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>
</build>
回答by Sri Sankaran
After moving the Hymanson-annotations
and Hymanson-core
dependencies to the parent POM you still need to reference it in the app-web
module by specifying (minimally) the groupId
and artifactId
. Maven documentationexplains this usage.
将Hymanson-annotations
andHymanson-core
依赖项移动到父 POM 后,您仍然需要app-web
通过(最少)指定groupId
and来在模块中引用它artifactId
。 Maven 文档解释了这种用法。
回答by Nitin T
If your war app pom has <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
exclude it and check
如果您的War应用程序 pom 已<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
排除它并检查