springboot extracts a submodule in the project into an independent project

Problem phenomenon:

Recently, I encountered the need to extract the sub-modules of a project into a separate project in my work. After the extraction, an error occurred when running the project:

java.lang.AbstractMethodError: null


problem analysis:

Checking the error message, it was found that the abstract method error was prompted because the relevant mapping relationship could not be found.

I didn't understand what it meant at all, so I searched the Internet and found that it is basically a problem caused by the inconsistency of the versions of springboot and springcloud .

So I checked the dependency package and found that the springcloud dependency package is not needed in this submodule, which is not the reason.

 

Through testing, it is found that the submodule only needs one springboot dependency package in the parent module to be referenced. The springboot dependency configuration in the parent module is as follows:

    <properties>
        <spring.boot.version>2.1.11.RELEASE</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <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>
            <!--其他依赖-->
        </dependencies>
    </dependencyManagement>

The pom.xml configuration  after extracting the submodules into independent projects is  as follows:

​    <properties>
        <spring.boot.version>2.1.11.RELEASE</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <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>
            <!--其他依赖-->
        </dependencies>
    </dependencyManagement>

It can be seen should be springboot dependent on the introduction of a problem, the parent module in dependence on springboot set:

                <type>pom</type>
                <scope>import</scope>

Meaning: Introduce the configured dependency into the pom.xml file of the submodule.

So after submodule extraction, there is no need to configure this way , so I modified the sprinboot dependency configuration after extracting the submodule into an independent project to:

​    <properties>
        <spring.boot.version>2.1.11.RELEASE</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>
            <!--其他依赖-->
        </dependencies>
    </dependencyManagement>

But the same error was reported. After many failed attempts, I remembered that the configuration dependent on springboot seemed to be different from the general dependent package configuration , so I reviewed the relevant information and found the problem:

It turns out that when the current module (a standalone project without a parent module) uses the springboot dependency package, the <parent> tag needs to be used instead of placed in the <dependencies> tag, so it is modified to the following configuration:

​    <properties>
        <spring.boot.version>2.1.11.RELEASE</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <!--其他依赖-->
        </dependencies>
    </dependencyManagement>

Note  <parent> in

<version>2.1.11.RELEASE</version>

Cannot be written as

<version>${spring.boot.version}</version>

Because the variables configured in < properties > cannot take effect here!

After configuration


Solution:

step:

1.  First , copy the sub-module (S ) in the parent module (P) , then use IDEA to open the P project and the  S project, and open the respective pom.xml files .

2.  Pay attention to the Project Settings configuration problem of the project:

2.1 SDK is guaranteed to be above JDK1.8

2.2 Language Level is generally set to 8-Lambdas...

3. The S project any original  <parent> ...... </ parent> tag (which tags include all sub) deleted, let it be an independent program.

4. The P item  of  <plugins> in maven-compiler-plugin plug  copied to S project go , or will run the program given below:

5. The  parent module (P)  in the sub-module (S) requires a reference to the dependency into the project S </ dependencies> tag  go (do not rely on introducing springboot).

6. In addition to the </dependencies> tag of the S project , create a <parent></parent> tag, and then import the springboot dependency in this tag, and keep the version number consistent with the springboot dependency in the P project, such as:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
    </parent>
    <dependencies>
        <!--该项目所引用的依赖包-->
    </dependencies>

7. In Maven Projects , click  reimport (reimport) the dependency, and then if you still see the  dependency package reported in red (red underline) , you can restart the project and take a look.

8.  Start the project successfully, and the test interface is available!


Expansion

Here is another skill to share, that is, after extracting the sub-modules into independent projects, you may need to submit this new project to the code warehouse.

There are many ways, here is just the command:

(If the code repository is empty, you can execute the following command)

1. In the git window (open the computer to the project root directory, that is, you can see the path of the .idea and src folders, and then right-click and select Git Bash Here, you can see the git window, but don’t forget, you need to install git in advance)

Enter the following commands in order:

git init

git remote add origin 仓库地址

git add --all

git commit -m”xxx”

git push -u origin master

注意:如果出现 ! [rejected]        master -> master (fetch first) 失败提示,说明代码仓库并非空库,则需要先 git pull origin master

2. It will be more convenient to use IDEA's companions, but there is one very important thing that must be paid attention to:

Focus:

A file must be added in the root directory of the project: .gitignore, the content is as follows:

The function is to ignore the .idea folder and all files under it, .iml files, target folder and all files under it, *.log  when submitting and uploading

The first three types of files are the unique configuration generated by the local project that will be automatically generated after the IDEA project is imported and started, so they are not suitable for other developers who use the git repository, so this step is very important, *.log is me Added by yourself, used to ignore log files.

 

After the git init and git remote add origin warehouse addresses , you can see the master branch with the warehouse address in the lower right corner of the idea:

Just click the green submit button, and the following window will pop up:

 

Finally, if you find that the pull or push keeps reporting errors, it must be a problem in a submission link. At this time, we can use the following to perform a commit rollback:

git log #查看commit日志

git reset --hard 版本号(版本号在日志里可以看到)


git add --all #回退之后,再重新刷缓存

git commit -m 'X' #提交,正常都会报出黄色警告

git push -f origin master #不用管警告,强制上传即可

 

Log:

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/111996335