Maven插件使用记录

1.tomcat-maven-plugin

用途: 可将windows环境下开发的webapp部署到linux服务器上的tomcat下。

用法: http://jiessiedyh.iteye.com/blog/471066

2.maven-antrun-plugin

用途: 运行 Ant 任务。

实际应用:

1) 打包时包括hbm文件

用法: http://blog.csdn.net/symgdwyh/archive/2009/07/30/4393962.aspx

2) 启动H2数据库

用法:

pom.xml中配置片段:

	<profiles>
		<profile>
			<id>startdb</id>
			<build>
				<plugins>
					<plugin>
						<groupId>org.apache.maven.plugins</groupId>
						<artifactId>maven-antrun-plugin</artifactId>
						<executions>
							<execution>
								<id>startdb</id>
								<phase>initialize</phase>
								<configuration>
									<tasks>
										<java classname="org.h2.tools.Console" classpathref="maven.plugin.classpath" fork="true" spawn="true">
										</java>
									</tasks>
								</configuration>
								<goals>
									<goal>run</goal>
								</goals>
							</execution>
						</executions>

						<dependencies>
							<dependency>
								<groupId>com.h2database</groupId>
								<artifactId>h2</artifactId>
								<version>${h2.version}</version>
							</dependency>
						</dependencies>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

  运行 mvn initialize -Pstartdb 即可

3.maven-source-plugin

用途: 打包源代码。

用法: http://blog.csdn.net/symgdwyh/archive/2009/08/04/4407945.aspx

4.findbugs-maven-plugin

用途: 检查代码。

用法:

pom.xml中配置片段: 

<reporting>
	<plugins>
		<!-- FindBugs插件 -->
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>findbugs-maven-plugin</artifactId>
			<version>2.0.1</version>
			<configuration>
				<effort>Max</effort>
				<threshold>Low</threshold>
				<xmlOutput>true</xmlOutput>
				<xmlOutputDirectory>target</xmlOutputDirectory>
				<findbugsXmlOutput>true</findbugsXmlOutput>
				<findbugsXmlOutputDirectory>target</findbugsXmlOutputDirectory>
			</configuration>
		</plugin>
	</plugins>
</reporting>

运行 mvn site 命令即可。

5. maven-compiler-plugin

用途: 编译源代码

用法: http://maven.apache.org/plugins/maven-compiler-plugin/usage.html

几个例子: http://maven.apache.org/plugins/maven-compiler-plugin/examples/

pom.xml中配置片段: 

<project>
  ...
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.0</version>
          <configuration>
            <!-- source,target默认值为1.5 -->
	    <source>1.5</source>
	    <target>1.5</target>
            <encoding>UTF-8</encoding>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  ...
</project>

6.maven-dependency-plugin

用途: copy依赖包

用法: http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

几个例子: http://maven.apache.org/plugins/maven-dependency-plugin/examples/

pom.xml中配置片段: 

<project>
  ...
  <build>
      <plugins>
			<!-- 打包时将所依赖的jar包copy到指定路径下 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.6</version>
				<executions>
					<execution>
						<id>copy-dependencies</id>
						<phase>package</phase>
						<goals>
							<goal>copy-dependencies</goal>
						</goals>
						<configuration>
							<!-- 设置依赖包copy目的地,默认目的地为${project.build.directory}/dependency -->
							<outputDirectory>${project.build.directory}/dependency/jars</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
       </plugins>
  </build>
  ...
</project>

7.maven-surefire-plugin

用途: 运行单元测试

默认情况下,maven-surefire-plugin会自动执行测试源码路径下所有符合下列命名规约的测试类:
**/Test*.java:任何子目录下以Test开头的Java类
**/*Test.java:任何子目录下以Test结尾的Java类
**/*TestCase.java:任何子目录下所有命名以TestCase结尾的Java类

用法: http://maven.apache.org/surefire/maven-surefire-plugin/usage.html

pom.xml中配置片段: 

<project>
  [...]
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.2</version>
	  <configuration>
		[...]
		<excludes>
			<exclude>a/b/c/XxxTest.java</exclude>
		</excludes>
	  </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  [...]
</project>

JUnit, TestNG  testcase 不能同时运行的解决办法

增加 execution 配置:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<configuration>
		<!-- 加上下面这2段配置,则可以只运行JUnit testcase(下面的execution配置要去掉) -->
		<testNGArtifactName>none:none</testNGArtifactName>
		<properties>
			<property>
				<name>junit</name>
				<value>true</value>
			</property>
		</properties>
	</configuration>
	<executions>
		<!-- 加上下面这段execution,则可以同时运行JUnit and TestNG testcase -->
		<execution>
			<phase>test</phase>
			<goals>
				<goal>test</goal>
			</goals>
			<configuration>	
				<junitArtifactName>none:none</junitArtifactName>
				<testNGArtifactName>org.testng:testng</testNGArtifactName>
			</configuration>
		</execution>
	</executions>
</plugin>

8. cobertura-maven-plugin

用途: 单元测试覆盖率统计

用法: http://mojo.codehaus.org/cobertura-maven-plugin/usage.html

pom.xml中配置片段: 

<project>
  ...  
  <reporting>
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.5.2</version>
      </plugin>
    </plugins>
  </reporting>
</project>
 单独运行: mvn cobertura:cobertura

运行完毕后,通过 target/site/cobertura/index.html 查看结果。

运行完毕后,通过 target/site/cobertura/index.html 查看结果。

9. emma-maven-plugin

用途: 单元测试覆盖率统计

用法: http://mojo.codehaus.org/emma-maven-plugin/usage.html

pom.xml中配置片段: 

<project>
  ...
  <reporting>
    ...
    <plugins>
      ...
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>emma-maven-plugin</artifactId>
        <version>1.0-alpha-3</version>
      </plugin>
      ...
    </plugins>
    ...
  </reporting>
  ...
</project>
 单独运行: mvn emma:emma 运行完毕后,通过 target/site/emma/index.html 查看结果。  

10. maven-assembly-plugin

用途: 打包发布包

用法: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

pom.xml中配置片段(创建可执行jar): 

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         -->
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <!-- 指定输出文件名 -->
          <finalName>${project.name}-${project.version}_all-in-one</finalName>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
             <mainClass>xxx.Xxx</mainClass>
            </manifest>
          </archive>
        </configuration>
          
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase><!-- 绑定到package生命周期阶段上 -->
            <goals>
             <goal>assembly</goal>
            </goals>
          </execution>
        </executions>
        [...]
</project>
运行: mvn assembly:assembly  或  mvn package

运行完毕后,target 下会生成结果。

 Maven Assembly插件介绍

使用maven插件对java工程进行打包

 

 

运行完毕后,target 下会生成结果。  Maven Assembly插件介绍 使用maven插件对java工程进行打包    

猜你喜欢

转载自coffeelover.iteye.com/blog/509379