程序员

systemScope和动态打包

2020-06-09  本文已影响0人  炎許諾圉福

1.systemScope的使用

利用systemScope可以让maven项目依赖本地jar包

    <!--钉钉jar-->
    <dependency>
        <groupId>com.taobao.top</groupId>
        <artifactId>top-api-sdk-java</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/../lib/taobao-sdk-java-auto_1479188381469-20200201.jar</systemPath>
    </dependency>
</dependencies>

2.使用了systemScope后的项目打包

Spring Boot项目用systemScope方式引用了jar包后,若需要支持打包后可以用内置tomcat/外置tomcat运行,需要利用maven plugin对打包过程进行处理,打包时将本地jar包一并打包。

2.1 war包

如果项目打包成war包,目录结构如图2-1,lib下的jar包无论内置tomcat/外置tomcat启动时,都会去加载;lib-provided下的包仅内置tomcat会去加载(被视为内置tomcat的自身提供的依赖包)

图2-1 war包目录结构
    <build>
        <plugins>
            <!-- 打包成war -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <!-- 忽略web.xml-->
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <warName>${artifactId}</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <!-- 将外部依赖一并打包,若是打包war则打包到lib-provided -->
                    <!-- <includeSystemScope>true</includeSystemScope> -->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>compile</phase>
                       <!-- 编译阶段复制外部jar到lib -->
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib
                            </outputDirectory>
                            <includeScope>system</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>${artifactId}</finalName>
    
    </build>

2.2 jar包

spring boot项目依赖外部jar要打包成jar包,只需要以下配置

<plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.1.1.RELEASE</version>
        <configuration>
            <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
            <includeSystemScope>true</includeSystemScope>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>           

打包后目录结构如图2-2:


v2-2 打包成jar包的效果

3.Spring Boot项目动态打包

build节点新增,如下代码:

        <resources>
            <resource>
                <directory>src/main/resources/</directory>
                <excludes>
                    <exclude>application.yml</exclude>
                </excludes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources/</directory>
                <includes>
                    <include>application.yml</include>
                </includes>
                <!-- 用参数化值替换文件文本内容(如@profileActive@)   -->
                <filtering>true</filtering>
            </resource>
        </resources>

然后pom.xml文件继续添加以下配置

<profiles>
        <profile>
            <!-- 本地开发环境打包: mvn clean package -P dev -->
            <id>dev</id>
            <properties>
                <profileActive>druid,dev</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境打包:  mvn clean package -P test -->
            <id>test</id>
            <properties>
                <profileActive>druid,test</profileActive>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境打包:  mvn clean package -P prod -->
            <id>prod</id>
            <properties>
                <profileActive>druid,prod</profileActive>
            </properties>
        </profile>
        <profile>
            <!-- 预发布环境打包:  mvn clean package -P pre -->
            <id>pre</id>
            <properties>
                <profileActive>druid,prod,pre</profileActive>
            </properties>
        </profile>
    </profiles>

application.yml文件中配置

spring:
    profiles:
        active:  @profileActive@

@profileActive@打包时,会被动态替换成profileActive节点的值。

图3-1 spring boot配置文件

参考:Spring boot 项目分环境 Maven 打包

上一篇下一篇

猜你喜欢

热点阅读