maven-assembly-plugin 打包简单案例

时间:2022-01-29 02:51:28
1. maven  netty  lomback 
 
包含项目依赖
  1. <dependencies>
  2. <dependency>
  3. <groupId>io.netty</groupId>
  4. <artifactId>netty-all</artifactId>
  5. <version>4.1.9.Final</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.projectlombok</groupId>
  9. <artifactId>lombok</artifactId>
  10. <version>1.16.18</version>
  11. <scope>provided</scope>
  12. </dependency>
  13. </dependencies>
 
2. 几个打包插件使用
 
依赖管理、依赖剔除处理、启动函数处理
  1. <build>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-compiler-plugin</artifactId>
  5. <configuration>
  6. <source>1.6</source>
  7. <target>1.6</target>
  8. <encoding>UTF-8</encoding>
  9. <compilerArguments>
  10. <extdirs>lib/</extdirs>
  11. </compilerArguments>
  12. </configuration>
  13. </plugin>
  14. <plugin>
  15. <artifactId>maven-assembly-plugin</artifactId>
  16. <executions>
  17. <execution>
  18. <id>make-assembly</id>
  19. <phase>package</phase>
  20. <goals>
  21. <goal>single</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. <configuration>
  26. <archive>
  27. <manifest>
  28. <mainClass>com.dalong.netty.MyMain</mainClass>
  29. </manifest>
  30. </archive>
  31. <!--<descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef>
  32. </descriptorRefs>-->
  33. <descriptors><!--描述文件路径-->
  34. <descriptor>src/main/assembly/assembly.xml</descriptor>
  35. </descriptors>
  36. </configuration>
  37. </plugin>
  38. <plugin>
  39. <groupId>org.apache.maven.plugins</groupId>
  40. <artifactId>maven-jar-plugin</artifactId>
  41. <configuration>
  42. <archive>
  43. <manifest>
  44. <addClasspath>true</addClasspath>
  45. <classpathPrefix>lib/</classpathPrefix>
  46. <mainClass>com.dalong.netty.MyMain</mainClass>
  47. </manifest>
  48. </archive>
  49. <excludes>
  50. <exclude>**/*.conf</exclude>
  51. </excludes>
  52. </configuration>
  53. </plugin>
  54. <plugin>
  55. <groupId>org.apache.maven.plugins</groupId>
  56. <artifactId>maven-dependency-plugin</artifactId>
  57. <executions>
  58. <execution>
  59. <id>copy-dependencies</id>
  60. <phase>prepare-package</phase>
  61. <goals>
  62. <goal>copy-dependencies</goal>
  63. </goals>
  64. <configuration>
  65. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  66. <overWriteReleases>false</overWriteReleases>
  67. <overWriteSnapshots>false</overWriteSnapshots>
  68. <overWriteIfNewer>true</overWriteIfNewer>
  69. <excludeScope>provided</excludeScope>
  70. </configuration>
  71. </execution>
  72. </executions>
  73. </plugin>
  74. <plugin>
  75. <groupId>org.projectlombok</groupId>
  76. <artifactId>lombok-maven-plugin</artifactId>
  77. <version>1.16.18.0</version>
  78. <executions>
  79. <execution>
  80. <phase>generate-sources</phase>
  81. <goals>
  82. <goal>delombok</goal>
  83. </goals>
  84. </execution>
  85. </executions>
  86. </plugin>
  87. </plugins>
  88. </build>
 
3.  assembly.xml 文件定义
 
文件别名、配置拷贝
  1. <assembly
  2. xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  5. <id>mydemo</id>
  6. <includeBaseDirectory>true</includeBaseDirectory>
  7. <formats>
  8. <format>zip</format>
  9. </formats>
  10. <fileSets>
  11. <fileSet>
  12. <directory>${project.basedir}\src\main\resources</directory>
  13. <outputDirectory>conf/</outputDirectory>
  14. </fileSet>
  15. <fileSet>
  16. <directory>${project.basedir}\target\lib</directory>
  17. <outputDirectory>lib/</outputDirectory>
  18. </fileSet>
  19. </fileSets>
  20. <files>
  21. <file>
  22. <outputDirectory>/</outputDirectory>
  23. <source>${project.basedir}\target\websocket-server-0.0.1-SNAPSHOT.jar</source>
  24. <destName>websocket-server.jar</destName>
  25. </file>
  26. </files>
  27. </assembly>
 
4. 打包构建
 
  1. mvn clean compile paakge
 
5. 参考说明