java Maven 包含父类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10024320/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:18:41  来源:igfitidea点击:

Maven include parent classes

javainheritancemaven

提问by Dave

I have a fairly simple maven-ized Java project, but am having trouble getting my head around it.

我有一个相当简单的 Maven 化 Java 项目,但我无法理解它。

My parent module defines a lot of Java classes (and dependencies) that I expect to be useful for several child modules. One of the child modules is dedicated to deploying a web app, so it needs a few extra classes (the servlets) plus everything from the parent module.

我的父模块定义了很多 Java 类(和依赖项),我希望它们对几个子模块有用。其中一个子模块专门用于部署 Web 应用程序,因此它需要一些额外的类(servlet)以及父模块中的所有内容。

The file structure looks like this

文件结构看起来像这样

 - parent
   - src
   - pom.xml
   - child
     - src
     - pom.xml

My parent pom looks like this:

我的父 pom 看起来像这样:

<project>
  <groupId>my.group</groupId>
  <artifactId>parent</artifactId>
  <version>0.0.1</version>
  <packaging>pom</packaging>

  ...

  <modules>
    <module>child</module>
  </modules>
</project> 

And the child looks like this:

孩子看起来像这样:

<project>
  <artifactId>child</artifactId>
  <packaging>war</packaging>
  <parent>
    <artifactId>parent</artifactId>
    <groupId>my.group</groupId>
    <version>0.0.1</version>
  </parent>

  ...

</project>

Is this all I need to have the child know about the classes and dependencies defined in parent? It doesn't seem to be: eclipse gives compile errors, and running mvn clean packagefrom parent folder or child folder results "cannot find symbol" messages any time a class from parent is mentioned.

这是我需要让孩子知道父级中定义的类和依赖项的全部内容吗?它似乎不是:eclipse 会出现编译错误,并且在任何时候提到父级的类时,从父文件夹或子文件夹运行mvn clean package 都会导致“找不到符号”消息。

What am I doing wrong?

我究竟做错了什么?

回答by Roadrunner

I'd change the structure of your project like this:

我会像这样改变你的项目结构:

  • parent (pom)
    • core (jar, with all the classes that used to be in parent)
    • child (war, depends on core)
  • 父母(pom)
    • 核心(jar,所有过去都在父类中的类)
    • 孩子(War,取决于核心)

Parent:

家长:

<project>
  <groupId>my.group</groupId>
  <artifactId>parent</artifactId>
  <version>0.0.1</version>
  <packaging>pom</packaging>

  <modules>
    <module>core</module>
    <module>child</module>
    <!-- possibly more modules... -->
  </modules>
</project>

Core:

核:

<project>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1</version>
  </parent>
  <artifactId>core</artifactId>
  <packaging>jar</packaging>
</project>

Child:

孩子:

<project>
  <parent>
    <groupId>my.group</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1</version>
  </parent>
  <artifactId>module1</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>my.group</groupId>
      <artifactId>core</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</project>

回答by n0rm1e

Since your parent module's packaging is "pom", no jar file will be provided and no .class file from that will be accessible to other modules. Try changing it to "jar" and give it another try.

由于您的父模块的包装是“pom”,因此不会提供 jar 文件,并且其他模块无法访问其中的 .class 文件。尝试将其更改为“jar”并再试一次。

Although it's not a good practice to have a parent as "jar". I'd stick to the "pom" parent and probably create a new "core" or "common" child and make my "war" depend on it.

尽管将父母作为“jar”并不是一个好习惯。我会坚持“pom”父级,可能会创建一个新的“核心”或“普通”子级,并使我的“War”依赖于它。

回答by MykoB

Try to add an internal dependency in child module pom, so it is familiar with its parent

尝试在子模块 pom 中添加内部依赖,使其熟悉其父模块

<dependency>
   <groupId>my.group</groupId>
   <artifactId>parent</artifactId>
   <version>0.0.1</version>
</dependency>

回答by khmarbaise

The first thing you should do is to define your dependencies within a dependencyManagmeentblock in your parent like this:

您应该做的第一件事是在您的父级中的dependencyManagmeent块中定义您的依赖项,如下所示:

<dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>group-a</groupId>
       <artifactId>artifact-a</artifactId>
       <version>1.0</version>
     </dependency>
     ...
   </dependencies>
</dependencyManagement>

and furthermore in your child you should say which dependency your child would like to use as dependency:

此外,在您的孩子中,您应该说明您的孩子想使用哪个依赖项作为依赖项:

   <dependencies>
     <dependency>
       <groupId>group-a</groupId>
       <artifactId>artifact-a</artifactId>
       <!-- No version given. See dependencyManagement -->
     </dependency>
     ...
   </dependencies>