zoukankan      html  css  js  c++  java
  • maven项目打包时将依赖的jar包和项目代码分离

    项目做到后面依赖的jar包比较多,Maven项目打包后打的Jar包会很大,每次部署更新的时候上传都会很慢。

    其实我们只是想更新里面的代码而已,而那众多的依赖包并不想重新上传,这时候我们就需要将依赖包和项目代码分离开来了。

    pom.xml配置修改

    <build>
            <finalName>cus</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <!--代表maven打包时会将外部引入的jar包 打包到项目jar-->
                    <configuration>
                        <includeSystemScope>true</includeSystemScope>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <plugin>
                    <!--打包时去除第三方依赖-->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <layout>ZIP</layout>
                        <includes>
                            <include>
                                <groupId>non-exists</groupId>
                                <artifactId>non-exists</artifactId>
                            </include>
                        </includes>
                    </configuration>
                </plugin>
                <!--拷贝第三方依赖文件到指定目录-->
                <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>
                                <!--target/lib是依赖jar包的输出目录,根据自己喜好配置-->
                                <outputDirectory>target/lib</outputDirectory>
                                <excludeTransitive>false</excludeTransitive>
                                <stripVersion>false</stripVersion>
                                <includeScope>runtime</includeScope>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

    启动命令修改

    java -Dloader.path=lib包的路径 -jar cus.jar

    image-20211115151840688

    修改后,原本cus.jar是170M,现在只有4M。

    本地引用jar问题

    当项目中存在本地引入的jar包时,本地引用的jar无法打包到target/lib/目录下。

    例:

    image-20211115152224501

    解决方案:

    将pom.xml中的<includeScope>runtime</includeScope>注释即可

  • 相关阅读:
    golang的select典型用法
    vscode配置git和提交代码到github教程
    VsCode中好用的git源代码管理插件GitLens
    GoMock框架使用指南
    golang对结构体排序,重写sort
    Go语言开发Prometheus Exporter示例
    golang 字符串拼接性能比较
    golang中的strings.Compare
    各大厂分布式链路跟踪系统架构对比
    NV triton启动方式说明
  • 原文地址:https://www.cnblogs.com/doagain/p/15556590.html
Copyright © 2011-2022 走看看