springboot maven静态文件与lib包分离打包配置

本次打包根据网上找到多种的配置,最终生成我的可以使用的配置方法,可能与在座的各位可能不一致,但是我自身使用没有任何问题,不足之处请谅解,谢谢。

 

环境准备

springboot  2.1.0.RELEASE

jdk 1.8

1.设置pom.xml文件

着重修改<build>中的配置

<build>
	<finalName>collect</finalName>
	<plugins>
		<!-- 跳过单元测试 -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-plugin</artifactId>
			<configuration>
				<skipTests>true</skipTests>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
				<encoding>UTF-8</encoding>
				<compilerArguments>
					<verbose />
					<bootclasspath>${java.home}\lib\rt.jar;${java.home}\lib\jce.jar</bootclasspath>
				</compilerArguments>
			</configuration>
		</plugin>
		<!--**********分离资源文件及依赖包打包配置************* -->
		<!--打包jar -->
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<!--不打包资源文件 -->
				<excludes>
					<exclude>*.**</exclude>
					<exclude>static/**</exclude>
					<exclude>templates/**</exclude>
					<exclude>config/**</exclude>
				</excludes>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<!--MANIFEST.MF 中 Class-Path 加入前缀 -->
						<classpathPrefix>lib/</classpathPrefix>
						<!--jar包不包含唯一版本标识 -->
						<useUniqueVersions>false</useUniqueVersions>
						<!--指定入口类 -->
						<mainClass>com.xxxx.ApplicationRun</mainClass>
					</manifest>
					<manifestEntries>
						<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
						<!--我个人在程序中引入了外部包,需要打包的时候把外部包的信息写入MANIFEST.MF -->
						<!--Class-Path>./resources/</Class-Path> 与下面二选一,我是使用了外部包-->
						<Class-Path>lib/XXXX.jar lib/XXXX.jar ./resources/ </Class-Path>
					</manifestEntries>
				</archive>
				<outputDirectory>${project.build.directory}</outputDirectory>
			</configuration>
		</plugin>
		
		<!--拷贝依赖 copy-dependencies-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <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>

        <!--拷贝资源文件 copy-resources-->
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <excludes>
                                	<!--static与mapper不需要重复拷贝,在这处给排除掉,否则程序会找不到mapper文件-->
                                	<exclude>static/**</exclude>
                                	<exclude>mapper/**</exclude>
                            	</excludes>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/resources</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
	</plugins>

</build>

2.使用maven install打包

检查打好的文件与配置

 

3.在target下查看生成的文件与文件夹

4.将选中的三个文件与文件夹上传到部署服务器上

5.执行命令,使程序生效

java -Dloader.path=resources,lib -jar   XXXX.jar

即可

 

附录

参考网上其他人,按照自己需要编写了一个启停的shell脚本,如果用到可以直接使用

server.sh

#!/bin/bash
app='XXXX.jar'
args='-Dloader.path=resources,lib'
cmd=$1

pid=`ps -ef|grep java|grep $app|awk '{print $2}'`

startup(){
  nohup java -jar $args $app &
  tail -f nohup.out
}

if [ ! $cmd ]; then
  echo "Please specify args 'start|restart|stop'"
  exit
fi

if [ $cmd == 'start' ]; then
  if [ ! $pid ]; then
    startup
  else
    echo "$app is running! pid=$pid"
  fi
fi

if [ $cmd == 'restart' ]; then
  if [ $pid ]
    then
      echo "$pid will be killed after 3 seconds!"
      sleep 3
      kill -9 $pid
  fi
  startup
fi

if [ $cmd == 'stop' ]; then
  if [ $pid ]; then
    echo "$pid will be killed after 3 seconds!"
    sleep 3
    kill -9 $pid
  fi
  echo "$app is stopped"
fi

 

如果有帮助就点歌赞呗,谢谢啦。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值