Maven steps to establish a parent-child project and invoke content across projects

Source: Internet
Author: User

The new company uses MAVEN to manage the project, and we do the Micro service, so for the Micro Service project, the relationship between using Maven to build a parent-child project is easy to manage (just personal feeling)


Design Level

(1) The server and the client must be written separately, and there is a public project, such as some entity classes that can be put into this public project as a public class.

(2) In this way, there are at least three subprojects, server (service layer subprojects), web (view layer subprojects), common (public-level subprojects). It's not a definition, ha, I started it myself.

(3) The parent project can be named parent, which constitutes the concept of a parent-child project. The following starts up.

2017-05-21 added Note: A small project has been built these days. To the leadership, the leader said I design mistakes, there will be hundreds of micro-service projects, common if as a single micro-service public layer, it is a bit wasteful, the leader said should be put all the micro-service sub-project common things, specifically I do not understand, So here's a misunderstanding, sorry, but the way to build a parent-child project is this, but the micro-service project design is not like this, you can refer to ...


Create a parent project

(1) This picture below is the difference between a parent-child project, which is built by Maven project, or project. A subproject establishes a module, or modules. are as follows:

(2) Select Maven Project to start building the parent item. The following figure, select Create a simple project, cause the parent project needs to establish a packaging for POM, subproject established packaging for jar or war, check here, in the next step you can choose Packaging:

(3) again, the packaging of the parent project selects Pom, the subproject chooses the jar or the war, by the way, in general, to create a project that runs in Tomcat, it should be the creation of a war, such as the service subprojects and web subprojects described in this article, It should be a built war, but the exception is that if you want to create the spring boot micro-service project, because spring boot is in rich jar form, all of the subprojects need to be jars, and this is just a personal understanding, based on your own needs.


(4) If you accidentally set the parent project's packaging to be a pom, you can open the project's Pom.xml file and modify it. are as follows:


(5) After the save, the project will error, direct project Right-click Select Maven------->update project can.

To create a child project

(1) The creation of a subproject must select Maven module, and in the meantime, make the parent project, and be careful not to forget to change the packing of the parent project from the jar to Pom in the front, creating the subproject steps as follows:


(2) After the creation, you can see the following:


(3) in the same vein, after the service subproject and the Web subproject are established, clicking on the Pom.xml in the parent project will see the following:


(4) This completes the creation of the parent-child project. However, there are some configurations for how projects relate to projects.


accessing content across projects

For example, the common subproject in this article is a public project in which you can store common things, such as entity classes, tool classes, and so on, which can be called directly in service subprojects and web subprojects, and you need to add a dependency tag if you achieve this function. As an example of invoking the common subproject as a service subproject, the steps are as follows:

(1) test requirements: First create a person class in the common subproject, and then create a Testperson class in the service subproject to invoke the person class in the class.


(2) In fact, to be in the Service Child project to introduce common dependencies can be. as follows, add the following in the pom.xml of the Service Child project:


(3) add more than one dependency tag to the service subproject, which points to the common subproject, which is equivalent to getting the contents of the common subproject, note that the above content is written, if you have to common the following in the subproject, This content defaults to exist, as long as not to delete, there will always be, otherwise, even in the service subproject fill in the above dependency, also can not get to the content.


(4) OK, in the service sub-project test some of it, as follows:


(5) This completes the call between projects, and in the future puts all the public content into the common subproject, as well as the web subprojects that want to introduce common subprojects.

(6) In addition to the common classes, can be public, also include jar packages, can be imported in the common subproject, so that the service child project can also use the jar package.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.