Architecture direction - build a multi module single architecture

If you need to practice the single architecture or need to build a single multi module, this article can serve as a reference

1. Construction of the outermost layer of the project

Click new project and select maven

Click Next

Enter our groupId and artifact ID and click next


Choose our own maven warehouse and configuration

Delete our src folder and leave only one pom file. We need to modify the pom file. Just leave

<?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.weiyi</groupId>
  <artifactId>single</artifactId>
  <version>1.0-SNAPSHOT</version>
  
  <name>single Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

So our outermost module is built

2. Module building


Click as shown in the figure, and then follow the same steps as the first step. The difference is that if the new module is a public module, just select quickstart
This option is used for public modules. jar is recommended for its packaging method. After the creation, there will be one more pom file in the outermost layer. This means which modules I have below,

<?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.weiyi</groupId>
  <artifactId>single</artifactId>
  <version>1.0-SNAPSHOT</version>
    <modules>
        <module>common</module>//The name of the new module you created (this place is only for explanation, please delete if you paste it)
    </modules>
    <packaging>pom</packaging>

  <name>single Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

Common dependency module

In order to unify dependency, we can create a new public dependency module, which requires nothing but one pom.xml file
Let's build a new module called father

<?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>
<parent>//pom on behalf of reference layer
     <groupId>com.weiyi</groupId>
     <artifactId>single</artifactId>
     <version>1.0-SNAPSHOT</version>
     <relativePath>../pom.xml</relativePath>
</parent>

     <artifactId>father</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!-- Environment configuration -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
    //What dependency needs to be added at your own discretion is a public dependency
    </dependencies>

    <build>
        <plugins>
            <!-- Compiler plug-in unit, set up JDK edition -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>

        <!-- Resource file configuration -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

</project>

2.2 public module pom modification

We have created a new public dependency module, so we have to use it naturally. Now let's modify the pom file of the common module

<?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>
<parent>//This represents the dependency of referencing the other module, and sets the packaging mode to jar, which is convenient for us to reference
  <groupId>com.weiyi</groupId>
  <artifactId>father</artifactId>
  <version>1.0-SNAPSHOT</version>
  <relativePath>../father/pom.xml</relativePath>
</parent>

  <artifactId>common</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>common</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
</project>


This is an example we used in the common module. I didn't introduce spring in the common module, but I can use its annotation. The reason is that I introduced spring dependency in my father, and the common module used dependency in my father

3. Calls between modules

We have created a new common module. As the name implies, the common module must be public methods and constants, which need to be provided for others to reference. If we have created a new front module to call the common method, we only need to add it to the pop file of the front module

<dependency>
      <groupId>com.weiyi</groupId>
      <artifactId>common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

We write a method in common, which is called useTest

Next, we call in the front module

You can see that the execution is successful. If it does not attract common dependency, it will report an error.
End of building example

Tags: Maven Apache xml snapshot

Posted on Sun, 07 Jun 2020 02:55:10 -0700 by varasurf