Maven包括父类 [英] Maven include parent classes

查看:150
本文介绍了Maven包括父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

文件结构如下所示

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

我的父母pom看起来像这样:

My parent pom looks like this:

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

  ...

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

孩子看起来像这样:

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

  ...

</project>

这是否需要让孩子了解父母中定义的类和依赖关系?它似乎不是:eclipse给出编译错误,并且从父文件夹或子文件夹运行 mvn clean package 会在提到父类的任何时候产生找不到符号消息。

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 package from parent folder or child folder results "cannot find symbol" messages any time a class from parent is mentioned.

我做错了什么?

推荐答案

我要改变结构你的项目是这样的:

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


  • parent(pom)

    • core(jar,with曾经在父母中的所有类别)

    • 孩子(战争,取决于核心)

    父母:

    <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>
    

    核心:

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

    儿童:

    <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>
    

    这篇关于Maven包括父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆