补习系列-springboot-使用assembly进行项目打包

本文介绍如何使用Spring Boot Maven插件进行项目打包,包括生成独立可执行的Jar文件及完整构建项目的步骤。还介绍了如何使用maven-assembly-plugin来创建包含配置文件、启停脚本等在内的发布包。

springboot-maven插件

springboot-maven插件

repackage目标声明


Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: compile+runtime.
Since version: 1.1.
Binds by default to the lifecycle phase: package.

1. 项目打包Jar

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

如此,执行mvn package可自动生成一个独立可执行的jar文件

2. 项目完整构建

通常,项目发布时除了jar包,还会包含配置文件、启停脚本等,此时需要借助assembly插件完成重复打包
构建结构

base
    - bin
        - start.sh
        - stop.sh
    - application.properties
    - log4j.properties
    - app-0.0.1-SNAPSHOT.jar

pom定义

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>bundle</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>${basedir}/src/main/build/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

说明
将assembly定义在spring-boot:repackage之后,这样maven在执行package阶段时会按照声明顺序处理;
assembly.xml存放于src/main/build目录,此外用于发布的config、bin目录及文件都放到这个目录;

assembly定义

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bundle</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory> <!-- disable the creation of root's distribution dir in the archive -->
    
     <fileSets>  
        <!-- config files -->
        <fileSet>  
            <directory>${basedir}/src/main/build/config</directory>  
            <excludes></excludes>  
             <includes>
                <include>application*.properties</include>
                <include>log4j.properties</include> 
            </includes>
            <fileMode>0644</fileMode>
            <outputDirectory>/</outputDirectory>  
        </fileSet>  
        <!-- scripts -->
        <fileSet>
            <directory>${basedir}/src/main/build/bin</directory>
            <includes>
                <include>*.sh</include>
            </includes>
            <fileMode>0755</fileMode>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <!-- executable jar -->
         <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.artifactId}-${project.version}.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>  
    
</assembly>

关于内置变量

3. 本地包依赖

  • 定义scope=system依赖
<dependency>
   <groupId>com.xxx.component</groupId>
   <artifactId>mongoop</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <scope>system</scope>
   <systemPath>D:\work\maven\repo\m2\xxx.jar</systemPath>
  </dependency>
  • 声明springboot打包时包含system范围的依赖
<build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
     <includeSystemScope>true</includeSystemScope>
    </configuration>
    <executions>
     <execution>
      <goals>
       <goal>repackage</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>

参考文档

springboot文档-maven插件使用
关于springboot-repackage
maven内置变量

转载于:https://www.cnblogs.com/littleatp/p/9278517.html

### 使用 Spring Boot 开发补习班系统的相关技术和步骤 开发一个基于 Spring Boot 的补修班系统需要综合考虑系统需求、技术栈选择以及具体的实现细节。以下是一些关键点和实现建议: #### 1. 系统架构设计 Spring Boot 提供了一个轻量级的框架,能够快速搭建应用程序。在开发补习班系统时,可以采用分层架构,包括前端(如 Vue.js 或 React)、后端(Spring Boot)以及数据库(如 MySQL 或 MongoDB)。这种架构可以确保系统的可维护性和扩展性[^1]。 #### 2. 技术栈选择 - **后端**:使用 Spring Boot 作为主要框架,结合 Spring Data JPA 进行数据库操作。 - **前端**:可以选择 Vue.js 或 React 来构建用户界面,与后端通过 RESTful API 进行交互。 - **数据库**:推荐使用关系型数据库 MySQL 或非关系型数据库 MongoDB,具体取决于数据模型的设计。 - **其他工具**:可以集成 Redis 用于缓存,集成 RabbitMQ 或 Kafka 用于消息队列处理。 #### 3. 核心功能模块 根据引用[3]的内容,补习班系统的核心功能可能包括: - 用户管理:学生、教师和管理员的注册、登录及权限控制。 - 课程管理:课程的创建、编辑、删除及查询。 - 学习记录:记录学生的课程学习进度及成绩。 - 在线交流:提供师生之间的即时沟通功能。 #### 4. 示例代码 以下是一个简单的 Spring Boot 控制器示例,用于处理课程相关的 CRUD 操作: ```java @RestController @RequestMapping("/api/courses") public class CourseController { @Autowired private CourseService courseService; // 获取所有课程 @GetMapping public List<Course> getAllCourses() { return courseService.getAllCourses(); } // 根据ID获取课程 @GetMapping("/{id}") public Course getCourseById(@PathVariable Long id) { return courseService.getCourseById(id); } // 创建新课程 @PostMapping public Course createCourse(@RequestBody Course course) { return courseService.createCourse(course); } // 更新课程 @PutMapping("/{id}") public Course updateCourse(@PathVariable Long id, @RequestBody Course updatedCourse) { return courseService.updateCourse(id, updatedCourse); } // 删除课程 @DeleteMapping("/{id}") public void deleteCourse(@PathVariable Long id) { courseService.deleteCourse(id); } } ``` #### 5. 数据库设计 数据库设计是系统开发的重要环节。以下是一个简单的课程表设计示例: ```sql CREATE TABLE courses ( id BIGINT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, description TEXT, teacher_id BIGINT NOT NULL, price DECIMAL(10, 2), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); ``` #### 6. 安全性考虑 在开发过程中,安全性是一个不可忽视的问题。可以通过 Spring Security 实现用户认证和授权,确保系统的安全性和稳定性[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值