Maven插件简述
Maven中有两种类型的插件
- Build plugins 是在项目创建时执行的插件,位于
<build/>
标签内
- Reporting plugins是在网站发布时执行的插件,位于
<reporting/>
标签内
插件原理
所有的Maven插件的配置都来自于<configuration>
标签,每个<configuration>
都会映射到Mojo类(Mojo类有插件具体执行工作的类)中你所指定的变量或set方法,具体案例如下
创建一个插件用于访问特定的url,并需要配置url和超时时间等配置,创建步骤如下
创建Maven工程,可使用quickstar快速创建
修改pom文件
1 2 3 4 5 6 7 8
| <packaging>maven-plugin</packaging>
<dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.5.0</version> </dependency>
|
实现AbstractMojo接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class MyQueryMojo extends AbstractMojo { @Parameter(property = "query.url", required = true) private String url; @Parameter(property = "timeout", required = false, defaultValue = "50") private int timeout; @Parameter(property = "options") private String[] options; public void execute() throws MojoExecutionException { ... } }
|
执行mvn clean install
将会在本地.m2库中生成你的插件
具体如何使用插件如下,可对应步骤3了解插件配置如何映射
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| oject> ... <build> <plugins> <plugin> <artifactId>maven-myquery-plugin</artifactId> <version>1.0</version> <configuration> <url>http://www.foobar.com/query</url> <timeout>10</timeout> <options> <option>one</option> <option>two</option> <option>three</option> </options> </configuration> </plugin> </plugins> </build> ... </project>
|
有时,我们想通过命令行界面来控制url参数以便我们不去修改pom.xml的configuration
配置
- pom配置中的url修改为
${query.url}
- 执行命令
mvn myquery:query -Dquery.url=http://maven.apache.org
6.当有复杂参数时,需要定义实体类封装参数
1 2 3 4 5 6
| <configuration> <person implementation="com.mycompany.mojo.query.SuperPerson"> <firstName>Jason</firstName> <lastName>van Zyl</lastName> </person> </configuration>
|
常用插件
关于Maven插件的原理参考
https://blog.csdn.net/jerry010101/article/details/93601563
插件文档:
- http://maven.apache.org/plugins/index.html
- http://mojo.codehaus.org/plugins.html
maven内置配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| ${basedir}表示项目根目录,即包含pom.xml文件的目录;
${version}表示项目版本;
${project.basedir}同${basedir};
${project.baseUri}表示项目文件地址;
${maven.build.timestamp}表示项目构件开始时间;
${maven.build.timestamp.format}表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat。 ${project.build.directory}表示主源码路径;
${project.build.sourceEncoding}表示主源码的编码格式;
${project.build.sourceDirectory}表示主源码路径;
${project.build.finalName}表示输出文件名称;
${project.version}表示项目版本,与${version}相同;
${project.xxx} 当前pom文件的任意节点的内容 ${env.xxx} 获取系统环境变量。 ${settings.xxx} 指代了settings.xml中对应元素的值。
|
编译
maven-compiler-plugin
设置maven编译的jdk版本,maven3默认用jdk1.5,maven2默认用jdk1.3
1 2 3 4 5 6 7 8 9 10 11
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> <skipTests>true</skipTests> </configuration> </plugin>
|
打包
Maven Archiver
Maven Archiver是主要的控制打包功能的配置
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
| <archive> <addMavenDescriptor>false</addMavenDescriptor> <compress>true</compress> <forced/> <index/>
<pomPropertiesFile/> <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile> <manifest> <addClasspath>true</addClasspath>
<addDefaultEntries/>
<addDefaultImplementationEntries/>
<addDefaultSpecificationEntries/>
<addBuildEnvironmentEntries/>
<addExtensions/>
<classpathLayoutType/>
<classpathPrefix>lib/.</classpathPrefix> <customClasspathLayout/> <mainClass/> <packageName/> <useUniqueVersions/> </manifest> <manifestEntries> <key>value</key> </manifestEntries> <manifestSections> <manifestSection> <name/> <manifestEntries> <key>value</key> </manifestEntries> </manifestSection> </manifestSections> </archive>
|
maven-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
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <addClasspath>true</addClasspath>
<classpathPrefix>lib/.</classpathPrefix> </manifest> </archive> <excludes> <exclude>**/bookFlight/</exclude> <exclude>**/database/</exclude> <exclude>**/debug/</exclude> <exclude>**/IBE/</exclude> <exclude>*.xml</exclude> </excludes> </configuration> </plugin>
|
maven-assembly-plugin
- maven-assembly-plugin的用途是制作项目分发包,该分发包可能包含了项目的可执行文件、源代码、readme、平台脚本等等。 maven-assembly-plugin支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的,例如用户可以 按文件级别的粒度、文件集级别的粒度、模块级别的粒度、以及依赖级别的粒度控制打包,此外,包含和排除配置也是支持的。maven-assembly- plugin要求用户使用一个名为assembly.xml的元数据文件来表述打包,它的single目标可以直接在命令行调用,也可以被绑定至生命周期。
- assembly插件打压缩包(jar)
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
| <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs>
<archive> <manifest> <mainClass>org.sample.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
|
- 当有个性化配置时可参考Assembly,,改方案需要该插件支持
<descriptor/>
Assembly配置
- assembly文件是插件的配置,通过配置的变更来实现不同的自定义打包方式和类型等
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id/>
<formats/> <includeBaseDirectory/> <baseDirectory/> <includeSiteDirectory/> <containerDescriptorHandlers> <containerDescriptorHandler> <handlerName/> <configuration/> </containerDescriptorHandler> </containerDescriptorHandlers> <moduleSets> <moduleSet> <useAllReactorProjects/> <includeSubModules/> <includes/> <excludes/> <sources> <useDefaultExcludes/> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <fileSets> <fileSet> <useDefaultExcludes/> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <directory/> <lineEnding/> <filtered/> <nonFilteredFileExtensions/> </fileSet> </fileSets> <includeModuleDirectory/> <excludeSubModuleDirectories/> <outputDirectoryMapping/> </sources> <binaries> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <attachmentClassifier/> <includeDependencies/> <dependencySets> <dependencySet> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <useStrictFiltering/> <outputFileNameMapping/> <unpack/> <unpackOptions> <includes/> <excludes/> <filtered/> <nonFilteredFileExtensions/> <lineEnding/> <useDefaultExcludes/> <encoding/> </unpackOptions> <scope/> <useProjectArtifact/> <useProjectAttachments/> <useTransitiveDependencies/> <useTransitiveFiltering/> </dependencySet> </dependencySets> <unpack/> <unpackOptions> <includes/> <excludes/> <filtered/> <nonFilteredFileExtensions/> <lineEnding/> <useDefaultExcludes/> <encoding/> </unpackOptions> <outputFileNameMapping/> </binaries> </moduleSet> </moduleSets> <fileSets> <fileSet> <useDefaultExcludes/> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <directory/>
<lineEnding/> <filtered/> <nonFilteredFileExtensions/> </fileSet> </fileSets> <files> <file> <source/> <sources/> <outputDirectory/> <destName/> <fileMode/> <lineEnding/> <filtered/> </file> </files> <dependencySets> <dependencySet> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <useStrictFiltering/> <outputFileNameMapping/> <unpack/> <unpackOptions> <includes/> <excludes/> <filtered/> <nonFilteredFileExtensions/> <lineEnding/> <useDefaultExcludes/> <encoding/> </unpackOptions> <scope/> <useProjectArtifact/> <useProjectAttachments/> <useTransitiveDependencies/> <useTransitiveFiltering/> </dependencySet> </dependencySets> <repositories> <repository> <outputDirectory/> <includes/> <excludes/> <fileMode/> <directoryMode/> <includeMetadata/> <groupVersionAlignments> <groupVersionAlignment> <id/> <version/> <excludes/> </groupVersionAlignment> </groupVersionAlignments> <scope/> </repository> </repositories> <componentDescriptors/> </assembly>
|
container–file-aggregator
- 该组件负责将读取
<filePattern/>
中的表达式对应的文件,将其内容合并,最终输出到<outputPath/>
中
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> .... <containerDescriptorHandlers> <containerDescriptorHandler> <handlerName>file-aggregator</handlerName> <configuration> <filePattern>.*/file.txt</filePattern> <outputPath>file.txt</outputPath> </configuration> </containerDescriptorHandler> </containerDescriptorHandlers> </assembly>
|
- 这个组件会匹配所有的
META-INF/services
目录中的文件并将它们合并到一个META-INF/services
目录下的文件中
1 2 3 4 5 6 7 8 9 10
| <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> .... <containerDescriptorHandlers> <containerDescriptorHandler> <handlerName>metaInf-services</handlerName> </containerDescriptorHandler> </containerDescriptorHandlers> </assembly>
|
- 该组件类似metaInf-services会将文件名为
META-INF/spring.
开头的文件合并
1 2 3 4 5 6 7 8 9 10
| <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> .... <containerDescriptorHandlers> <containerDescriptorHandler> <handlerName>metaInf-spring</handlerName> </containerDescriptorHandler> </containerDescriptorHandlers> </assembly>
|
container–plexus
- 该组件会同上,将所有
META-INF/plexus/components.xml
文件内容合并,并输出到一个有效的META-INF/plexus/components.xml
文件中
1 2 3 4 5 6 7 8 9 10
| <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> .... <containerDescriptorHandlers> <containerDescriptorHandler> <handlerName>plexus</handlerName> </containerDescriptorHandler> </containerDescriptorHandlers> </assembly>
|
Assembly样例
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
| <?xml version="1.0" encoding="UTF-8"?> <assembly> <id>full</id> <formats> <format>zip</format> </formats>
<dependencySets> <dependencySet> <useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet> </dependencySets>
<fileSets> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory></outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet>
<fileSet> <directoryl>${projet.document.directory}</directoryl> <outputDirectory></outputDirectory> <includes> <include>readme.*</include> </includes> </fileSet>
<fileSet> <directory>${project.document.directory}</directory> <outputDirectory>document</outputDirectory> <excludes> <exclude>readme.*</exclude> </excludes> </fileSet>
<fileSet> <directory>${project.script.execute.directory}</directory> <outputDirectory></outputDirectory> <includes> <include>*</include> </includes> </fileSet>
</fileSets> </assembly>
|
maven-shade-plugin
使用maven-assembly-plugin容易产生一个错误,就是在多个依赖冲突的时候无法解决。不过使用 maven-shade-plugin 生成一个 uber-jar,可以解决依赖间的冲突,适用于Spring Framework。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <configuration> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ... </project>
|
- 该插件有以下几个默认的配置功能可供选择,命名空间都为
org.apache.maven.plugins.shade.resource
一些服务提供者或jar包产商在提供的jar包中都会提供他们的权限在META-IN
文件夹中,它们都会被习惯性的被命名为LICENSE
或LICENSE.txt
,当合并这些依赖时,添加这些资源时就会产生冲突而报错;ApacheLicenseResourceTransformer
配置则可以忽视掉这些名字的证书
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
| <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer"> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>
|
一些jar包包含一些同名资源文件例如(propertes文件),你可以合并输出它以避免冲突
- 如下案例将
spring-context
和plexus-spring
这两个jar包的META-INF/spring.handlers
文件,该文件是用于处理XML schema的命名空间的
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
| <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project>
|
其它插件
maven-archetype-plugin
http://maven.apache.org/archetype/maven-archetype-plugin/
Archtype指项目的骨架,Maven初学者最开始执行的Maven命令可能就是mvn archetype:generate,这实际上就是让maven-archetype-plugin生成一个很简单的项目骨架,帮助开发者快速上手。可能也有人看到一些文档写了mvn archetype:create, 但实际上create目标已经被弃用了,取而代之的是generate目标,该目标使用交互式的方式提示用户输入必要的信息以创建项目,体验更好。 maven-archetype-plugin还有一些其他目标帮助用户自己定义项目原型,例如你由一个产品需要交付给很多客户进行二次开发,你就可以为 他们提供一个Archtype,帮助他们快速上手。
1 2 3 4 5
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version>3.0.0</version> </plugin>
|
maven-resource-plugin
该插件处理项目的资源文件拷贝到输出目录。可以分别处理main resources 和 test resources。
1 2 3 4 5 6 7 8
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin>
|
参考文档:
- https://blog.csdn.net/daerzei/java/article/details/82883472
- https://blog.csdn.net/jerry010101/java/article/details/93601563
- https://blog.csdn.net/liuzhuojava/article/details/86691218
- https://maven.apache.org/plugins