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

相关推荐
布莱克6053 小时前
Swift 类详解:从定义、继承到内存管理的完整指南
服务器·开发语言·ios·ssh·swift·类
是main不是漫4 小时前
【快慢指针 | LeetCode202 快乐数】鸽巢原理佐证环特性,快慢指针巧解快乐数
c语言·开发语言·c++·算法
果果软件4 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源
布莱克6055 小时前
Swift 方法详解:实例方法、类型方法与 mutating 机制
服务器·开发语言·ios·ssh·swift·方法
Risehuxyc7 小时前
PHP能不能在参数里放匿名函数
开发语言·php
码云社区7 小时前
Java家政服务系统源码:人员技能、服务范围与多商家业务落地
java·开发语言·家政服务·家政小程序·家政系统
FRing038 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
摇滚侠8 小时前
《On Java 中文版 基础卷》阅读笔记 接口 18
java·笔记·python
CoderYanger8 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
whitelbwwww9 小时前
CUDA C++实战--中级
开发语言·前端·c++