maven 常用插件

maven插件是用来完成构建任务的。用户可以通过两种方式调用Maven插件。
第一种方式是将插件与生命周期绑定,如命令mvn compile就实现了maven-compiler-plugin的调用目标。
第二种方式是直接在命令行指定要执行的插件,如mvn archetype:generate 就表示调用maven-archetype-plugin。

1.maven-assembly-plugin

用于导出jar包或war包。插件官网: http://maven.apache.org/plugins/maven-assembly-plugin/

具体打包哪些文件是高度可控的,在assembly.xml中指定。

<!--一个assembly.xml例子-->
<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <formats>
        <format>jar</format>
    </formats>
    <id>with-dependencies</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <excludes>
                <exclude>org.apache.storm:storm-core</exclude>
            </excludes>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>


2.maven-source-plugin

打包时用的,附带源代码。pom写法见下:
 <plugin>  
       <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> 


3.maven-dependency-pligin

打包时用。用于把项目的依赖单独放到 jar外 的一个目录中。
<plugin>  
	<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>
			 </configuration>
		</execution>  
	</executions>  
</plugin>

4.maven-enforcer-plugin

当一个旧的库被很多库间接依赖,挨个使用<excluds>不简洁时,可使用这个插件强制排除过时的依赖。有些框架升级时artifactid都会变,比如spring升级为spring-core。
<!-- 排除包 -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-enforcer-plugin</artifactId>
	<version>1.1</version>
	<executions>
		<execution>
			<id>enforce-denpendency</id>
			<phase>compile</phase>
			<goals>
				<goal>enforce</goal>
			</goals>
			<configuration>
				<rules>
					<bannedDependencies>
						<!-- groupId[:artifactId][:version][:type][:scope] -->
						<excludes>
							<!--<exclude>ch.qos.logback</exclude> -->
							<exclude>org.slf4j:log4j-over-slf4j</exclude>
							<exclude>org.springframework:spring</exclude>
						</excludes>
					</bannedDependencies>
				</rules>
				<fail>true</fail>
			</configuration>
		</execution>
	</executions>
</plugin>




上一篇:Eclipse上安装Web Service插件


下一篇:常用Maven插件介绍