Starting a Spring Boot Microservices Maven Project
Tags: java, spring, maven, intellij
Published: 2022-05-26 07:02:06


## Create Parent Module

1. Open terminal, navigate to the place you want to create the project and run the following command:

```
mvn archetype:generate -DgroupId=[GROUP_ID(ie com.nutsnet)] -DartifactId=[ARTIFACT_ID(ie nutsnetservices)] -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

```

2. Open IntelliJ, Open the created project. Then go to **File > Project** Structure to make sure the Java version is what you want.

3. Delete the **src** folder which is auto generated by maven. Because here is the parent directory of our project, we will create our microservices under this parent directory .
![Delete default src folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrfl538ra25nwepuxvdi.png)

4. Open pom.xml. Set the project's website url and java compile version. 
![pom.xml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7782xdjp6xhqwh6nejty.png)

Get rid of all original dependencies and plugins, and finally, our pom.xml file will be like the following:

```xml
<?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>com.test</groupId>
  <artifactId>testservices</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>testservices</name>
  <url>http://www.test.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <dependencyManagement>
    <dependencies>
    </dependencies>
  </dependencyManagement>

  <dependencies>
 
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        
      </plugins>
    </pluginManagement>
  </build>
</project>

```

5. Now let's start adding dependencies and plugins that we want. 

- For the **dependencyManagement** tag, add `spring-boot-dependencies`.

- For the **dependencies** tag, we need `lombok` and `spring-boot-starter-test`

- For the **pluginManagement** tag, add `spring-boot-maven-plugin`.


```xml
<?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>com.test</groupId>
  <artifactId>testservices</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>testservices</name>
  <url>https://www.test.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <spring.boot.maven.plugin.version>2.5.7</spring.boot.maven.plugin.version>
    <spring.boot.dependencies.version>2.5.7</spring.boot.dependencies.version>
  </properties>

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

  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>${spring.boot.maven.plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

```
 6. Click the reload maven project button.
![Click the reload](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t744dt4tniki3t4u3079.png)

Then we can see the dependencies and plugins are loaded successfully.
![Maven reload successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/920l905oowijww6baajp.png)
  

## Create Microservices

1. Right click the parent module > **new** > **Module...**
![Create new module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2tx05elo5bnow6tmicog.png)

2. Click **New Module**, enter the module(service) name, select JDK version, and parent module. Then click **Create**.
![Enter new module details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0xqzvulq6axl715mfzn.png)
Now in the parent module's pom.xml, we can see the created sub-module(service) is in the **module** tag.
![parent module's pom.xml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/295rj3v6r3hrviilhm2z.png)

In the sub-module's pom.xml, we can also see the **parent** tag specify its parent module and the **artifactId** tag of the sub-module.
![sub-module's pom.xml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e7gpxi91z6cqx9rkvoh.png)
 

3. Add `spring-boot-starter-web` to the sub-module, and click reload maven project.
```xml
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
```

4. Create the main class.
![main class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4nmowvz3jjg66s4w2h3.png)

5. Create the application.yml file.
![application.yml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tglnbfx6xyfrj7dt0o7s.png)

6. Let's create a Spring Boot banner. (Optional)
 Go to [https://devops.datenkollektiv.de/banner.txt/index.html](https://devops.datenkollektiv.de/banner.txt/index.html) and copy the generated banner text.
Back to IntelliJ, Create banner.txt, and paste the banner text.
![banner.txt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75x70ao3xhnuzxizmhwa.png)

7. Run the spring boot application. Now our first service is running successfully !! 
We can keep adding other services like this first service.