Some problems encountered in Maven multi-module compilation during the game server construction process

        

Table of contents

1. Creation of multiple modules

1.1 Creation of the parent module

 1.2 Delete junk files

1.3 Modify pom.xml

1.4 Create submodule inheritance

2. Mutual references between submodules

3. Version management among multiple modules

3.1 dependencis

3.2 dependencyManagement

4. Dependency execution

5. There are several common methods for loading components of dependent projects in Spring Boot projects

5.1. Use @ComponentScan annotation:

2. Use the @Import annotation:

3. Use @Configuration annotation and @Bean method:

6. @ComponentScan scans dependencies for packages


        There are several projects in the game server, so multiple modules are involved. This development is often encountered, but because I haven’t paid much attention to it before, I still don’t understand some details in the development process. I will record it this time and share the same problem with students who have never tried it.

1. Creation of multiple modules

Using idea to create modules is mainly divided into the following steps

1.1 Creation of the parent module

Create a Maven project directly, choose Spring web and Lombok in this process, or choose other packages, and save handwriting at that time

 1.2 Delete junk files

Delete all the files, just leave pom.xml, because the parent module is just a module and dependency management, so no code is needed.

1.3 Modify pom.xml

To modify the file of this parent module pom.xml, first delete <dependencies>the node, <build>node and <properties>all: then modify the version number to define it for yourself (to facilitate subsequent sub-modules to specify the parent 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>MultMoudle</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MultMoudle</name>
    <description>MultMoudle</description>
    <packaging>pom</packaging>
</project>

1.4 Create submodule inheritance

 Inherit parent module

You can see that the pom of the parent module already has the configuration of the submodule.

2. Mutual references between submodules

Because some interface files need to be used together in several projects, it is very reasonable to extract the same files into the common project. How to reference them in room and game is very simple, just like other jar packages

        <dependency>
            <groupId>com.pdool</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

Use the coordinates and version to reference, refresh the reference and you can see it in the reference

 

3. Version management among multiple modules

The dependencies used in multiple modules are likely to be duplicated, but how to manage each version?

Assuming that both of the above module-oneand module-twoabove require dependencies fastjson2, we will add dependencies to the pom.xml of the two modules before, but such repeated configurations seem inelegant. If you need to upgrade, you need to change two places. At this time, the management function of the parent module can be used.

3.1 dependencis

 In the above figure, two commonly used tool libraries have been added to dependencies, and this library can be used even if it is not added to the submodule! Because in addition to using its own dependencies, submodules will also look up the dependencies of the parent module, that is to say, the dependencies of the parent module are inherited downwards , so we can write the dependencies that all modules need to use in the parent module.

Therefore, both modules depend on Spring Webwords, and the dependencies of the two modules can also be Spring Webmoved to the parent module.

So in the parent module and the child module, dependencies also have an inheritance relationship! Parent modules propertiesare also inherited downwards.

3.2 dependencyManagement

dependencyManagementFor managing dependent versionspom.xml , we add this tag to the parent module :

dependencyManagementNotes:

  • dependencyManagementIt is only used to manage versions, and does not import dependencies for itself and submodules , so dependencyManagementafter declaring dependencies in , the corresponding submodules still need to dependenciesadd dependencies in
  • In pom.xmland siblings, and dependencyManagementalso need to have adependenciesdependencyManagementdependencies
  • dependencyManagementNot only can manage the dependency version of submodules, but also can manage its own dependency version
  • If you do not want a submodule to use the version of the parent module , then declare the corresponding version dependencyManagementin the submoduledependencies

4. Dependency execution

Because mybatis-plus is used in the project, some mappers are defined in the common module, which need to be used in room and game, and componentscan is added to the project, but it still reports an error when running, indicating that the class in common cannot be found, and no error is reported in the editor. This is very strange, and a label needs to be added to the dependent project.

module-twoThis is due to the mode problem of Spring Boot packaging. We open the file of the dependent module pom.xmlto find the bottom <build>node, and spring-boot-maven-pluginadd the following configuration in the plug-in section:

<classifier>exec</classifier>

The definition of pom in the final common

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>execute</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

5. There are several common methods for loading components of dependent projects in Spring Boot projects

There are several common ways to load components of dependent projects in Spring Boot projects:

5.1. Use @ComponentScan annotation:

Use the @ComponentScan annotation on the main application class to specify the package path to scan. This will make Spring Boot scan and load components annotated with @Component, @Service, @Repository, etc. in dependent projects. For example:

   @SpringBootApplication
   @ComponentScan(basePackages = "com.example.dependencyproject")
   public class MyApplication {
       // ...
   }

2. Use the @Import annotation:

Use the @Import annotation on the main application class to introduce configuration classes or component classes in dependent projects. This will make Spring Boot load these configuration classes and register components within them. For example:

   @SpringBootApplication
   @Import(com.example.dependencyproject.MyConfiguration.class)
   public class MyApplication {
       // ...
   }

3. Use @Configuration annotation and @Bean method:

If there is a configuration class annotated with @Configuration in the dependent project, you can use the @Bean method in the main application class to load the components in it. For example:

   @SpringBootApplication
   public class MyApplication {
       // ...
       
       @Bean
       public MyComponent myComponent() {
           return new MyComponent();
       }
   }

   In this way, MyComponent will be loaded into the Spring application context as a bean.

According to your specific situation and the structure of the dependent project, you can choose the appropriate method to load the components of the dependent project. Note that in order to be able to load the dependent project's components, ensure that the dependent project has been properly added as a dependency of the project and can be properly referenced and accessed during the build and deployment process.

6. @ComponentScan scans dependencies for packages

The @ComponentScan annotation can be used to specify the package to be scanned, and its role is not limited to scanning only dependent packages. @ComponentScan` can scan the components under the specified package and its subpackages, and load them into the context of the application.

When you use the `@ComponentScan` annotation on the main class of a Spring Boot application, it will scan the specified package and its subpackages, and register components annotated with `@Component`, `@Service`, `@Repository`, `@Controller`, etc. in these packages.

If the package path you specify includes the package of the dependency, then it will scan and load the components in the dependency. However, it should be noted that `@ComponentScan` will not limit the package that only scans dependencies, it will scan all components under the specified package path.

For example, suppose you have the following `@ComponentScan` annotation on the main class of your Spring Boot application:

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.myapp", "com.example.dependency"})
public class MyApplication {
    // ...
}

In the above example, `@ComponentScan` will scan for components under the `com.example.myapp` package and its subpackages, and the `com.example.dependency` package and its subpackages, and load them into the application's context. This way, you can load components from dependencies and components of the application itself at the same time.

In short, the `@ComponentScan` annotation is not limited to scanning dependent packages, it can scan all components under the specified package and its subpackages, and load them into the context of the application.

Note:

If you use annotations on the main class of the Spring Boot application @ComponentScanand specify a package path, only the components of this project under the specified package path will be automatically loaded into the application context.

@ComponentScanThe annotation scans only components under the specified package and its subpackages and loads them into the application's context. If the components in this project are not under the specified package path, they will not be loaded automatically.

7. Summary

If you don't do it, you don't know. You have to practice


Jingdong link: https://item.jd.com/14010448.html

feature

No omissions: comprehensive coverage of Dubbo core knowledge points

Straight to the point: Practical cases and precise positioning of technical details

Apply what you have learned: essential presentations ensure that development and learning are not disconnected

Subtle influence: Grinding knowledge to explain key points of infiltration technology

Efficiency improvement: vertical technology does not spare detours

Sequential improvement: progressive knowledge point arrangement ensures coherence

Supporting resources: Give the case source files of the whole book to help you learn

Guess you like

Origin blog.csdn.net/perfect2011/article/details/131655640