Maven 配置文件 POM 的常用插件配置代码
Maven 配置文件 POM 的常用插件配置代码
- 普通
- 将 Maven 多模块依赖集成打进一个 JAR 包(方法 1)
- 将 Maven 多模块依赖集成打进一个 JAR 包(方法 2)
- 生成单入口类 JAR 包
- 生成完整依赖单 JAR 包
- 导出资源文件
- Spring Boot
- 制作 JAR 包
- JavaFX
- 添加在命令行运行 JavaFX 应用的功能
【说明】
本文只是关于 Maven 中的插件(plugin)的 POM 代码。
关于 Maven 的常用依赖配置代码,可见笔者的另一篇博客:
Maven 配置文件 POM 的常用依赖配置代码:
https://blog.csdn.net/wangpaiblog/article/details/112797500
普通
运行环境
JDK 17
Maven 3.6.3
IntelliJ IDEA 2021.2.2 (Ultimate Edition)
将 Maven 多模块依赖集成打进一个 JAR 包(方法 1)
此插件可以用于在 Maven 当前模块中生成本模块的 JAR 包,其中,此 JAR 包含了本模块直接或间接依赖的所有 JAR 包,及它们直接或间接依赖的所有资源文件。如果不在父 POM 使用该插件,则需要提前将此模块依赖的其它模块手动安装至 Maven 仓库中,所以建议在父 POM 中使用该插件。
如果遇到 Maven 多模块中各自的模块有同名同路径的资源文件这种情况,此时打包时只会保留其中的一个。更详细的信息,可见笔者的另一篇博客:
将 Maven 中的多模块项目只打成一个 JAR 包:
https://blog.csdn.net/wangpaiblog/article/details/119628194?spm=1001.2014.3001.5501
使用该插件的命令为:mvn package assembly:single。
<project xmlns=...><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-assembly-pluginartifactId><version>3.3.0version><executions><execution><id>make-assemblyid><phase>packagephase><goals><goal>singlegoal>goals>execution>executions><configuration><archive><manifest><mainClass>包名.类名mainClass>manifest>archive><outputDirectory>${project.build.directory}/#maven-assembly-pluginoutputDirectory><descriptorRefs><descriptorRef>jar-with-dependenciesdescriptorRef>descriptorRefs>configuration>plugin>plugins>build>project>
将 Maven 多模块依赖集成打进一个 JAR 包(方法 2)
此插件可以用于在 Maven 当前模块中生成本模块的 JAR 包,其中,此 JAR 包含了本模块直接或间接依赖的所有 JAR 包,及它们直接或间接依赖的所有资源文件。如果不在父 POM 使用该插件,则需要提前将此模块依赖的其它模块手动安装至 Maven 仓库中,所以建议在父 POM 中使用该插件。
如果遇到 Maven 多模块中各自的模块有同名同路径的资源文件这种情况,此时打包时只会保留其中的一个。更详细的信息,可见笔者的另一篇博客:
将 Maven 中的多模块项目只打成一个 JAR 包:
https://blog.csdn.net/wangpaiblog/article/details/119628194?spm=1001.2014.3001.5501
使用该插件的命令为:mvn package。
<project xmlns=...><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-shade-pluginartifactId><version>3.2.4version><executions><execution><phase>packagephase><goals><goal>shadegoal>goals><configuration><shadedArtifactAttached>trueshadedArtifactAttached><shadedClassifierName>shaded-with-dependenciesshadedClassifierName><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>包名.类名mainClass>transformer>transformers><outputDirectory>${project.build.directory}/#maven-shade-pluginoutputDirectory>configuration>execution>executions>plugin>plugins>build>project>
生成单入口类 JAR 包
此插件可以用于在 Maven 当前模块中生成单入口类 JAR 包。利用此插件生成的 JAR 包,只包含当前模块的文件(包含本模块下的资源文件),虽然不包含任何依赖,但其中设置了程序运行入口类、依赖 JAR 包的相对路径。可以通过将依赖 JAR 包放入指定目录,然后仅通过本 JAR 包(无需在命令行中指定依赖包路径)来运行 Java 应用。因此,本插件一般在程序入口模块使用。本插件往往与插件 maven-dependency-plugin 一起配合使用。
<project xmlns=...><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-jar-pluginartifactId><version>3.2.0version><configuration><archive><manifest><mainClass>包名.类名mainClass><classpathPrefix>lib/classpathPrefix><addClasspath>trueaddClasspath>manifest>archive><outputDirectory>${project.build.directory}/#maven-jar-pluginoutputDirectory>configuration>plugin>plugins>build>project>
生成完整依赖单 JAR 包
此插件可以用于在 Maven 当前模块中生成完整的依赖单 JAR 包。完整指的是对于本模块直接或间接依赖的 JAR 包都会生成,且这些 JAR 包还各自包含了它们所属模块的资源文件。单 JAR 包指的是生成各依赖包之间互相没有交集(没有重复依赖)。但不生成本模块自身的 JAR 包,所以在此插件的设置中无需设置程序运行入口类。本插件往往与插件 maven-jar-plugin 一起配合使用。
<project xmlns=...><build><plugins><plugin><groupId>org.apache.maven.pluginsgroupId><artifactId>maven-dependency-pluginartifactId><version>3.2.0version><executions><execution><id>copy-dependenciesid><phase>packagephase><goals><goal>copy-dependenciesgoal>goals><configuration><outputDirectory>${project.build.directory}/#maven-jar-plugin/liboutputDirectory><overWriteReleases>trueoverWriteReleases><overWriteSnapshots>trueoverWriteSnapshots><overWriteIfNewer>trueoverWriteIfNewer><includeScope>compileincludeScope>configuration>execution>executions>plugin>plugins>build>project>
导出资源文件
此插件可以用于在 Maven 当前模块中导出本模块所需的资源文件。但生成的资源文件只包含本模块的资源文件,不包括其依赖的其它模块的资源文件,因此作用有限。此插件往往与插件 maven-jar-plugin、maven-dependency-plugin 一起使用。此插件建议放在父 POM 中使用。
<project xmlns=...><build><plugins><plugin><artifactId>maven-resources-pluginartifactId><version>3.2.0version><executions><execution><id>copy-resourcesid><phase>packagephase><goals><goal>copy-resourcesgoal>goals><configuration><outputDirectory>${project.build.directory}/#maven-resources-pluginoutputDirectory><resources><resource><directory>src/main/resourcesdirectory>resource>resources>configuration>execution>executions>plugin>plugins>build>project>
Spring Boot
制作 JAR 包
对于 Spring Boot 项目,使用常规的生成 JAR 包的插件会引发很多问题,因此,对于它们应该使用如下插件。
<project xmlns=...><build><plugins><plugin><groupId>org.springframework.bootgroupId><artifactId>spring-boot-maven-pluginartifactId>plugin>plugins>build>project>
【注意事项】
-
使用此插件的 Maven 模块,必须直接或间接继承至 Spring Boot 模块。也就是说,此模块或其直接或间接父模块需要添加如下继承代码。
<parent><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-parentartifactId><version>Spring Boot 的版本version> parent>- 如果不这么做,使用本插件打包成 JAR 后运行时会有遇到各种问题,如本插件失效,从而打包时不包含依赖等等。
- 如果不这么做,而选择普通方法(将 Spring Boot 视为常规依赖项,然后使用其它插件进行打包),打包成 JAR 后运行时会有遇到各种问题,如运行时提示缺失 Spring Boot 的一些配置文件等等。
-
本项目的入口类被 Spring Boot 认为是拥有注解
@SpringBootApplication的类。 -
只有有 Spring Boot 入口类的模块才能运行本插件。另外,如果使用本插件的模块有多于一个的 Spring Boot 入口类,打包时会报错。
-
当使用了此插件制作 JAR 包后,程序的启动类会变成
org.springframework.boot.loader.JarLauncher。
JavaFX
运行环境:
JDK 17
JavaFX 17-ea+11
Maven 3.6.3
IntelliJ IDEA 2020.2.2 (Ultimate Edition)
添加在命令行运行 JavaFX 应用的功能
<project xmlns=...><build><plugins><plugin><groupId>org.openjfxgroupId><artifactId>javafx-maven-pluginartifactId><version>0.0.6version><executions><execution><id>default-cliid><configuration><mainClass>包名.类名mainClass>configuration>execution>executions>plugin>plugins>build>project>
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
