maven 打包记录

流水记录·······························

springboot项目,每次都是打整包上传,很不方便,也不灵活。

把依赖包和项目源码包单独打包,后期只针对项目包进行上传。

pom.xml 配置:

在 plugins 里加入如下配置:

<!-- Copy project dependency -->
      <!-- 将所依赖的第三方jar包copy入lib目录 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <id>copy-dependencies-runtime</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <!-- exclude junit, we need runtime dependency only
              把scope = runtime 的依赖包copy到指定目录
              -->
              <includeScope>runtime</includeScope>
              <outputDirectory>${project.build.directory}/lib/</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>


      <!-- 打包成一个可运行jar文件的插件,如果没有这个,使用 java -jar xx.jar 命令启动时会提示 “没有主清单属性” -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <!-- 这个插件会找有这个public static void main(String[] args)方法的类,当做可执行的类。当出现两个类含有main方法时,会报错。 。
            项目中有可能会在不同的类中定义main方法,所以这里要指定真正启动的那个main方法所在的类 -->
          <mainClass>org.example.App</mainClass>

          <!-- layout这个配置的作用 是为了打包出来的简化jar(只含项目代码)能够加载第三方jar包目录。也就是说
              打包出来的jar里面是没有第三方依赖包的,然后我们在启动项目时可以通过 –Dloader.path=第三方依赖包目录
              这个配置来指定加载第三方依赖包
           -->
          <layout>ZIP</layout>
          <includes>

            <!-- 不包含任何jar包。-->
            <include>
              <groupId>nothing</groupId>
              <artifactId>nothing</artifactId>
            </include>
          </includes>
          <outputDirectory>${project.build.directory}/lib/</outputDirectory>

        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

对于一些额外的jar包,比如第三方平台给的jar包,需要加入到项目中,可以在项目里新建一个目录来存放,然后通过 scope=system 来引入。如 使用c3p0这个包来做示例:

项目中创建other-libs 目录来放额外需要依赖的包。那么通过在pom.xml 里这么引入:

 <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
      <scope>system</scope>
      <systemPath>${basedir}/other-libs/c3p0-0.9.1.2.jar</systemPath>
    </dependency>

 打包时需要把这种scope=system的包一起打包到指定目录,则在 maven-dependency-plugin 插件里加入:

<execution>
            <id>copy-dependencies-system</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <!-- exclude junit, we need system dependency only
                  把scope = system 的依赖包copy到指定目录
               -->
              <includeScope>system</includeScope>
              <outputDirectory>${project.build.directory}/lib/</outputDirectory>
            </configuration>
          </execution>

整体内容如图:

通过 package 打包后,就会看到 第三方依赖包和项目包都在指定目录下了:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值