IntelliJ IDEA自身以及maven项目打包方式(单模块)

您所在的位置:网站首页 maven打包项目 IntelliJ IDEA自身以及maven项目打包方式(单模块)

IntelliJ IDEA自身以及maven项目打包方式(单模块)

#IntelliJ IDEA自身以及maven项目打包方式(单模块)| 来源: 网络整理| 查看: 265

1. Idea自身打包方式 1.1 创建Artifacts

  快捷键(Ctrl+Alt+Shift+S)打开项目的Project Structure。在Artifacts创建

  

   接着,指定main class,如下:

  

   最后,得到创建得到的artifacts

   

  注:

     这里可以通过“+”或者“-”选择那些需要打入artifacts的第三方jar文件,如图:

  

1.2 打包Artifacts

  在菜单栏目选Build,最后一栏Build Artifacts

  

  最后,在出现的窗口创建

  

   得到打包结果:

  

2. maven打包方式(单模块)

  maven打包的步骤:

先mvn clean,再 package即可 如需把项目编译成jar包,打进maven仓库里面,可以运行install命令

  

2.1 maven-jar-plugins 无依赖第三方jar的打包

   maven 默认打包插件,用来创建 project jar

   pom.xml配置如下:

${project.build.directory}/classes src/main/resources true **/*.xml **/*.conf net.alchim31.maven scala-maven-plugin 3.2.1 scala-compile-first process-resources add-source compile org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 UTF-8 compile compile org.apache.maven.plugins maven-jar-plugin 2.4 true lib/ com.swordfall.restserver.base.WebServer

  运行:在target中找到打包出来的xxx.jar包,运行java -jar xxx.jar即可,但是如果程序有依赖其他包,比如程序依赖jdbc去查询db,这时候再执行就会出现找不到jdbc依赖,因为我们并没有将依赖包打进去。

2.2 maven-assembly-plugins 解决依赖第三方jar包,并可执行jar的打包

  支持定制化打包方式,更多是对项目目录的重新组装。以下方式用的比较少,因为我们依赖的jar,也会打进到我们最终生成的jar中,会导致一个jar文件过大,而且如果要给第三方使用,最好给一个纯净的。

  pom.xml配置如下:

${project.build.directory}/classes src/main/resources true **/*.xml **/*.conf net.alchim31.maven scala-maven-plugin 3.2.1 scala-compile-first process-resources add-source compile org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 UTF-8 compile compile org.apache.maven.plugins maven-assembly-plugin 2.3 false jar-with-dependencies com.swordfall.restserver.base.WebServer make-assembly package assembly    2.3 maven-assembly-plugins zip打包

  支持定制化打包方式,更多是对项目目录的重新组装。在2.2基础上,我们还可以利用assembly,将我们依赖的jar归类,打包成一个zip。发布项目的时候,将zip包copy到服务器上,直接unzip xxx.zip,里面包含要运行的jar以及依赖的lib,还有配置的config文件,还可以包括执行脚本,可以直接启动服务。

src/main/resources **/*.properties **/*.xml net.alchim31.maven scala-maven-plugin 3.2.1 scala-compile-first process-resources add-source compile org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 UTF-8 compile compile org.apache.maven.plugins maven-jar-plugin 2.4 *.properties *.xml *.txt false true lib/ com.swordfall.restserver.base.WebServer conf/ org.apache.maven.plugins maven-assembly-plugin 2.4 src/main/assembly/assembly.xml make-assembly package single   maven-assembly-plugin还需要配置一个assembly.xml文件,用于过滤不必要的第三方jar包文件,或者确定自己编译的项目文件路径、项目的启动脚本文件目录,打包的类型格式zip或者tar.gz等。  assembly.xml文件内容为: bin false zip false lib false ${project.build.directory} *.jar ${project.basedir}/src/main/scripts bin startup.*

  打包结果为:

  

2.4 maven-shade-plugin 打包

  用来打可执行包,包含依赖,以及对依赖进行取舍过滤。使用这种方式打出来的是揉合在一起,成为一个jar文件。这种打包方式的优点是可以利用transformer把第三方jar相同的配置文件组合成一个文件,如reference.conf文件等等。

  pom.xml配置如下:

${project.build.directory}/classes src/main/resources true **/*.xml **/*.conf net.alchim31.maven scala-maven-plugin 3.2.1 scala-compile-first process-resources add-source compile org.apache.maven.plugins maven-compiler-plugin 3.1 1.8 1.8 UTF-8 compile compile org.apache.maven.plugins maven-shade-plugin 2.4.3 package shade com.hongshan.bdp.restserver.base.WebServer reference.conf version.conf akka-http-version.conf true jar-with-dependencies io.spray:spray-json_2.11:jar: org.slf4j:slf4j-api:jar: org.slf4j:slf4j-log4j12:jar: log4j:log4j:jar: commons-logging:commons-logging:jar: commons-lang:commons-lang:jar: true

  打包结果:

  

2.5 其他插件 2.5.1 maven-surefire-plugin

  该插件主要用于项目maven编译打包时,跳过单元测试,pom.xml配置为:

[...] org.apache.maven.plugins maven-surefire-plugin 2.22.2 true [...] 总结

【github地址】

https://github.com/SwordfallYeung/MavenPackage

【参考资料】

https://blog.csdn.net/qq_16055765/article/details/79481258

https://www.cnblogs.com/Andrew520/p/8857603.html

https://blog.csdn.net/zzm3280/article/details/84953070

https://blog.csdn.net/hxpjava1/article/details/79711710

https://blog.csdn.net/u012834750/article/details/80937747 scala打包插件配置①

https://blog.csdn.net/tf461991046/article/details/80834685 scala打包插件配置②

https://blog.csdn.net/u013019338/article/details/83377070 spring-boot配置读取外部配置文件

https://www.cnblogs.com/hdwang/p/6627912.html 普通jar包如何读取外部的配置文件

https://www.cnblogs.com/wangfajun/p/9585530.html linux shell脚本启动或停止jar

https://blog.csdn.net/qq_18300109/article/details/80798334 IDEA如何打包可运行jar,外部引用jar包版

https://blog.csdn.net/qingfengmuzhu1993/article/details/80284739 IDEA自身打包方式

https://my.oschina.net/u/2377110/blog/1585553 shade 过滤包名

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3