JavaMaven Plugin 相关使用
MeteorCatSpringBoot 高级框架底层都内部集成了 Maven 打包插件, 有时候 Java 最小依赖就需要自己引入底层打包功能
这里 Maven 内部主要插件有以下类型
-
maven-compiler-plugin: 自动扫描项目 src/main/java 所有 Java 文件编译生成字节码到 target/classes 目录
-
javax.annotation-api: 编译通用 Java 项目的注解依赖包, 后面官方包组织已经移动到 jakarta.annotation-api
-
maven-surefire-plugin: 用于运行单元测试功能
-
maven-resources-plugin: 在编译前处理资源文件, 可以针对某些文件做动态打包
-
maven-assembly-plugin: 将打包编译后的 class 生成对应 jar/war 包
-
maven-shade-plugin: 将打包做排除冗余依赖来堆 Jar 包做瘦身, 比 assembly 体积小很多
一般没必要对 Jar 做极致的瘦身, 所以 maven-assembly-plugin 日常已经够用了
一般搭建纯净的 Maven 项目之后比较常用的就是 maven-compiler-plugin, 主要使用方式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| <project> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version> <maven.assembly.plugin.version>3.7.1</maven.assembly.plugin.version>
<maven.test.skip>true</maven.test.skip> </properties>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <compilerArgument>-Xlint:all</compilerArgument>
<forkOptions> <jvmArgs>-Xms512m -Xmx1024m</jvmArgs> </forkOptions>
<excludes> <exclude>**/Temp*.java</exclude> <exclude>**/Debug*.java</exclude> </excludes> </configuration> </plugin>
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>${maven.assembly.plugin.version}</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>NovaGameApplication</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
|
其他高级的框架只是将这部分 Maven 依赖给隐藏, 实际上底层大部分是这样处理从而节省开发周期无意义配置时间