简体   繁体   English

具有多个模块的Maven项目结构

[英]Maven Project Structure with Multiple Modules

I am somewhat new to the Maven project structure. 我对Maven项目结构有些陌生。 I am creating a project this is going have two jars. 我正在创建一个项目,它将有两个罐子。 Neither jar is dependent on each other, however, they will use some of the same libraries and there are two helper classes I created (one for logging) that would be used in both. 两个jar都不相互依赖,但是,它们将使用某些相同的库,并且我创建了两个助手类(一个用于记录),这两个都将使用。

I was following this guide as far as project structure goes: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html 就项目结构而言,我一直在遵循本指南: http : //maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

My current project only has one module. 我当前的项目只有一个模块。 I am using the IntelliJ IDE to create my build.xml (using ANT) to create my Jars. 我正在使用IntelliJ IDE创建我的build.xml(使用ANT)来创建我的Jars。 The IntelliJ build.xml, however, did not work off the bat and I had to do some manual editing to have it build both jars. 但是,IntelliJ build.xml并没有奏效,我必须进行一些手动编辑才能构建两个罐子。 I believe this would be solved if each jar was in it's own module. 我相信,如果每个jar都位于其自己的模块中,则可以解决此问题。 Also, according to the following, it seems they should be anyway http://www.jetbrains.com/idea/webhelp/module.html 另外,根据以下内容,看来它们应该还是http://www.jetbrains.com/idea/webhelp/module.html

This is where I am a little confused. 这是我有点困惑的地方。 If I do create a second module for my second jar, how do I deal with classes that are shared by both modules? 如果确实为第二个jar创建了第二个模块,该如何处理两个模块共享的类? Whenever I go to create a new module , IntelliJ gives the new module it's own src/ path. 每当我创建一个新模块时,IntelliJ都会为新模块提供自己的src /路径。

As I said, I am new to the Maven project structure. 正如我所说,我是Maven项目结构的新手。 I am also fairly new to ant and building jars using a build.xml. 对于使用build.xml构建jar的蚂蚁,我也相当陌生。 If I am completely on the wrong path please let me know so I can fix this problem early. 如果我走错了路,请告诉我,以便我尽早解决此问题。

Thanks ahead of time. 提前谢谢。

Create a multi-module project (x) which contains a pom.xml with packaging = pom 创建一个多模块项目(x),其中包含带有包装= pom的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>x</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>x1</module>
    <module>x2</module>
  </modules>
</project>

and 2 regular (jar) projects (x1 and x2). 和2个常规(jar)项目(x1和x2)。 This is how the project structure should look like 这就是项目结构的样子

x
  x1
    ...
    pom.xml
  x2
    ...
    pom.xml
pom.xml

The main project pom should contain dependencies common to both modules. 主项目pom应该包含两个模块共有的依赖项。 Nested projects poms should have a reference to the parent. 嵌套项目poms应该引用父项。

<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>test</groupId>
    <artifactId>x</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>x1</artifactId>
</project>

See more here http://maven.apache.org/guides/mini/guide-multiple-modules.html 在此处查看更多信息http://maven.apache.org/guides/mini/guide-multiple-modules.html

If you are going to do things the maven way you should remember that each (maven-)project only builds one artifact. 如果您打算以行家的方式做事,那么您应该记住,每个(行家)项目都只能构建一个工件。 So if you want to build two jars you will need two maven-projects (p1,p2) each with their own pom.xml. 因此,如果要构建两个jar,则需要两个maven项目(p1,p2),每个项目都有自己的pom.xml。

If you got some classes that are used by both of these projects they will have to go to their own maven-project as well to build their own module-jar (p3). 如果您获得了这两个项目都使用的某些类,则它们也必须转到自己的maven-project来构建自己的模块jar(p3)。 p3 will then be included in p1 and p2 as a dependency. 然后p3将作为依赖项包含在p1和p2中。

To build these jarrs in one go you could resort to the module-layout suggested by Evgeniy Dorofeev 要一次性构建这些jars,您可以诉诸Evgeniy Dorofeev建议的模块布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM