加载中...
Featured image of post 4 springboot项目

4 springboot项目

maven的使用,springboot项目

一、模块划分

一个父工程,一个子工程微服务,一个普通maven子工程

二、建父工程

1、github建仓库

2、idea新建maven项目(父工程)

3、idea工程和github建立联系

  • 选中 scr和pom.xml ,git add

  • git commit

  • git fetch

  • 命令行输入:

1
2
git branch --set-upstream-to=origin/master
git merge origin/master --allow-unrelated-histories
Copied!
  • commit + push 推送远程仓库
  • 接下来就可以直接开发了

三、建子工程

  • 另一个子工程以同样方式创建

四、父子关系

三个工程的pom文件内容是这样的:

  • springboot-parent-child
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?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>org.example</groupId>
    <artifactId>springboot-parent-child</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>user-service</module>
        <module>base-entity</module>
    </modules>

    <properties>
      	<!-- maven-resources-plugin插件的默认取值 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- maven-site-plugin等插件的默认取值 -->
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      
      	<!-- maven-compiler-plugin插件的默认取值 -->
      	<maven.compiler.source>1.8</maven.compiler.source>
    		<maven.compiler.target>1.8</maven.compiler.target>
      
      	<!-- spring-boot-starter-parent里定义的的默认取值 -->
        <java.version>1.8</java.version>
      
        <spring-boot-version>2.6.3</spring-boot-version>
    </properties>
<!-- 
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.6.3</version>
</parent>
-->
    <dependencyManagement>
        <dependencies>
            <!-- SpringBoot 依赖导入 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringCloud 依赖导入 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringCloud Alibaba 依赖导入 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.1.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  	<!-- 
    1、这里显式指定插件版本,其实版本在springboot的pom中有,但这种引入方式会对插件不友好
 		2、maven打包时会出现找不到版本的警告,为了规避,所以在这里指定版本,
						后面继承的子工程就可以直接依赖这个插件了
		3、用spring-boot-starter-parent引入的,插件可以直接使用,没有警告			
		-->
    <!-- build 标签:用来配置对构建过程的定制 -->
    <build>
        <pluginManagement>
            <!-- plugins 标签:定制化构建过程中所使用到的插件 -->
            <plugins>
                <!-- plugin 标签:一个具体插件 -->
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${spring-boot-version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
Copied!
  • user-service
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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">
    <parent>
        <artifactId>springboot-parent-child</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>user-service</artifactId>

    <dependencies>
        <!-- Nacos 服务注册发现启动器 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- web启动器依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>base-entity</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <!-- build 标签:用来配置对构建过程的定制 -->
    <build>
        <!-- plugins 标签:定制化构建过程中所使用到的插件 -->
        <plugins>
            <!-- plugin 标签:一个具体插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
              <!-- 
							1、对于用import引入的springboot,这里要指定这个目标,否则执行mvn package时,不会自动执行springboot:repackage
							2、如果不这样指定的话,那打包命令需要显式指定插件,命令为:
							mvn clean package spring-boot:repackage -Dmaven.test.skip=true
							3、repackage前面必须有已经存在的包,否则会出错,所以前面跟着一个package原生命令
 							-->
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
Copied!
  • base-entity
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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">
    <parent>
        <artifactId>springboot-parent-child</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>base-entity</artifactId>

    <dependencies>
        <!-- OpenFeign 专用依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>
Copied!
  • 部署
1
nohup java -jar user-service-1.0-SNAPSHOT.jar>demo06.log 2>&1 &
Copied!

注意

1、引入springboot的方式

  • 不是每个人都喜欢从 spring-boot-starter-parent 继承 POM。您可能需要使用自己公司标准的父 POM,或者您可能只是希望明确地声明所有 Maven 配置。

  • 如果您不想使用 spring-boot-starter-parent,则仍然可以通过使用 scope=import 依赖来获得依赖管理(但不是插件管理)的好处:

  • 假如 A工程这样配置,这种方式只能在后面继承这个A工程的的子工程中去覆盖依赖的版本:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<packaging>pom</packaging>
...
...
<dependencyManagement>
        <dependencies>
            <!-- SpringBoot 依赖导入 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.3</version>
                <type>pom</type>
                <scope>import</scope>
           </dependency>
        </dependencies>
</dependencyManagement>
Copied!
  • 如果这样,是覆盖不了的:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<packaging>pom</packaging>
...
...
<dependencyManagement>
        <dependencies>
            <!-- SpringBoot 依赖导入 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.3</version>
                <type>pom</type>
                <scope>import</scope>
           </dependency>
        </dependencies>
</dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.4</version>
        </dependency>
</dependencies>
Copied!
  • 但是这样,可以覆盖某个依赖的版本:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<packaging>pom</packaging>
...
...
<dependencyManagement>
        <dependencies>
            <!-- SpringBoot 依赖导入 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.6.3</version>
                <type>pom</type>
                <scope>import</scope>
           </dependency>
          
          <!-- 覆盖web的版本 -->
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.6.4</version>
          </dependency>
          
        </dependencies>
</dependencyManagement>
Copied!
  • 这种方式引入,也会使spring-boot-maven-plugin 引入进来,但是打包时有warning

Licensed under CC BY-NC-SA 4.0
最后更新于 2024年5月10日 01:57
发表了90篇文章 · 总计613.28k字
本站总访问量390本站访客数353人次

目录