Java Spring Cloud Road to Actual Work - 1 Create Project

0. Preface

The project is managed and built using Maven, so it needs to be preconfigured.Well, I haven't done much in this series.

1. Create a project

Create one firstPom.xmlFile, add the following:

<?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 POM full name project object model, also known as the project object model, is a label file for a maven project, in XML format, named asPom.xml.This file is used to manage source code, configuration files, developer information and roles, problem tracking systems, organization information, project authorization, project addresses, and dependencies.

pom files are important for maven projects. A Maven project can have no other files and content, but it can never have nonePom.xmlFile.

1.1 Project Information

The pom file contains basic information about the project:

  • groupId Organization Name
  • artifactId project name
  • Version version number

1.2 Use placeholders

In pom we use a lot of project version numbers and sometimes other constants.If the constant distribution is scattered, it will be detrimental to our maintenance and management.Placeholders, or attributes, are introduced.We declare the required constants within the properties node:

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

This is similar to declaring a variable

revision=1.0-SNAPSHOT

Use ${revision} when you need this variable.

2. Hierarchy of project structure

In an industrial-level project, we need to have a reasonable hierarchy of projects.This is conducive to development and later maintenance.

First add the three directories under the root directory:

  • common is used to store some public packages
  • The parent package where the activities business module resides
  • manager is used to store some management tools for Spring Cloud, such as Spring boot admin, Spring Cloud Gateway, etc.

Add in three directoriesPom.xmlFile, 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>Individual project names, recommended directory names</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <parent>
        <groupId>club.attachie</groupId>
        <artifactId>nature</artifactId>
        <version>${revision}</version>
    </parent>

</project>

And modify their artifactId.

Then register it with the root directoryPom.xmlTo mark the three items as subprojects.

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

Then run:

mvn clean install

Check the configuration for errors.

3. Introducing Public Dependency

Spring boot and Spring Cloud have built dependency packages that allow us to develop without worrying too much about version conflicts between common items and Spring boot and Spring Cloud.

First under the root directoryPom.xmlThe file declares the imported Springboot version number and Spring Cloud version number:

<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>

All three-party packages needed in a subpackage should be from the root directoryPom.xmlManage, and the managed packages are placed under the dependency management >> dependencies node.

Use dependencies in projects, inPom.xmlAdd the following nodes under the project node of the file, and if you already have one that you can ignore, note that it is not under dependency management.

<dependencies>
    
</dependencies>

Try adding the lombok package, which is a well-known package in Java and can omit the creation of get/set methods in Java.

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

The version number is not declared because the package is already managed by spring-boot-dependencies.So we can just bring it in here.

4. Summary

Now we have built a project framework with nothing empty inside, but this creates a framework which will enrich the blood and flesh of the framework and make it more powerful in the future.

Please pay attention to more content My blog, Mr. Gao's Cabin

Tags: C# Spring Maven Apache xml

Posted on Sun, 31 May 2020 12:34:53 -0400 by widget