Maven multi-module project example

An example of creating a Maven multi-module project in Eclipse

more 0
 

If you need to create multiple projects, which are both independent and related, then creating a Maven multi-module project is a very good choice, and very cool! How to create a multi-module project in Eclipse, and what to pay attention to, I will give a brief introduction here.

1. Preparation

If you want to do this in Eclipse, before doing all this, please make sure you have installed the maven plugin in eclipse. If there is no plugin installed, it can only be done through the command line.

Ok, now assuming that the maven plug-in has been installed in Eclipse, let's use Eclipse to create a Maven multi-module project!

2. Create the parent project first

  1. Inside Eclipse New ->  ;Maven Project
  2. Do not select "Create a simple project" in the popup (so you can use the archetype to create the project)
  3. Use the default Archetype (default: GroupId:org.apache.maven.archetypes,Artifact Id:maven-archetype-quickstart)
  4. 设置工程的参数,见下图
    <iframe id="iframe_0.2960056195753932" style="border: medium; border-image: none; width: 618px; height: 545px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.blogways.net/images/post/maven-modules1.png?_=4351921%22%20style=%22border:none;max-width:1518px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2960056195753932',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  5. Click Finish

In this way, we have created a Maven project according to the regular version. We also need to modify this project.

Because this is a parent project and does not need any source code, then we delete all unused directories under this project in Eclipse, leaving only the files. The directory structure after deletion is as follows:pom.xml

<iframe id="iframe_0.022282215906490377" style="border: medium; border-image: none; width: 227px; height: 72px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.blogways.net/images/post/maven-modules2.png?_=4351921%22%20style=%22border:none;max-width:1518px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.022282215906490377',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>

There is another one in the project , leave him alone, he will disappear automatically later.Maven Dependencies

In addition, it is necessary to modify thepom.xml<packaging>jar</packaging><packaging>pom</packaging>

At this time, a project error may be displayed in Eclipse. According to the prompt, select the project and click the pop-up menu Maven ->  , so it is OK.Update Project

3. Create sub-projects

  1. Select the newly created parent project and click New -> in the pop-up menu  ;Maven Module
  2. 如图配置
    <iframe id="iframe_0.8335390476344266" style="border: medium; border-image: none; width: 618px; height: 539px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.blogways.net/images/post/maven-modules3.png?_=4351921%22%20style=%22border:none;max-width:1518px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8335390476344266',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  3. Use the default Archetype (default: GroupId:org.apache.maven.archetypes,Artifact Id:maven-archetype-quickstart)
  4. 完成工程配置,见下图
    <iframe id="iframe_0.7988350133952454" style="border: medium; border-image: none; width: 618px; height: 531px;" src="data:text/html;charset=utf8,%3Cstyle%3Ebody%7Bmargin:0;padding:0%7D%3C/style%3E%3Cimg%20id=%22img%22%20src=%22http://www.blogways.net/images/post/maven-modules4.png?_=4351921%22%20style=%22border:none;max-width:1518px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.7988350133952454',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no"></iframe>
  5. Click Finish

In this way, a subproject is created. In the file system, the subproject will be built in the directory of the parent project. Run mvn testa command such as in the parent directory and all child projects will be executed in order.

A more careful person may find that while creating a sub-project through this step, the parent project will be modified , adding information similar to the following:pom.xml

<modules><module>module-children1-demo</module></modules>
     

This information is which submodules are marked.

Repeat the steps to create subprojects to create multiple subprojects.

Fourth, optimize the configuration

Although the above steps can complete the creation of multi-modules, the created multi-modules are still quite awkward in the eyes of a programmer. What's going on? Yes, there are repetitions. Let's refactor then.

The sub-project created by the above steps has a node in it, so he can inherit the relevant information of the parent project. That's right, there is an inheritance relationship between parent and child projects.pom.xmlparent

In the sub-project , the sub-project is generally the same as the parent project, then you can delete these two parameters of the sub-project, which will automatically inherit the value of the parent project.pom.xmlgroupIdversion

Similarly, if some other properties are the same in all sub-projects, they can be moved up to the parent project to set, and there is no need to repeat the settings in the sub-projects. For example: it can be set only once in the parent project.<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

In addition to this case, there is another case, that is, dependencies and plugins. The situation of dependencies and plug-ins is like this. A certain dependency or plug-in may be used by most sub-projects, but some sub-projects may not need to be used. In this way, simple inheritance is not appropriate using the above method.

Manen provides dependencyManagementand pluginManagementtwo tabs. Using these two tags, the configuration parameters of dependencies and plugins, such as version numbers, can be managed uniformly in the parent project. In the sub-project, you only need to list the sum of the dependencies and plugins groupIdto artifactIdbe used, and other information will be automatically obtained from the information managed by the parent project.

Look at the example, in the parent project:

<dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.5</version><scope>test</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId>
  
                   <version>1.7.5</version></dependency></dependencies></dependencyManagement>   

In the subproject:

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency></dependencies>
  
       

Fourth, the command line creation

The above are the steps to create multiple modules in Eclipse, and some optimized configurations.

The specific steps can be appropriately modified according to the actual situation. For example Archetype, when selecting, the appropriate one can be selected as required Archetype.

Some links in the above steps can also be generated through the command line first, and then modified to achieve.pom.xml

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228811&siteId=291194637