Maven 常见插件

版权声明:欢迎转载,转载请说明出处. 大数据Github项目地址https://github.com/SeanYanxml/bigdata。 https://blog.csdn.net/u010416101/article/details/88530783

前言

上章我们了解了Maven的基础命令和生命周期. 本章我们详细的聊聊插件,为后面的打包做基础.


插件

所有的插件配置都是写在pom.xml文件的<build>目录之间的.我们常见的Maven插件如下所示:

  • maven-compiler-pluginJDK版本和编码

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>1.8</source>
		<target>1.8</target>
		<encoding>utf-8</encoding>
	</configuration>
</plugin>
  • maven-resources-plugin设置Resources编码
<!-- resource插件, 设定编码,防止中文乱码 -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<configuration>
		<encoding>${project.build.sourceEncoding}</encoding>
	</configuration>
</plugin>
  • maven-jar-pluginJar包 指定启动类
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <archive>
            <manifest>
                <mainClass>com.yanxml.win.AWTFileReaderDemo</mainClass>
                <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            </manifest>

        </archive>
        <classesDirectory>
        </classesDirectory>
    </configuration>
</plugin>
  • maven-war-pluginwar包
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<configuration>
		<webResources>
			<resource>
				<directory>src/main/resources/static</directory>
				<targetPath>WEB-INF/classes</targetPath>
				<filtering>true</filtering>
			</resource>
		</webResources>
		<failOnMissingWebXml>false</failOnMissingWebXml>
	</configuration>
</plugin> 
  • maven-source-plugin源码包
<!--demo1-->
<plugin>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!--demo2-->
<plugin>  
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-source-plugin</artifactId>  
	<version>2.1</version>  
	<configuration>  
		<attach>true</attach>  
	</configuration>  
	<executions>  
		<execution>  
			<phase>compile</phase>  
			<goals>  
				<goal>jar</goal>  
			</goals>  
		</execution>  
	</executions>  
</plugin>
  • maven-surefire-plugin跳过单元测试
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

<skip>true</skip>mvn package -Dmaven.test.skip=true

  • maven-shade-plugin所有依赖包压成一个包
<!--demo1-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>com.meiyou.topword.App</Main-Class>
                            <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
                            <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

<!--demo1-->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>1.2.1</version>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
				<transformers>
					<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
						<mainClass>com.zhaoyanblog.Launcher</mainClass>
					</transformer>
					<!--防止spring多个包同名配置文件覆盖-->
					<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>
  • maven-dependency-plugin所有依赖包另外输出
<!--demo1-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.10</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<!--demo2-->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>copy</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<includeScope>runtime</includeScope>
				<outputDirectory>target/lib/</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>

  • maven-assembly-plugin自定义打包(Jar/Tar.gz)
<!-- to generate compressed package(tar package) specified by package.xml 
	begin -->
<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<finalName>${package}-${package.version}</finalName>
		<ignorePermissions>true</ignorePermissions>
		<appendAssemblyId>false</appendAssemblyId>
		<descriptors>
			<descriptor>${basedir}/scripts/${package.format}/common/package.xml</descriptor>
		</descriptors>
	</configuration>
</plugin>
  • 自定义打包(package.xml文件)
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 
  http://maven.apache.org/xsd/assembly-1.1.3.xsd">

	<id>0.1</id>
	<formats>
		<format>tar</format>
	</formats>

	<fileSets>

		<fileSet>
			<directory>images</directory>
			<outputDirectory>/images</outputDirectory>
		</fileSet>
	</fileSets>

	<files>
		<file>
			<source>target/${package}-${package.version}.war</source>
			<outputDirectory>/</outputDirectory>
			<destName>${package}-${package.version}.war</destName>
			<fileMode>0755</fileMode>
		</file>
	</files>

</assembly>
  • spring-boot-maven-pluginspringboot打包插件
<!-- to generate excutable jar/war with springboot style begin -->
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<phase>package</phase>
			<goals>
				<goal>repackage</goal>
			</goals>
		</execution>
	</executions>
	<configuration>
		<mainClass>${start-class}</mainClass>
	</configuration>
</plugin>

注: 更多的打包插件,请查看http://maven.apache.org/plugins/index.html 官方提供的文档.


Reference

[1]. Maven的几个常用plugin

[2]. maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项

[3]. maven关于打包的那些插件

[4]. Java技术–maven的assembly插件打包(依赖包归档

[5]. 使用maven插件对java工程进行打包

[6]. Offical Maven Plugin Doc

没有更多推荐了,返回首页