DEV Community

Ed is Taking Note
Ed is Taking Note

Posted on • Updated on

Starting a Spring Boot Microservices Maven Project

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

Enter fullscreen mode Exit fullscreen mode
  1. Open IntelliJ, Open the created project. Then go to File > Project Structure to make sure the Java version is what you want.

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

  3. Open pom.xml. Set the project's website url and java compile version.
    pom.xml

Get rid of all original dependencies and plugins, and finally, our pom.xml file will be like 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>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>

Enter fullscreen mode Exit fullscreen mode
  1. 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 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>

Enter fullscreen mode Exit fullscreen mode
  1. Click the reload maven project button. Click the reload

Then we can see the dependencies and plugins are loaded successfully.
Maven reload successfully

Create Microservices

  1. Right click the parent module > new > Module...
    Create new module

  2. Click New Module, enter the module(service) name, select JDK version, and parent module. Then click Create.
    Enter new module details
    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

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

  1. Add spring-boot-starter-web to the sub-module, and click reload maven project.
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
Enter fullscreen mode Exit fullscreen mode
  1. Create the main class.
    main class

  2. Create the application.yml file.
    application.yml

  3. Let's create a Spring Boot banner. (Optional)
    Go to 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

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

Oldest comments (0)