简体   繁体   English

如何在没有指定版本的情况下拥有多模块Maven项目?

[英]How can I have a multi-module Maven project without specifying the version all over the place?

I am currently learning Maven for a specific project. 我目前正在学习Maven的特定项目。 In this project, I need to have multiple modules (in the sense of a collection of classes and resources) that depend on each-other. 在这个项目中,我需要有多个依赖于彼此的模块(在类和资源的集合意义上)。 Specifically, I need a module to store the classes of a public-facing API (the API is a Java API, as the desktop program can load addons at runtime), another for a set of common classes that are shared between the client and server but that are not part of the API, and of course the client and server themselves need a module each. 具体来说,我需要一个模块来存储面向公众的API的类(API是Java API,因为桌面程序可以在运行时加载插件),另一个模块用于存储客户端和服务器之间共享的一组公共类。但这不是API的一部分,当然客户端和服务器本身也需要一个模块。

Using the system that I used before Maven (IntelliJ Idea's build system), I would simply create different modules and setup dependencies using IntelliJ's GUI. 使用我在Maven(IntelliJ Idea的构建系统)之前使用的系统,我只需使用IntelliJ的GUI创建不同的模块和设置依赖项。 This works well because I can then put the whole IntelliJ Idea project in a git repository, and keep everything nicely tracked together with no fuss and no problems. 这很有效,因为我可以将整个IntelliJ Idea项目放在一个git存储库中,并且可以很好地跟踪所有内容,而不会有任何问题。

When researching how to do this in Maven, however, I ran into some problems. 然而,在研究如何在Maven中执行此操作时,我遇到了一些问题。 Maven's documentation has a section that seems to explain how to accomplish something similar, but its technique seems to have two problems. Maven的文档有一节似乎解释了如何完成类似的东西,但它的技术似乎有两个问题。 When Each sub-project, with its own pom.xml, needs to specify the version of the other sub-projects that it depends on¹. 当每个子项目都有自己的pom.xml时,需要指定它依赖的其他子项目的版本¹。 This implies that I need to make many changes all over the project whenever the version changes. 这意味着每当版本发生变化时,我需要在整个项目中进行许多更改。 This will (hopefully) be quite frequent. 这将(希望)非常频繁。 As it is perfectly plausible that my project will grow to have hundreds of modules, this is obviously impractical. 由于我的项目将成长为数百个模块是完全合理的,这显然是不切实际的。

Additionally, based on my other research, it seems like a Maven repository is involved in the documented technique. 此外,根据我的其他研究,似乎Maven存储库涉及到文档化的技术。 Specifically, it seems like when, for example, the API module is built to be included in the client and server modules it will first be placed in a local repository and then the client and server modules will retrieve it from there. 具体来说,似乎例如,当API模块构建为包含在客户端和服务器模块中时,它将首先放置在本地存储库中,然后客户端和服务器模块将从那里检索它。 This sounds problematic as the API module will also be published in a public repository (maybe Maven central, I haven't really thought about this too much -- but it will be public in a repo), and this repository step seems like it could end up building the client and server with a published jar rather than the local one, which is problematic for many reasons. 这听起来有问题,因为API模块也将在公共存储库中发布(可能是Maven中心,我没有真正考虑过这个 - 但它将在回购中公开),而这个存储库步骤似乎可以最终使用已发布的jar而不是本地jar来构建客户端和服务器,这有很多原因。 (eg. if a developer is making local changes to the api jar, building half the project without those changes is a problem). (例如,如果开发人员正在对api jar进行本地更改,那么在没有这些更改的情况下构建项目的一半是一个问题)。

Is there some better way to combine multiple modules to mitigate these problems (versions all over the place, and getting wrong jars from repos)? 有没有更好的方法来组合多个模块来缓解这些问题(各地的版本,以及从repos获取错误的罐子)? Is it better to modify the technique in the linked documentation instead? 改为在链接文档中修改技术是否更好? Am I misunderstanding something and the problems don't exist? 我误解了什么,问题不存在吗?

I have checked DuckDuckGo and all relevant questions that I can find on StackOverflow, and none address either of the two aforementioned problems. 我检查了DuckDuckGo以及我在StackOverflow上可以找到的所有相关问题,但没有一个解决上述两个问题。

¹: ¹:

  <dependencies>
    <dependency>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

In your multi-module project, you usually use just one version for all modules. 在多模块项目中,通常只对所有模块使用一个版本。 This means that you can define dependencies between modules with <version>${project.version}</version> where the property ${project.version}$ is resolved during the build. 这意味着您可以使用<version>${project.version}</version>定义模块之间的依赖关系,其中在构建期间解析属性${project.version}$ Maven builds all the modules in the correct order. Maven以正确的顺序构建所有模块。

Regarding your first question: you can create a parent pom over your whole project. 关于你的第一个问题:你可以为整个项目创建一个父pom。 In this parent pom you can add a dependencyManagement section where you can list your own modules with their version. 在这个父pom中,您可以添加dependencyManagement部分,您可以在其中列出您自己的模块及其版本。 In your modules you then only need to specify the dependeny without the version. 在您的模块中,您只需要指定不带版本的依赖项。

Here an example: 这是一个例子:

in parent pom: 在父母pom:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>yourgroup</groupId>
            <artifactId>yourmoduleA</artifactId>
            <version>3.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>yourgroup</groupId>
            <artifactId>yourmoduleB</artifactId>
            <version>3.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</dependencyManagement>

and if your module A uses module B, you can add in pom of module A 如果模块A使用模块B,则可以添加模块A的pom

     <dependencies>
        <dependency>
            <groupId>yourgroup</groupId>
            <artifactId>yourmoduleB</artifactId>
        </dependency>
    </dependencies>

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

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