zoukankan      html  css  js  c++  java
  • maven项目打包时排除依赖包

    1.背景

    为了快速上传jar包到服务器,很多时候我们需要把依赖包单独独立出来,避免每次修改都传依赖包

    2.实现方式

    maven的pom文件,没有独立依赖包时配置如下:

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <!-- 指定该Main Class为全局的唯一入口 -->
                        <mainClass>com.ldp.XXXXApplication</mainClass>
                        <layout>ZIP</layout>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    View Code

    maven的pom文件,将依赖包与自己写的代码分离:

     <build>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <!--不打包资源文件-->
                        <excludes>
                            <exclude>*.**</exclude>
                            <exclude>*/*.xml</exclude>
                        </excludes>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <!--MANIFEST.MF 中 Class-Path 加入前缀-->
                                <classpathPrefix>lib/</classpathPrefix>
                                <!--jar包不包含唯一版本标识-->
                                <useUniqueVersions>false</useUniqueVersions>
                                <!--指定入口类-->
                                <mainClass>com.ldp.XXXXXXApplication</mainClass>
                            </manifest>
                            <manifestEntries>
                                <!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
                                <Class-Path>./resources/</Class-Path>
                            </manifestEntries>
                        </archive>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
                </plugin>
    
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <classifier>exec</classifier>
                    </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>
                                <outputDirectory>
                                    ${project.build.directory}/lib/
                                </outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <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>
                                    </resource>
                                </resources>
                                <outputDirectory>${project.build.directory}/resources</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
                        <includes>
                            <include>
                                <groupId>null</groupId>
                                <artifactId>null</artifactId>
                            </include>
                        </includes>
                        <layout>ZIP</layout>
                        <!--使用外部配置文件,jar包里没有资源文件-->
                        <addResources>true</addResources>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <configuration>
                                <!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
                                <!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
                                <!--<classifier>run</classifier>-->
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    View Code

    打包后的文件:

    完美!

  • 相关阅读:
    eclipse 注释模板
    解决win7访问不了局域网共享文件
    java 执行command
    解决Unable to reach a settlement: [diffie-hellman-group1-sha1, diffie-hellman-group-exchange-sha1] and [curve25519-sha256@li
    解决java.lang.UnsupportedClassVersionError
    hadoop命令备忘
    intellij 提交代码到git
    java用代理访问
    解决 jersey javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
    解决Unable to locate Kerberos realm
  • 原文地址:https://www.cnblogs.com/newAndHui/p/14415297.html
Copyright © 2011-2022 走看看