常用maven插件总结-joed
2017-08-04 11:25
309 查看
1。官方插件地址
http://maven.apache.org/plugins/index.html
2。maven常用插件汇总
2.1 编译插件需要配置编码<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <encoding>UTF8</encoding> </configuration> </plugin> </plugins>
2.2需要在编译和生成的时候使用不同的jdk版本
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.6</source> <target>1.7</target> </configuration> </plugin>
2.3Tomcat插件
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> </configuration> </plugin>
2.4 打包插件maven-war-plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <webResources> <resource> <directory>src/main/webapp</directory> <excludes> <exclude>**/*.jpg</exclude> </excludes> </resource> </webResources> </configuration> </plugin>
2.5 打包时生成源码包插件maven-source-plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.2</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin>
2.6源代码打包插件
<plugin> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <!-- <finalName>${project.build.name}</finalName> --> <attach>true</attach> <encoding>${project.build.sourceEncoding}</encoding> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
2.7生成javadoc包插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin>
2.8 maven-assembly-plugin 它支持各种打包文件格式,包括zip、tar.gz、tar.bz2等等,通过一个打包描述文件(该例中是src/main/assembly.xml),它能够帮助用户选择具体打包哪些文件集合、依赖、模块、和甚至本地仓库文件,每个项的具体打包路径用户也能自由控制。如下就是对应上述需求的打包描述文件src/main/assembly.xml
<assembly> <id>bin</id> <formats> <format>zip</format> </formats> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> <fileSets> <fileSet> <outputDirectory>/</outputDirectory> <includes> <include>README.txt</include> </includes> </fileSet> <fileSet> <directory>src/main/scripts</directory> <outputDirectory>/bin</outputDirectory> <includes> <include>run.sh</include> <include>run.bat</include> </includes> </fileSet> </fileSets> </assembly> 最终生成一个zip格式的分发包,它包含如下的一个结构: bin/ lib/ README.txt 最后,我们需要配置maven-assembly-plugin使用打包描述文件,并绑定生命周期阶段使其自动执行打包操作: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2.1</version> <configuration> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 运行mvn clean package之后,我们就能在target/目录下得到名为hello-world-1.0-bin.zip的分发包了。
2.9 maven-surefire-plugin 打包时跳过单元测试
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.6</version> <configuration> <skip>true</skip> </configuration> </plugin> mvn package -Dmaven.test.skip=true 如果单元测试中有输出中文,eclipse的控制台里中文可能会变成乱码输出,也可以通过这个插件解决,参考配置: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.16</version> <configuration> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> </plugin>
2.10 maven-resource-plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>compile</phase> </execution> </executions> <configuration> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin>
2.11 把web项目的输出copy到tomcat的webapp下
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>deploy-website</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${server_home}/webapps/${project.build.finalName}</outputDirectory> <resources> <resource> <directory>${project.build.directory}/${project.build.finalName}</directory> </resource> </resources> </configuration> </execution> </executions> </plugin>
2.12 maven-dependency-plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-dependencies</id> <phase>compile</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- ${project.build.directory}为Maven内置变量,缺省为target --> <outputDirectory>${project.build.directory}/lib</outputDirectory> <!-- 表示是否不包含间接依赖的包 --> <excludeTransitive>false</excludeTransitive> <!-- 表示复制的jar文件去掉版本信息 --> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin> 在部署war包时,需要将项目依赖的jar包,也打到war包中,因此就会用到上述插件
2.13 在打包时,需要清空一些指定的目录
在一个J2EE项目中,想使用mvn clean命令清除target里的内容的同时,也清除tomcat/webapp下的相应目录,该怎么办呢?
<plugin> <artifactId>maven-clean-plugin</artifactId> <configuration> <verbose>true</verbose> <filesets> <fileset> <directory>c:/a/b/c/</directory> </fileset> </filesets> </configuration> </plugin> 本例中,删除的是e:/test/zbw/c/目录. 当用户在该maven项目中执行mvn clean后,除了删除clean插件默认的 project.build.directory project.build.outputDirectory project.build.testOutputDirectory project.reporting.outputDirectory e:/test/zbw/c/
2.14 利用tomcat-maven-plugin插件将项目自动打包并部署到tomcat中
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <configuration> <server>tomcat6-manager</server> <path>/${project.build.name}</path> <url>http://localhost:8080/manager</url> <username>admin</username> <password>admin</password> </configuration> <executions> <execution> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> </plugins> path: 是指项目部署到tomcat后的项目名称 url: 是指tomcat的manager访问地址 server: 这个是tomcat服务名称设置,需要配置maven的settings.xml文件,在servers节点中手动配置server,如下所示: <server> <id>tomcat6-manager</id> <username>admin</username> <password>admin</password> </server>
2.15 利用cargo-maven2-plugin插件将项目自动打包并部署到tomcat中
cargo插件可以帮助你完成WAR包到服务器的部署及服务器的启动和关闭等工作,方便,快速! <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.2.0</version> <configuration> <container> <containerId>${server_name}</containerId> <home>${server_home}</home> </container> <configuration> <type>existing</type> <home>${server_home}</home> <properties> <cargo.servlet.port>8088</cargo.servlet.port> </properties> </configuration> </configuration> </plugin> 注意,如果你的tomcat服务器的端口使用的不是默认的8080(如本例中的8088),则需要使用cargo.servlet.port参数将cargo的监听端口也配置到tomcat的那个监听端口(如本例中的8088),否则使用mvn cargo:run启动的服务器会在120000毫秒(120秒)后自动关闭! mvn cargo:start命令完成WAR包部署后,启动服务器,然后会将服务器立即关掉; mvn cargo:run命令完成WAR包部署后,启动服务器,直到你Ctrl+C将服务器关掉; mvn cargo:stop命令关闭服务器。 参考:http://cargo.codehaus.org/Maven2+plugin <plugin> <!-- 指定插件名称及版本号 --> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.2.3</version> <!-- 插件的Tomcat6.x配置 --> <configuration> <!-- 容器的配置 --> <container> <!-- 指定服务器版本 --> <containerId>tomcat6x</containerId> <!-- 指定服务器的安装目录 --> <home>E:\Program Files\tomcat-6.0.32</home> </container> <!-- 具体的配置 --> <configuration> <!-- 部署模式:existing、standalone等 --> <type>existing</type> <!-- Tomcat的位置,即catalina.home --> <home>E:\Program Files\tomcat-6.0.32</home> <!-- 配置属性 --> <properties> <!-- 管理地址 --> <cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url> <!-- Tomcat用户名 --> <cargo.remote.username>admin</cargo.remote.username> <!-- Tomcat密码 --> <cargo.remote.password>admin</cargo.remote.password> <!-- <cargo.jvmargs> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 </cargo.jvmargs> --> </properties> </configuration> </configuration> </plugin>
相关文章推荐
- 常用maven插件总结
- Maven学习总结:几个常用的maven插件
- maven学习总结:几个常用的maven插件
- maven常用插件总结
- 【干货】常用maven插件总结
- Maven学习总结:几个常用的maven插件
- Maven学习总结:几个常用的maven插件
- 常用maven插件总结
- maven常用插件总结
- maven 常用插件总结
- Maven学习总结:几个常用的maven插件
- Maven学习总结(18)——Maven常用插件介绍
- maven常用插件总结
- Maven学习总结(22)——Maven常用插件介绍
- Maven学习总结(22)——Maven常用插件介绍
- 常用Maven插件介绍
- maven 常用插件
- InfoQ Maven专栏(七)——常用Maven插件介绍(上)
- Myeclipse学习总结(4)——Eclipse常用开发插件
- Maven学习总结(42)——Maven多模块构建中常用的参数