maven-compiler-plugin 这个插件是用来编译源代码的。
例如报错 static import declarations are not supported in -source 1.3,这是因为编译的时候默认是 1.3 版本。可以设定编译器的版本为 1.6。
注意:target 版本一定大于等于 source 版本。
用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 2.3.2</version > <configuration > <source > 1.6</source > <target > 1.8</target > </configuration > </plugin > </plugins > </build >
maven-jar-plugin 打 jar 包,无依赖包。package 中指定 jar,不单独在 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 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-jar-plugin</artifactId > <version > 2.4</version > <configuration > <archive > <addMavenDescriptor > true</addMavenDescriptor > <manifest > <useUniqueVersions > false</useUniqueVersions > <addClasspath > true</addClasspath > <classpathPrefix > lib/</classpathPrefix > <mainClass > com.ht.pojo.Test</mainClass > </manifest > <manifestEntries > <Class-Path > ../config/</Class-Path > </manifestEntries > </archive > <outputDirectory > ${project.build.directory}/lib</outputDirectory > <includes > <include > **/*.class</include > <include > **/*.properties</include > </includes > </configuration > </plugin >
maven-dependency-plugin 我们在 IDE 的环境里编译和执行代码的时候,那是直接引用一些类库。但是在我们实际部署的环境里,那边很可能就一个 Java 执行环境,不可能有源代码和 IDE。这个时候,我们需要将源代码编译打包。如果我们引用的库很多的话,我们希望能够把他们统一打包到一个目录下,比如 lib 文件夹。这样部署执行的时候只需要将编译生成的程序 jar 包和依赖包文件夹拷到特定目录去执行。
用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-dependency-plugin</artifactId > <version > 3.1.1</version > <executions > <execution > <id > copy-dependencies</id > <phase > package</phase > <goals > <goal > copy-dependencies</goal > </goals > <configuration > <outputDirectory > ${project.build.directory}/alternateLocation</outputDirectory > <overWriteReleases > false</overWriteReleases > <overWriteSnapshots > false</overWriteSnapshots > <overWriteIfNewer > true</overWriteIfNewer > </configuration > </execution > </executions > </plugin >
maven-assembly-plugin 一些非 Spring Boot 项目打成可执行的 jar 包,主要打发布包使用,通过一个 xml 文件定义更多自定义打包项。
用法:
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 <?xml version="1.0" encoding="utf-8" ?> <assembly xmlns ="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd" > <id > ${project.version}</id > <formats > <format > jar</format > </formats > <includeBaseDirectory > true</includeBaseDirectory > <dependencySets > <dependencySet > <useProjectArtifact > true</useProjectArtifact > <outputDirectory > lib</outputDirectory > <scope > runtime</scope > </dependencySet > </dependencySets > <fileSets > <fileSet > <directory > src/main/script/linux/bin</directory > <outputDirectory > bin</outputDirectory > <includes > <include > terminal-dispatch</include > <include > server</include > </includes > <fileMode > 0755</fileMode > </fileSet > <fileSet > <directory > src/main/resources</directory > <outputDirectory > conf</outputDirectory > <includes > <include > config.properties</include > <include > logback.xml</include > </includes > <fileMode > 0644</fileMode > </fileSet > <fileSet > <directory > src/main/script/conf</directory > <outputDirectory > conf</outputDirectory > <includes > <include > wrapper.conf</include > </includes > <fileMode > 0644</fileMode > </fileSet > <fileSet > <directory > src/main/script/linux/lib</directory > <outputDirectory > lib</outputDirectory > <includes > <include > libwrapper.so</include > <include > wrapper.jar</include > </includes > <fileMode > 0755</fileMode > </fileSet > </fileSets > </assembly >
在 pom.xml 中引用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-assembly-plugin</artifactId > <version > 2.4</version > <executions > <execution > <id > make-zip</id > <phase > package</phase > <goals > <goal > single</goal > </goals > <configuration > <descriptors > <descriptor > src/main/script/assembly.xml</descriptor > </descriptors > </configuration > </execution > </executions > </plugin > </plugins > </build >
maven-shade-plugin 一些非 Spring Boot 项目打成可执行的 jar 包,主要打发布包使用,通过一个 xml 文件定义更多自定义打包项。
assembly 与 shade 的区别 对 xsd 文件的处理方式不同:
assembly :找到一个放入最终打出的 jar 包里,但是这样有个问题是当工程依赖到不同版本的依赖包时只能将某一个版本的 xsd 配置文件放入最终打出的 jar 包里,这就有可能遗漏了一些版本的 xsd 的本地映射,会报错shade :打包时在对 spring.schemas 文件处理上,它能够将所有 jar 里的 spring.schemas 文件进行合并,在最终生成的单一 jar 包里,spring.schemas 包含了所有出现过的版本的集合maven-surefire-plugin Maven 通过 Maven Surefire Plugin 插件执行单元测试(通过 Maven Failsafe Plugin 插件执行集成测试),也可用于打包时跳过测试。
spotless-maven-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 <plugin > <groupId > com.diffplug.spotless</groupId > <artifactId > spotless-maven-plugin</artifactId > <version > 2.44.4</version > <configuration > <java > <googleJavaFormat > <version > 1.8</version > <style > AOSP</style > </googleJavaFormat > <importOrder > <order > com.dtstack,org.apache.flink,org.apache.flink.shaded,,javax,java,scala,\#</order > </importOrder > <removeUnusedImports /> </java > </configuration > <executions > <execution > <id > spotless-check</id > <phase > validate</phase > <goals > <goal > check</goal > </goals > </execution > </executions > </plugin >