栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot能做主包和依赖包分离的工作么?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring boot能做主包和依赖包分离的工作么?

任务:学习spring boot能做主包和依赖包分离

参考资料:

https://blog.csdn.net/shaofengzong/article/details/121191018
http://t.zoukankan.com/yuboon-p-11919329.html

需求背景:SpringBoot打包后,可以直接运行,但维护部署不方便,体积庞大,不利于传输

做法:将依赖库与项目的class进行分离打包(一定保证 执行命令 的目录 与项目jar、lib、config都在同一目录下)

1、数据库操作 1.1 建立单表
create database tasks;
use tasks;
drop table if EXISTS user;
create table user(
 userId  BigInt unsigned not null primary key auto_increment, 
 username varchar(100),
 userage int
);
1.2 建立数据库权限用户
create user george@localhost IDENTIFIED by '123456';
use tasks;
grant all PRIVILEGES on tasks.*  to george@localhost;
show GRANTs for george@localhost;
2、建立springboot项目 2.1 搭建基本的springboot框架项目(只有一个user表)
# 应用名称
spring.application.name=Demo
# 应用服务 WEB 访问端口
server.port=8080
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/tasks?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
# 数据库用户名&密码:
spring.datasource.username=george
spring.datasource.password=123456

项目架构:

3、打包 3.1 常规方法:


    4.0.0
    com.example
    Demo
    0.0.1-SNAPSHOT
    Demo
    Demo

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
    

    
        
        
            org.springframework.boot
            spring-boot-devtools
            true 
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            org.mybatis
            mybatis
            3.5.7
        

        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        

        
        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            com.baomidou
            mybatis-plus-generator
            3.5.1
        

        
            org.apache.velocity
            velocity
            1.7
        


    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.3.7.RELEASE
                
                    com.example.demo.DemoApplication
                
                
                    
                        repackage
                        
                            repackage
                        
                    
                
            
        
    


mvn clean package -Dmaven.test.skip=true

结果:

3.2 主包和依赖包分离 3.2.1 修改pom.xml文件,实现目的


    4.0.0
    com.example
    Demo
    0.0.1-SNAPSHOT
    Demo
    Demo

    
        1.8
        UTF-8
        UTF-8
        2.3.7.RELEASE
        
        
        lib/
        
        bin/
        
        config/
        F:IntellijIdea_workplacespringbootValidatetarget
    

    
        
        
            org.springframework.boot
            spring-boot-devtools
            true 
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            org.mybatis
            mybatis
            3.5.7
        

        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        

        
        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        

        
        
            org.projectlombok
            lombok
            true
        

        
        
            com.baomidou
            mybatis-plus-generator
            3.5.1
        

        
            org.apache.velocity
            velocity
            1.7
        


    

    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    1.8
                    1.8
                    UTF-8
                
            

            

















            
            
            
                org.apache.maven.plugins
                maven-jar-plugin
                2.4
                
                    
                    ${env.LEARN_HOME}/${output.jar.file.path}
                    
                    
                        *.properties
                        *.yml
                        *.xml
                        *.txt
                    
                    
                        
                        true
                        
                        
                            
                            false
                            
                            true
                            
                            
                            
                            ../${output.dependence.file.path}
                            
                            com.example.demo.DemoApplication
                        
                        
                            
                            
                            
                            ../${output.resource.file.path}
                        
                    
                
            

            
            
                org.apache.maven.plugins
                maven-dependency-plugin
                
                    
                        copy-dependencies
                        package
                        
                            copy-dependencies
                        
                        
                            
                            ${env.LEARN_HOME}/${output.dependence.file.path}
                            
                            false
                            
                            false
                            true
                            true
                            true
                        
                    
                
            
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                2.6
                
                    
                    
                        copy-resources
                        package
                        
                            copy-resources
                        
                        
                            
                                
                                    src/main/resources
                                    
                                        
                                        *.properties
                                        *.yml
                                        *.xml
                                        *.txt
                                    
                                
                            
                            
                            ${env.LEARN_HOME}/${output.resource.file.path}
                        
                    
                
            
        
    


mvn clean package -Dmaven.test.skip=true

结果:



备注:bin/ ,config/,lib/, 文件夹需要同时存在

3.2.2 修改数据库用户密码和config内配置文件
  • 数据库改密码
ALTER USER 'george'@'localhost' IDENTIFIED BY '123' PASSWORD EXPIRE NEVER;
ALTER USER 'george'@'localhost' IDENTIFIED BY '123';
  • 配置文件
# 应用名称
spring.application.name=Demo
# 应用服务 WEB 访问端口
server.port=8080
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/tasks?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
# 数据库用户名&密码:
spring.datasource.username=george
# 修改密码,仍然可以!!!
spring.datasource.password=123
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938950.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号