Locally:
This packaging method only needs jar packages. Add maven packaging plug-ins. There are many plug-ins that can be packaged into jar packages. Here are a few.
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
To compile Java source code, you generally only need to set the compiled jdk version
<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>
In addition to setting here, you can also properties to set the jdk version, for example:
<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
It is used to copy the dependent jar package to the specified folder
<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
When creating a jar, set the parameters of the manifest, such as specifying the Main class to run and the dependent jar package, and add them to the 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
Run Ant tasks in maven, such as copying files during the packaging phase
<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
It is used for one click deployment, upload the locally packaged jar file to the remote server, and execute the shell command on the server
<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>
We use the package command to skip the test
mvn clean install -Dmaven.test.skip=true
Either way, we need a jar package
Upload the jar package to a linux folder
I uploaded three here, mainly to write a dockerfile file without any extension. The contents of the file are as follows
FROM java:8 # Add the jar package to the image. hxzn-fast.jar is the name of the jar package you uploaded ADD hxzn-fast.jar /app.jar # Mirror exposed port EXPOSE 8080 RUN bash -c 'touch /app.jar' # Container start command ENTRYPOINT ["java","-jar","/app.jar"] # Set time zone RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
The above is an example. First, pay attention to two points. Don't forget to expose the port number. The dockerfile is in the same directory as the jar package you uploaded. Run command
docker build -t hxzn-fast:v1.0 .
Don't forget the following. This means to find the dockerfile file in the current directory. If not, you need to specify the file path where the dockerfile is located, otherwise an error will be reported. If the file path cannot be found, you can use docker images to view the packaged image after success.
Then run the mirror
docker run -d -p 8080:8080 3a06e920d5bf #Mirror id
View the operation status of the mirror after success
docker ps
docker logs -f 211befd8cbcf #Container id
Show the of success and error reporting
successful
Error reporting
The error reported here is the error reported by java. The nacos address is wrong. You can check your jar package, upload it again, and then package it into an image
However, this method is not the best in the actual deployment. In fact, the module of a distributed project here is only tens of megabytes, with a maximum of 140m. It is packaged into an image, but it is several times larger
There is a big gap in the size here. The common jar package packages the dependency maven. When forming an image, the dependency is very large. In fact, the enterprise deployment is still the CI/CD deployment of k8s+kubeshpere. After it is built, the deployment will be particularly simple. Here is a simple docker image deployment.