资料内容:
7.持续集成
项目开发完成后,通过 jenkins 持续集成,打包成 docker 镜像后上传到服务器上的 docker
repository 后运行,所以需要在 pom.xml 文件里配置 maven docker 插件。
Pom.xml 关键配置:
<resources><!-- 使用@@站位符,输出 Dockerfile 至 docker 文件夹 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration><imageName>172.168.4.43:5000/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<pushImage>true</pushImage>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
在 src/main/下建立 docker 目录,并新建 Dockerfile,内容如下:
FROM java:8
VOLUME /tmp
ADD spring-boot-demo-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
CMD java -jar app.jar
当程序经过测试并提交到 gitlab 后,jenkins 将自动构建并打包成 docker 镜像发布到远程
docker 仓库。同时 jenkins 会通知远程服务下载该镜像并重新运行。
8.集成 mybatis
所有数据库操作采用 Mybatis 来管理。集成 Mybatis 关键配置如下:
添加 pom.xm 依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>1.3.0</version>
</dependency>
<!--以 mysql 数据库为例-->
<dependency><groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>