简体   繁体   English

如何使用Maven运行多模块项目

[英]How to run a multiple module project using Maven

I'm trying to make my Java app modular, so that there will be the core base module that a client will have to aqcuire, but he/ she will be able to add additional features/ plugins as they come up, or as her needs extend. 我正在尝试使我的Java应用程序模块化,以便有一个客户端必须要使用的核心基本模块,但是他/她将能够根据需要添加其他功能/插件。延伸。 Like how IDEs like NetBeans go about it. 就像像NetBeans这样的IDE一样。

This is how far I've gone trying to implement this using Maven: 这是我尝试使用Maven实施此操作已经走了多远:


I start by positioning myself in the workspace directory and creating the main (or parent project) by issuing the following Maven command: 我首先将自己放在工作区目录中,然后通过发出以下Maven命令来创建主(或父项目):

mvn archetype:create -DgroupId=com.byteslounge.main -DartifactId=com-byteslounge-main 

Now you I delete the src folder since it will not be used. 现在,您将删除src文件夹,因为它将不被使用。 After this I edit my pom.xml file and change jar to pom. 之后,我编辑pom.xml文件,然后将jar更改为pom。

After creating the main project I create the main library module. 创建主项目后,我将创建主库模块。 This is what a user will download and add extend by adding other modules to. 用户将通过下载其他模块来下载并添加扩展。 From the main project directory, I issue the following Maven command: 从主项目目录,我发出以下Maven命令:

mvn archetype:create -DgroupId=com.byteslounge.jar -DartifactId=com-byteslounge-jar 

Now I create the the first module by issuing the following command: 现在,我通过发出以下命令来创建第一个模块:

mvn archetype:create -DgroupId=com.byteslounge.lib -DartifactId=com-byteslounge-lib

This module should be called whenever the main program, com-byteslounge-jar, above is called. 每当调用上面的主程序com-byteslounge-jar时,都应调用此模块。

How can I compile the project in maven in such a way that all modules get called whenever com-byteslounge-jar is run? 我如何在maven中编译项目,以便在运行com-byteslounge-jar时都调用所有模块?


PS: PS:

Is this the best way to go about this, considering that I will later need to fully automate this by having an implementation of the The ServiceLoader Class in this project. 考虑到以后我将需要在该项目中实现ServiceLoader类的实现来使其完全自动化,因此这是解决此问题的最佳方法。 I plan on having a sub-directory, called modules/ plugins, in the clients pc where any plugin applications will be included as .jar files. 我计划在客户端PC中有一个子目录,称为模块/插件,其中任何插件应用程序都将作为.jar文件包含在内。 The main module (com-byteslounge-jar) will, when a user launches the application, include these other plugins in the final single program. 当用户启动应用程序时,主模块(com-byteslounge-jar)将在最终的单个程序中包含这些其他插件。

To expand on the link that @jan-doerrenhaus provides in the comments. 要扩展@ jan-doerrenhaus在评论中提供的链接。

Your top level pom should reference the two sub-folders as 'modules' using something like this: 您的顶级pom应该使用如下方式将两个子文件夹引用为“模块”:

<modules>
    <module>com-byteslounge-jar</module>
    <module>com-byteslounge-lib</module>
</modules>

You then need to build your projects from the top level directory. 然后,您需要从顶层目录构建项目。 Maven will automatically work out the dependency ordering to compile the projects in the correct ordering. Maven将自动计算出依赖项排序,以正确的顺序编译项目。

It's difficult to tell from your question, but you may be creating the sub-projects while in the wrong directory - you should create a folder for each of the sub projects, and then run the mvn archetype:create inside the subdirectory. 从您的问题很难看出来,但是您可能在错误的目录中创建子项目-您应该为每个子项目创建一个文件夹,然后在子目录中运行mvn archetype:create You should have a directory structure which looks like this 您应该具有如下所示的目录结构

com-byteslounge-jar/
com-byteslounge-jar/pom.xml
com-byteslounge-lib/
com-byteslounge-lib/pom.xml
pom.xml

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

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