项目结构
image.png
1.pom.xml配置

<build>
        <resources>
            <resource>
                <!-- 构建期间需要放到 WEB-INF/classes/ 下的文件 -->
                <directory>src/main/resources</directory>
                <!-- 是否要经过filer文件解析${}参数配置,设置false不会替换profileActive值 -->
                <filtering>true</filtering>
            </resource>
        </resources>

        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                    <nonFilteredFileExtensions>
                        <!--keystore证书不编码-->
                        <nonFilteredFileExtension>keystore</nonFilteredFileExtension>
                        <nonFilteredFileExtension>exe</nonFilteredFileExtension>
                        <nonFilteredFileExtension>bat</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <includes>
                        <include>com/**</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <!-- jar 等压缩文件在被打包进入 zip、tar.gz 时是否压缩,设置为 false 可加快打包速度 -->
                            <recompressZippedFiles>false</recompressZippedFiles>
                            <!-- 打包生成的文件是否要追加 release.xml 中定义的 id 值 -->
                            <appendAssemblyId>true</appendAssemblyId>
                            <!-- 指向打包描述文件 package.xml -->
                            <descriptors>
                                <descriptor>src/main/assembly/package.xml</descriptor>
                            </descriptors>
                            <!-- 打包结果输出的基础目录 -->
                            <outputDirectory>${project.build.directory}/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2.assembly package.xml配置

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!-- assembly 打包配置更多配置可参考官司方文档: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html -->
    <id>release</id>
    <!-- 设置打包格式,可同时设置多种格式,常用格式有:dir、zip、tar、tar.gz dir 格式便于在本地测试打包结果 zip 格式便于
      windows 系统下解压运行 tar、tar.gz 格式便于 linux 系统下解压运行 -->
    <formats>
        <format>tar.gz</format>
    </formats>


    <!-- 打 zip 设置为 true 时,会在 zip 包中生成一个根目录,打 dir 时设置为 false 少层目录 -->
    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <!--vss-web start-->
        <!-- 排除静态文件,复制其他文件 -->
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <excludes>
                <exclude>${artifactId}</exclude>
                <exclude>${artifactId}/**</exclude>
                <exclude>static/**</exclude>
                <exclude>sound/**</exclude>
                <exclude>images/**</exclude>
                <exclude>*.yml</exclude>
            </excludes>
        </fileSet>

        <!--从target/classes目录下拷贝application.yml配置文件,因为这个目录下的配置替换了@profileActive@值-->
        <fileSet>
            <directory>target/classes</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>application.yml</include>
                <include>*-${profileActive}.yml</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>application-*-${profileActive}.yml</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>static/**</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>images/**</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>sound/**</include>
            </includes>
        </fileSet>
        <!--vss-web end-->

        <fileSet>
            <directory>../vss-db/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>application-mysql.yml</include>
                <include>application-*-${profileActive}.yml</include>
                <include>db/**</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>../vss-service/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
            <includes>
                <include>application-*-${profileActive}.yml</include>
            </includes>
        </fileSet>


        <!--复制静态文件 -->
        <fileSet>
            <directory>${basedir}/src/main/resources/${artifactId}</directory>
            <outputDirectory>static/${artifactId}</outputDirectory>
        </fileSet>

        <fileSet>
            <directory>${basedir}/src/main/bin</directory>
            <lineEnding>keep</lineEnding>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.bat</include>
                <include>*.exe</include>
                <include>*.xml</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${basedir}/lib</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>

    <!-- 依赖的 jar 包 copy 到 lib 目录下 -->
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
</assembly>

3.启动脚本(windows)
重点关注 -cp参数,这里要指定配置文件、资源文件和依赖库的目录

@echo off
set CURRENT_PATH=%~dp0

:: GET VSS_HOME
set VSS_HOME=
:begin
for /f "tokens=1,* delims=\" %%i in ("%CURRENT_PATH%") do (set content=%%i&&set CURRENT_PATH=%%j)
if "%VSS_HOME%%content%\" == "%~dp0" goto end
set VSS_HOME=%VSS_HOME%%content%\
goto begin
:end

java -XX:+UseG1GC -Xms1024m -Xmx1024m -Xss2m -XX:+IgnoreUnrecognizedVMOptions -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%VSS_HOME%logs/java_heapdump.hprof -Xverify:none -cp %VSS_HOME%lib\*;%VSS_HOME%config;%VSS_HOME%static;%VSS_HOME%images; com.howentech.vss.web.WebApplication

4.application.yml配置
重点关注追加配置

  spring:
    web:
      # 资源配置
      resources:
        # 静态文件地址,保留官方内容后,进行追加
        static-locations:
        # spring默认配置
        - "classpath:/static"
        - "classpath:/public"
        - "classpath:/resources"
        - "classpath:/META-INF/resources"
        # 开发阶段指定的资源目录,生产环境可以不要这个配置
        - "classpath:/images"
        - "classpath:/sound"
        # 追加配置
        - "classpath:../images"
        - "classpath:../static"
        - "classpath:../sound"
最后修改:2022 年 09 月 04 日
如果觉得我的文章对你有用,请随意赞赏