Skip to main content

Maven对Springboot项目配置文件、依赖分离打包

moremind...About 2 minMavenMaven

Maven对Springboot项目配置文件、依赖分离打包

1.使用maven-assembly-plugin进行配置分离

assembly.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- 可自定义,这里指定的是项目环境 -->
    <!-- xxx.tar.gz  -->
    <id>${name}</id>

    <!-- 打包的类型,如果有N个,将会打N个类型的包 -->
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <!-- 配置文件打包-打包至config目录下 -->
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>config</outputDirectory>
            <fileMode>0644</fileMode>
            <includes>
                <include>application.yml</include>
                <include>*.xml</include>
                <include>*.properties</include>
            </includes>
        </fileSet>
        <!-- 启动文件目录 -->
        <fileSet>
            <directory>${basedir}/src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>**.sh</include>
                <include>**.bat</include>
            </includes>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <!-- 依赖库 -->
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
            <fileMode>0755</fileMode>
            <excludes>
                <exclude>${project.groupId}:${project.artifactId}</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>boot</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>${project.groupId}:${project.artifactId}</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

2.pom.xml文件build节点的配置

pom.xml配置文件

    <build>
        <!-- 打包后的启动jar名称 -->
        <finalName>app-name</finalName>
        <plugins>
            <!-- 用于排除jar中依赖包 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layout>ZIP</layout>
                    <includes>
                        <!-- 项目启动jar包中排除依赖包 -->
                        <include>
                            <groupId>non-exists</groupId>
                            <artifactId>non-exists</artifactId>
                        </include>
                    </includes>
                </configuration>
            </plugin>

            <!-- 将依赖cp到lib目录下 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!--依赖输出目录-->
                            <outputDirectory>target/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <includeScope>compile</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- maven编译 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- 不同版本需要制定具体的版本进行编译 -->
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <!-- 打包时跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

            <!-- 将项目中代码文件打成jar包 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <excludes>
                        <!-- 打包后的jar包中不包括配置文件 -->
                        <!-- 通常是指classpath下目录下的文件,这样可以避免编写时的找不到相应文件 -->
                        <exclude>*.xml</exclude>
                        <exclude>*.properties</exclude>
                        <exclude>*.yml</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <!-- 项目启动类 -->
                            <mainClass>cn.moremind.app.SpringBootApplication</mainClass>
                            <!-- 依赖的jar的目录前缀 -->
                            <classpathPrefix>../lib/</classpathPrefix>
                            <addClasspath>true</addClasspath>
                        </manifest>
                        <!-- 将config目录加入classpath目录 -->
                        <manifestEntries>
                            <Class-Path>../config/</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

            <!-- 打包插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

3.编写shell脚本或者bat脚本

1.直接运行 java -jar xxxx.jar即可

2.编写shell/bash脚本

在bin目录下编写脚本文件如下:

shell

#! /bin/sh

HOME = '/opt/xxx/boot'
JAR_HOME = 'xxx.jar'

cd $HOME
nohup java -jar $JAR_HOME

bash

@echo off
rem ======================================================================
rem windows startup script
rem
rem ======================================================================
rem startup jar

java -jar ../boot/xxx.jar

pause

4.项目目录如下

1565327770486
1565327770486
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8