maven 常用插件

1 maven-compiler-plugin 

  1. <plugin>  
  2.     <artifactId>maven-compiler-plugin</artifactId>  
  3.     <extensions>true</extensions>   
  4.     <configuration>  
  5.         <source>1.6</source>  
  6.         <target>1.6</target>  
  7.     </configuration>  
  8. </plugin>  

2 maven-dependency-plugin 把依赖的jar包拷到指定目录下
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-dependency-plugin</artifactId>  
  4.     <executions>  
  5.         <execution>  
  6.             <id>copy-dependencies</id>  
  7.             <phase>process-resources</phase>  
  8.             <goals>  
  9.                 <goal>copy-dependencies</goal>  
  10.             </goals>  
  11.             <configuration>  
  12.                 <excludeScope>provided</excludeScope>  
  13.                 <excludeArtifactIds>  
  14.                     module1,module2  
  15.                 </excludeArtifactIds>  
  16.                 <outputDirectory>${project.build.directory}/lib</outputDirectory>  
  17.             </configuration>  
  18.         </execution>  
  19.         <execution>  
  20.             <id>copy-modules</id>  
  21.             <phase>process-resources</phase>  
  22.             <goals>  
  23.                 <goal>copy-dependencies</goal>  
  24.             </goals>  
  25.             <configuration>  
  26.                 <includeArtifactIds>  
  27.                     module1,module2  
  28.                 </includeArtifactIds>  
  29.                 <outputDirectory>${project.build.directory}/lib/modules</outputDirectory>  
  30.             </configuration>  
  31.         </execution>  
  32.     </executions>  
  33. </plugin>  

3 maven-resources-plugin 把依赖的资源拷到指定目录下

  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-resources-plugin</artifactId>  
  4.     <version>2.6</version>  
  5.     <executions>  
  6.         <execution>  
  7.             <id>copy-resources</id>  
  8.             <!-- here the phase you need -->  
  9.             <phase>validate</phase>  
  10.             <goals>  
  11.                 <goal>copy-resources</goal>  
  12.             </goals>  
  13.             <configuration>  
  14.                 <outputDirectory>${basedir}/target/test-classes</outputDirectory>  
  15.                 <resources>  
  16.                     <resource>  
  17.                         <directory>${basedir}/src/main/webapp/WEB-INF/config</directory>  
  18.                         <filtering>true</filtering>  
  19.                     </resource>  
  20.                 </resources>  
  21.             </configuration>  
  22.         </execution>  
  23.     </executions>  
  24. </plugin>  

4 maven-jar-plugin 打jar包配置

  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-jar-plugin</artifactId>  
  4.     <configuration>  
  5.         <excludes>  
  6.             <exclude>**/config/*</exclude>  
  7.         </excludes>  
  8.     </configuration>  
  9. </plugin>  

5 maven-shade-plugin 把依赖的lib打包到一个jar

  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-shade-plugin</artifactId>  
  4.     <version>2.2</version>  
  5.     <executions>  
  6.         <execution>  
  7.             <phase>package</phase>  
  8.             <goals>  
  9.                 <goal>shade</goal>  
  10.             </goals>  
  11.             <configuration>  
  12.                 <artifactSet>  
  13.                     <includes>  
  14.                         <include>thirdparty.mysql:mysql-connector-java</include>  
  15.                     </includes>  
  16.                 </artifactSet>  
  17.             </configuration>  
  18.         </execution>  
  19.     </executions>  
  20. </plugin>  

6 maven-war-plugin web工程需要这个

  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-war-plugin</artifactId>  
  4.     <configuration>  
  5.         <webappDirectory>../out</webappDirectory>  
  6.         <warSourceDirectory>webapp</warSourceDirectory>  
  7.     </configuration>  
  8. </plugin>  

7 maven-assembly-plugin 强大的归档利器
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-assembly-plugin</artifactId>  
  4.     <version>2.2.1</version>  
  5.     <configuration>  
  6.         <descriptors>  
  7.             <descriptor>src/main/assembly/assembly.xml</descriptor>  
  8.         </descriptors>  
  9.         <outputDirectory>${basedir}/deploy</outputDirectory>  
  10.     </configuration>  
  11.     <executions>  
  12.         <execution>  
  13.             <id>make-assembly</id>  
  14.             <phase>package</phase>  
  15.             <goals>  
  16.                 <goal>single</goal>  
  17.             </goals>  
  18.         </execution>  
  19.     </executions>  
  20. </plugin>  

其中, assembly.xml 文件定义了内部各个目录的资源生成策略
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <assembly>  
  3.     <id>distribution</id>  
  4.     <formats>  
  5.         <format>dir</format>  
  6.     </formats>  
  7.   
  8.     <includeBaseDirectory>false</includeBaseDirectory>  
  9.     <fileSets>  
  10.         <fileSet>  
  11.             <directory>${project.build.directory}</directory>  
  12.             <outputDirectory>/lib</outputDirectory>  
  13.             <includes>  
  14.                 <include>${project.artifactId}-${project.version}.jar</include>  
  15.             </includes>  
  16.         </fileSet>  
  17.         <fileSet>  
  18.             <directory>${project.build.directory}/lib</directory>  
  19.             <outputDirectory>/lib</outputDirectory>  
  20.         </fileSet>  
  21.         <fileSet>  
  22.             <directory>${basedir}/src/main/assembly/dist/</directory>  
  23.             <outputDirectory>/</outputDirectory>  
  24.             <includes>  
  25.                 <include>**</include>  
  26.             </includes>  
  27.         </fileSet>  
  28.         <fileSet>  
  29.             <directory>${basedir}/src/main/assembly/dist/bin</directory>  
  30.             <outputDirectory>/bin</outputDirectory>  
  31.             <includes>  
  32.                 <include>**</include>  
  33.             </includes>  
  34.             <fileMode>0755</fileMode>  
  35.         </fileSet>  
  36.   
  37.         <fileSet>  
  38.             <directory>${basedir}/../other-module/src/main/resources/</directory>  
  39.             <outputDirectory>/etc</outputDirectory>  
  40.             <includes>  
  41.                 <include>**</include>  
  42.             </includes>  
  43.         </fileSet>  
  44.     </fileSets>  
  45. </assembly>  


来自Apache的完整插件列表在:http://maven.apache.org/plugins/index.html

来自Codehaus的完整插件列表在:http://mojo.codehaus.org/plugins.html
posted @ 2017-07-30 11:18  Tony.J  阅读(917)  评论(0编辑  收藏  举报