[Java Spring Cloud Practical Road]-2 Create a project


0. Preface

The project uses Maven for management and construction, so Maven needs to be pre-configured. Well, I won't do too much introduction in this series.

image

1. Create a project

First create a pom.xml file and add the following content:

<?xml version="1.0" encoding="UTF-8"?>
<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>club.attachie</groupId>
   <artifactId>nature</artifactId>
   <packaging>pom</packaging>
   <version>${revision}</version>
   <properties>
       <revision>1.0-SNAPSHOT</revision>
   </properties>
</project>

The full name of POM is project object model, which is the project object model. It is the markup file of the maven project, in XML format, and the name is pom.xml. This file is used to manage source code, configuration files, developer information and roles, issue tracking system, organization information, project authorization, project address and dependencies.

The pom file is important to a maven project. A maven project can have no other files and content, but it must not be without a pom.xml file.

1.1 Project information

The pom file contains the basic information of the project:

  • groupId organization name

  • artifactId project name

  • version version number

1.2 Use placeholders

In the pom, we will use the version numbers of many projects, and sometimes some other constants. If the constant distribution is relatively scattered, it is not conducive to our maintenance and management. At this time, placeholders or attributes must be introduced. We declare the constants we need in the properties node:

<properties>
   <revision>1.0-SNAPSHOT</revision>
</properties>

This form is similar to declaring a variable

revision=1.0-SNAPSHOT

When you need this variable, just ${revision}use it.

2. Project structure layering

In an industrial-grade project, we need to carry out a reasonable stratification of the project. This is conducive to development and later maintenance.

So, first add these three directories under the root directory:

  • common is used to store some public packages

  • The parent package where the activities business module is located

  • manager is used to store some management tools of Spring Cloud, such as Spring boot admin, Spring Cloud Gateway, etc.

Add the pom.xml file in the three directories respectively, add:

<?xml version="1.0" encoding="UTF-8"?>
<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>club.attachie</groupId>
   <artifactId>各自的项目名称,建议是目录名</artifactId>
   <packaging>pom</packaging>
   <version>${revision}</version>
   <parent>
       <groupId>club.attachie</groupId>
       <artifactId>nature</artifactId>
       <version>${revision}</version>
   </parent>

</project>

And modify their respective artifactId.

Then register to the pom.xml of the root directory and mark these three items as sub-items.

<modules>
   <module>common</module>
   <module>activities</module>
   <module>manager</module>
</modules>

Then run:

mvn clean install

Check if the configuration is wrong.

3. Introduce public dependencies

Spring boot and Spring Cloud have established corresponding dependency packages, which allows us to not worry too much about the version conflicts of some common projects with Spring boot and Spring Cloud during the development process.

First declare the introduced Spring boot version number and Spring Cloud version number in the pom.xml file in the root directory:

<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>

Create a dependency management node and add Spring boot and Spring Cloud dependency packages:

<dependencyManagement>
   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-dependencies</artifactId>
           <version>${spring-boot.version}</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>${spring-cloud.version}</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>
   </dependencies>
</dependencyManagement>

The three-party packages that need to be used in the sub-packages should be managed by pom.xml in the root directory, and the managed packages will be placed under the dependencyManagement >> dependencies node.

To use dependencies in the project, add the following nodes under the project node of the pom.xml file. If there are already nodes, you can ignore them. Note that they are not under dependencyManagement .

<dependencies>

</dependencies>

Try to add the lombok package (this package is a well-known package in Java, you can omit the creation of methods such as get/set in Java).

<dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
</dependency>

The reason why the version number is not declared is because this package is already managed by spring-boot-dependencies. So we can directly introduce it here.

4. Summary

Now, we have set up a project shelf with nothing empty inside, but in this way a skeleton is formed, which will be enriched with flesh and blood in the later stage, making it more powerful.




Guess you like

Origin blog.51cto.com/15060511/2639822