maven打包可执行jar的pom配置---笔记

Myth丶恋晨 2024-03-22 16:45 145阅读 0赞

使用maven把一个java项目打成可以用java -jar命令执行的jar ,需要配置的pom文件内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>cn.xxxx.demo</groupId>
  7. <artifactId>RpcServer</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- 配置打jar包 -->
  10. <packaging>jar</packaging>
  11. <!-- 声明本项目使用的java版本和字符编码 -->
  12. <properties>
  13. <java.version>1.8</java.version>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>com.thoughtworks.xstream</groupId>
  20. <artifactId>xstream</artifactId>
  21. <version>1.4.10</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>io.netty</groupId>
  25. <artifactId>netty-all</artifactId>
  26. <version>4.1.50.Final</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.projectlombok</groupId>
  30. <artifactId>lombok</artifactId>
  31. <version>1.18.16</version>
  32. </dependency>
  33. </dependencies>
  34. <build>
  35. <plugins>
  36. <!-- 配置maven编译使用的java版本和字符编码 -->
  37. <plugin>
  38. <groupId>org.apache.maven.plugins</groupId>
  39. <artifactId>maven-compiler-plugin</artifactId>
  40. <configuration>
  41. <source>8</source>
  42. <target>8</target>
  43. <encoding>UTF-8</encoding>
  44. </configuration>
  45. </plugin>
  46. <!-- 配置jar包启动的主类以及依赖的classpath路径 -->
  47. <plugin>
  48. <groupId>org.apache.maven.plugins</groupId>
  49. <artifactId>maven-jar-plugin</artifactId>
  50. <configuration>
  51. <archive>
  52. <manifest>
  53. <mainClass>cn.xxxx.demo.Provider</mainClass>
  54. <addClasspath>true</addClasspath>
  55. <useUniqueVersions>false</useUniqueVersions>
  56. <classpathPrefix>lib/</classpathPrefix>
  57. </manifest>
  58. <manifestEntries>
  59. <Class-Path>.</Class-Path>
  60. </manifestEntries>
  61. </archive>
  62. </configuration>
  63. </plugin>
  64. <!-- 配置将app依赖的第三方jar包,copy到本项目最终生成jar包目录里的lib文件夹里 -->
  65. <plugin>
  66. <groupId>org.apache.maven.plugins</groupId>
  67. <artifactId>maven-dependency-plugin</artifactId>
  68. <executions>
  69. <execution>
  70. <id>copy-dependencies</id>
  71. <phase>package</phase>
  72. <goals>
  73. <goal>copy-dependencies</goal>
  74. </goals>
  75. <configuration>
  76. <type>jar</type>
  77. <includeTypes>jar</includeTypes>
  78. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  79. </configuration>
  80. </execution>
  81. </executions>
  82. </plugin>
  83. </plugins>
  84. </build>
  85. </project>

发表评论

表情:
评论列表 (有 0 条评论,145人围观)

还没有评论,来说两句吧...

相关阅读