在本地进行:
这种打包方式只需要jar包就可以了,添加maven打包插件,能打包成jar包的插件很多,这里简单介绍几个。
spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
maven-compiler-plugin
编译Java源码,一般只需设置编译的jdk版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
除了在这里设置还可以在 properties设置jdk版本,例如:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
maven-dependency-plugin
用于复制依赖的jar包到指定的文件夹里
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
maven-jar-plugin
打成jar时,设定manifest的参数,比如指定运行的Main class,还有依赖的jar包,加入classpath中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>/data/lib</classpathPrefix>
<mainClass>com.zhang.spring.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
maven-antrun-plugin
在maven中运行Ant任务,比如在打包阶段,对文件进行复制
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target name="copy">
<delete>
<fileset dir="target" includes="*.properties"></fileset>
</delete>
<copy todir="target">
<fileset dir="files"></fileset>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
wagon-maven-plugin
用于一键部署,把本地打包的jar文件,上传到远程服务器上,并执行服务器上的shell命令
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<serverId>crawler</serverId>
<fromDir>target</fromDir>
<includes>*.jar,*.properties,*.sh</includes>
<url>sftp://59.110.162.178/home/zhangxianhe</url>
<commands>
<command>chmod 755 /home/zhangxianhe/update.sh</command>
<command>/home/zhangxianhe/update.sh</command>
</commands>
<displayCommandOutputs>true</displayCommandOutputs>
</configuration>
</plugin>
我们使用跳过测试的打包命令
mvn clean install -Dmaven.test.skip=true
无论用哪种打包方式我们需要一个jar包就行了
将jar包上传到linux的一个文件夹下
我这里上传了三个,主要就是编写dockerfile文件,不需要任何扩展名哈,文件内容如下
FROM java:8
# 把jar包添加到镜像中,hxzn-fast.jar就是你上传的jar包名称
ADD hxzn-fast.jar /app.jar
# 镜像暴露的端口
EXPOSE 8080
RUN bash -c 'touch /app.jar'
# 容器启动命令
ENTRYPOINT ["java","-jar","/app.jar"]
# 设置时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
以上是举个例子,首先注意两点,别忘了暴露端口号,dockerfile和你上传的jar包在同一目录下。运行命令
docker build -t hxzn-fast:v1.0 .
注意别忘了后面的 . 这个.就是代表着在当前目录查找dockerfile文件,如果没有 . 那你就需要指定dockerfile所在的文件路径也行,不然会报错文件路径找不到,成功后即可用docker images查看打包的镜像。
之后运行镜像
docker run -d -p 8080:8080 3a06e920d5bf #镜像id
成功后查看镜像运行状态
docker ps
docker logs -f 211befd8cbcf #容器id
展示一下成功和报错的
成功的
报错的
这里报错就是java的报错了,nacos地址不对,你检查一下自己的jar包,重新上传再打包成镜像就行了,这样就可以直接按照虚拟机项目的路径访问资源了。当然这种打包还是太繁琐了,使用k8s+kubesphere的devops进行自动集成,自动部署,是目前主流的打包方式之一。