Maven 是一个执行插件的框架,每一个任务实际上是由插件完成的。
mvn [plugin-name]:[goal-name]
在本地仓库下就可以看到插件: D:\Repository\mvn\org\apache\maven\plugins\
每个插件要设置其对应的phrase,和插件的目标。
比如phrase是package,也就是打包,目标是打war包,jar包还是pom包等等。
Maven 插件通常用于:
-
创建 jar 文件
-
创建 war 文件
-
编译代码文件
-
进行代码单元测试
-
创建项目文档
-
创建项目报告

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build>
生成项目:
>mvn archetype:generate
Archetype 是一个 Maven 插件,其任务是按照其模板来创建一个项目结构。
mvn编译插件可解决mvn自动构建为J2E1.5的问题:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
dependency-plugin插件:
依赖打包插件,将依赖的jar包全部打入target的lib中:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
Jetty插件:
<build> <plugins> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.5</version> <configuration> <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory> <scanIntervalSeconds>3</scanIntervalSeconds> <contextPath>/jetty</contextPath> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>4000</port> </connector> </connectors> </configuration> </plugin> </plugins> </build>
Tomcat插件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/MavenWeb</path> </configuration> </plugin>
SpringBoot插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
如果项目引用的是spring-dependency,而不是spring-parent,则打包会出现 jar中没有主清单属性
以上要修改为:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.version}</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
Jacoco 插件:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.6</version> <!-- <configuration> <destFile>${basedir}/target/jacoco.exec</destFile> <dataFile>${basedir}/target/jacoco.exec</dataFile> <output>file</output> <append>true</append> </configuration>--> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
surefire 插件:
用于单元测试自动扫描*Test.java文件,如果遇到中文乱码,则在properties中添加:
<argLine>-Dfile.encoding=UTF-8</argLine>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin>