1. Idea自身打包方式
1.1 创建Artifacts
快捷键(Ctrl+Alt+Shift+S)打开项目的Project Structure。在Artifacts创建
接着,指定main class,如下:
最后,得到创建得到的artifacts
1.2 打包Artifacts
在菜单栏目选Build,最后一栏Build Artifacts
最后,在出现的窗口创建
得到打包结果:
2. maven打包方式
2.1 maven-jar-plugins 无依赖第三方jar的打包
pom.xml配置如下:
<!--①无依赖其他任何jar打包--> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.conf</include> </includes> </resource> </resources> <plugins> <!--scala打包插件--> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!--java打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.swordfall.restserver.base.WebServer</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
运行:先mvn clean,再 package,在target中找到打包出来的xxx.jar包,运行java -jar xxx.jar即可,但是如果程序有依赖其他包,比如程序依赖jdbc去查询db,这时候再执行就会出现找不到jdbc依赖,因为我们并没有将依赖包打进去。
2.2 maven-assembly-plugins 解决依赖第三方jar包,并可执行jar的打包
pom.xml配置如下:
<!--②解决依赖第三方,可执行jar的打包,全量打包--> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.conf</include> </includes> </resource> </resources> <plugins> <!--scala打包插件--> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> </executions> </plugin> <!--java打包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.3</version> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.swordfall.restserver.base.WebServer</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2.3 maven-assembly-plugins 解决依赖第三方jar包,并可执行jar的打包
总结
【参考资料】
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自身打包方式