Java中的Maven项目使依赖和自己写的代码的分开的部署的部署方式

文章目录

优点

随着项目的功能越来越多,如果把所有代码都打包到一个jar里,这样不利于传输。把源码和依赖包分开。这样如果依赖包没有变化的话,再此部署时,就不需要往服务器的lib目录下上传依赖包,只需要把源码的变更过的源码包部署即可。

maven中配置

在 存在启动类的工程里的pom.xml 配置以下内容。之前的<build>标签内的东西注释掉。

bash 复制代码
<!-- 使用下面的build内容在打依赖包和源码内容时可以分开-->
    <build>
        <plugins>
            <!-- 自定义ClassPath路径 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <!-- 把构建根目录的lib文件夹配置成ClassPath路径 -->
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!--配置Jar包构建信息-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.1.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                    <includeSystemScope>true</includeSystemScope>
                    <!--手动设置构建需要加入的依赖,只有配置的依赖才会加入。-->
                    <includes>
                        <!--配置一个不存在的groupId,这样就不会引入任何依赖,jar包就只包含代码文件-->
                        <include>
                            <groupId>not-exists</groupId>
                        </include>
                        <!--如果是多模块项目,其他模块需要手动添加下面-->
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--把依赖拷贝到Lib路径-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!--生成的jar包名称为项目的artifactId-->
        <finalName>${project.artifactId}</finalName>
    </build>

执行maven 打包


项目部署

启动(重启)服务的脚本( restart-customer.sh)

bash 复制代码
#!/bin/bash
# *******************************
# example
#     cd bin/
#     sh debug.sh
# ********************************

kill -9 $(netstat -lnp|grep 8083 | awk '{print $7}' | awk -F"/" '{print $1}')
cd /home/user/app
chmod +755 platform-customer.jar
source /etc/profile
#cd ..
#ulimit -n 524280
cd /home/user/app/logs
JAVA_OPTS=""
nohup java -jar -Djava.library.path=/home/user/app-Dloader.path=./lib /home/user/app/platform-customer.jar -Xms512M -Xmx512M -Xss256k $JAVA_OPTS -Djava.ext.dirs=$JAVA_HOME/jre/lib/ext --server.port=8083 &

查看服务启动状态

#jps

相关推荐
文丑颜不良啊2 小时前
LeetCode2981~3000题目解析
java·leetcode
nice_lcj5204 小时前
【从零理解 Spring AI · 13】RAG 要解决什么:把“闭卷考试“变成“开卷“
java·spring·检索增强·rag·spring ai·ai应用开发
pps-key4 小时前
网络攻防入门:从“黑客梦”到合法实操(生存篇)
开发语言·web安全·php
学长毕设4 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
汤姆宝库5 小时前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
这个昵称不容易5 小时前
SpringBoot 医疗病历交互系统的设计与实现:192 个 Java 文件、256 个接口、22 张表,附真实运行截图
java·springboot·毕设
驭渊的小故事6 小时前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
Sakura_j_h_7 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
微笑星河q7 小时前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
Eider19988 小时前
Python基础语法之并发相关3
开发语言·python