Plugin是Maven的核心,所有功能的实现是通过插件完成。Maven提供了很多插件,也有第三方提供。参考:http://maven.apache.org/plugins/index.html,几个比较常用的插件:
Maven Archetype Plugin用来创建maven项目的模板即archetype,也可以从一个项目创建一个archtype,
参考:http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html
archetype:createDeprecated.Please use the generate mojo instead.
archetype:create-from-projectCreates an archetype from the current project.
archetype:generateGenerates a new project from an archetype.
archetype:helpDisplay help information on maven-archetype-plugin. Call
mvn archetype:help -Ddetail=true -Dgoal=
to display parameter details.
mvn archetype:create \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-webapp \
-DgroupId=com.mycompany.app \
-DartifactId=my-webapp
mvn archetype:create \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-webapp \
-DgroupId=com.mycompany.app \
-DartifactId=my-webapph4.3.创建复杂的maven项目
mvn archetype:generate -DarchetypeCatalog=internal
通过指定archetypeCatalog参数,从指定的参数中选择需要创建的项目,参数定义见参考。如:
E:\workspaces>mvn archetype:generate -DarchetypeCatalog=internal
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.0-alpha-5:generate (default-cli) @ standalon
e-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.0-alpha-5:generate (default-cli) @ standalon
e-pom <<<
[INFO]
[INFO] — maven-archetype-plugin:2.0-alpha-5:generate (default-cli) @ standalon
e-pom —
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.
archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web applicati
on with Hibernate, Spring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web applic
ation with Hibernate, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web applic
ation with Hibernate, Spring and Struts 2)
4: internal -> appfuse-basic-tapestry (AppFuse archetype for creating a web appl
ication with Hibernate, Spring and Tapestry 4)
5: internal -> appfuse-core (AppFuse archetype for creating a jar application wi
th Hibernate and Spring and XFire)
6: internal -> appfuse-modular-jsf (AppFuse archetype for creating a modular app
lication with Hibernate, Spring and JSF)
7: internal -> appfuse-modular-spring (AppFuse archetype for creating a modular
application with Hibernate, Spring and Spring MVC)
8: internal -> appfuse-modular-struts (AppFuse archetype for creating a modular
application with Hibernate, Spring and Struts 2)
9: internal -> appfuse-modular-tapestry (AppFuse archetype for creating a modula
r application with Hibernate, Spring and Tapestry 4)
10: internal -> makumba-archetype (Archetype for a simple Makumba application)
11: internal -> maven-archetype-j2ee-simple (A simple J2EE Java application)
12: internal -> maven-archetype-marmalade-mojo (A Maven plugin development proje
ct using marmalade)
13: internal -> maven-archetype-mojo (A Maven Java plugin development project)
14: internal -> maven-archetype-portlet (A simple portlet application)
15: internal -> maven-archetype-profiles ()
16: internal -> maven-archetype-quickstart ()
17: internal -> maven-archetype-site-simple (A simple site generation project)
18: internal -> maven-archetype-site (A more complex site project)
19: internal -> maven-archetype-webapp (A simple Java web application)
20: internal -> data-app (A new Databinder application with sources and resource
s.)
21: internal -> camel-archetype-component (Creates a new Camel component)
22: internal -> camel-archetype-activemq (Creates a new Camel project that confi
gures and interacts with ActiveMQ)
23: internal -> camel-archetype-java (Creates a new Camel project using Java DSL
)
24: internal -> camel-archetype-scala (Creates a new Camel project using Scala D
SL)
25: internal -> camel-archetype-spring (Creates a new Camel project with added S
pring DSL support)
26: internal -> camel-archetype-war (Creates a new Camel project that deploys th
e Camel Web Console, REST API, and your routes as a WAR)
27: internal -> jini-service-archetype (Archetype for Jini service project creat
ion)
28: internal -> jbosscc-seam-archetype (Maven Archetype to generate a Seam Appli
cation- Documentation)
29: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype)
30: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence)
Archetype)
31: internal -> softeu-archetype-jsf (JSF+Facelets Archetype)
32: internal -> jpa-maven-archetype (JPA application)
33: internal -> spring-osgi-bundle-archetype (Spring-OSGi archetype)
34: internal -> bamboo-plugin-archetype (Atlassian Bamboo plugin archetype)
35: internal -> confluence-plugin-archetype (Atlassian Confluence plugin archety
pe)
36: internal -> jira-plugin-archetype (Atlassian JIRA plugin archetype)
37: internal -> maven-archetype-har (Hibernate Archive)
38: internal -> maven-archetype-sar (JBoss Service Archive)
39: internal -> wicket-archetype-quickstart (A simple Apache Wicket project)
40: internal -> quickstart (A simple Apache Tapestry 5 Project)
41: internal -> scala-archetype-simple (A simple scala project)
42: internal -> lift-archetype-blank (A blank/empty liftweb project)
43: internal -> lift-archetype-basic (The basic (liftweb) project)
44: internal -> cocoon-22-archetype-block-plain (http://cocoon.apache.org/2.2/ma
ven-plugins/)
45: internal -> cocoon-22-archetype-block (http://cocoon.apache.org/2.2/maven-pl
ugins/)
46: internal -> cocoon-22-archetype-webapp (http://cocoon.apache.org/2.2/maven-p
lugins/)
47: internal -> myfaces-archetype-helloworld (A simple archetype using MyFaces)
48: internal -> myfaces-archetype-helloworld-facelets (A simple archetype using
MyFaces and facelets)
49: internal -> myfaces-archetype-trinidad (A simple archetype using Myfaces and
Trinidad)
50: internal -> myfaces-archetype-jsfcomponents (A simple archetype for create c
ustom JSF components using MyFaces)
51: internal -> gmaven-archetype-basic (Groovy basic archetype)
52: internal -> gmaven-archetype-mojo (Groovy mojo archetype)
53: internal -> struts2-archetype-starter (Struts 2 Starter Archetype)
54: internal -> tynamo-archetype (Creates a CRUD web application based on Tapest
ry 5 and Tynamo, a model-driven Java web framework)
55: internal -> tellurium-junit-archetype (Tellurium JUnit project archetype for
the Tellurium Automated Testing Framework)
56: internal -> tellurium-testng-archetype (Tellurium TestNG project archetype f
or the Tellurium Automated Testing Framework)
57: internal -> scalate-archetype-empty (Generates a Scalate empty web applicati
on)
58: internal -> scalate-archetype-guice (Generates a Scalate Jog web application
)
Choose a number: 16: 2
Define value for property 'groupId': : cn.bidlink
Define value for property 'artifactId': : srpingMVN
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': cn.bidlink: : jar
Confirm properties configuration:
groupId: cn.bidlink
artifactId: srpingMVN
version: 1.0-SNAPSHOT
package: jar
Y: : Y
通过指定'groupId','artifactId','version','package'完成项目的创建。
Maven Eclipse Plugin 用来创建Eclipse的项目文件((*.classpath, *.wtpmodules and the .settings)。
参考:http://maven.apache.org/plugins/maven-eclipse-plugin/index.html
eclipse:configure-workspaceis used to add the classpath variable M2_REPO to Eclipse which points to your local repository and optional to configure other workspace features.
eclipse:eclipsegenerates the Eclipse configuration files.
eclipse:cleanis used to delete the files used by the Eclipse IDE.
eclipse:myeclipseGenerates the MyEclipse configuration files.
eclipse:myeclipse-cleanDeletes the config files used by MyEclipse.
eclipse:help:Display help information on maven-eclipse-plugin.
mvn eclipse:configure-workspace \
-Declipse.workspace=testworkspace \
-Declipse.workspaceCodeStylesURL=http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-eclipse-plugin/src/optional/eclipse-config/maven-styles.xml
(或者mvn -Declipse.workspace=
在.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.core.prefs中会创建:
#Fri Nov 12 13:45:10 CST 2010
org.eclipse.jdt.core.classpathVariable.M2_REPO=C\:
Documents and Settings\\Administrator\\.m2
repository
并且将workspaceCodeStylesURL指定的code styles format文件存放在org.eclipse.jdt.ui.prefs中。
2.1 Load Coding Styles into Workspace
mvn eclipse:eclipse -Declipse.projectDir=. \
-Declipse.workspaceCodeStyleURL=http://svn.apache.org/repos/asf/maven/plugins/trunk/maven-eclipse-plugin/src/optional/eclipse-config/maven-styles.xml
或者在pom.xml中配置
2.2 Configure Checkstyle
]]>
2.3 Multpile Module Project
2.3.1 垂直的工程结构
mvn archetype:create -DgroupId=cn.bidlink -DartifactId=myproject
cd myproject
mvn archetype:create -DgroupId=cn.bidlink -DartifactId=myproject-site
mvn archetype:create -DgroupId=cn.bidlink.core -DartifactId=myproject-core
mvn archetype:create
-DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
-DgroupId=cn.bidlink -DartifactId=myproject-webapp
mvn install
mvn eclipse:eclipse
2.3.2扁平的工程结构
注意:扁平的工程结构有很多maven plguin 如:release插件不支持。
2.4 Help
通过运行mvn eclipse:help -Ddetail=true -Dgoal=
Maven compiler plugin用来对maven工程源代码进行编译,默认是采用javac,source 和target均采用java1.5版本。
参考:http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html
compiler:compileCompiles application sources
compiler:helpDisplay help information on maven-compiler-plugin. Call
mvn compiler:help -Ddetail=true -Dgoal=
to display parameter details.
compilerplugin
Xodoclet plugin用来创建hibernate的pojo对应的mapping文件
参考:http://xdoclet.codehaus.org/Maven2+Plugin
http://www.samaxes.com/2006/07/using-maven-2-xdoclet-2-and-hibernate-3/
Goals:xdoclet
Usage:
配置如下:
pluginhibernate
Maven source plugin 用来对代码源文件进行打包,打包后的source代码可以发布到资源库,在jar包被用到时可以一同被下载到本地,在eclipse工具中可以方便查看源代码。
参考:http://maven.apache.org/plugins/maven-source-plugin/jar-mojo.html
source:aggregateAggregate sources for all modules in an aggregator project.
source:jarThis plugin bundles all the sources into a jar archive.
source:helpDisplay help information on maven-source-plugin. Call
mvn source:help -Ddetail=true -Dgoal=
to display parameter details.
Javadoc plugin通过使用javadoc工具给工程创建javadocs
参考:http://maven.apache.org/plugins/maven-javadoc-plugin/
...
...
...
或者
...
...
...
...
SCM(Source Control Manager)plugin是maven提供的可以结合版本控制工具(CVS,SVN等)使用的插件。
配置scm链接后,可以在插件中引用
配置好cvs,username,password等后可以通过执行scm的相关目标命令。如:
mvn scm:bootstrap 从cvs下载代码并执行install操作
2.1显示cvs提交修改记录
mvn -DstartDate=YYYY-MM-DD -DendDate=YYYY-MM-DD scm:changelog
2.2显示不同修改版本之间的不同
mvn -DstartRevision=
2.3打tag标记
mvn -Dtag="
2.4验证scm配置的正确
mvn -DscmConnection="
2.5通过命令执行bootstrap,对部署项目很方便
mvn -DconnectionUrl=scm:cvs:sspi:@9bb5e6546229466:2401:/repository:guide-ide-eclipse -Dmaven.scm.provider.cvs.implementation=cvs_native -Dpassword=1q2w3e -Dbasedir=. -DcheckoutDirectory=guide-ide-eclipse -Dgoals=install -DgoalsDirectory=parent scm:bootstrap
Antrun plugin提供了执行ant中的task的功能,可以将ant的task放在pom中。
antrun:helpDisplay help information on maven-antrun-plugin. Call
mvn antrun:help -Ddetail=true -Dgoal=
to display parameter details.
antrun:runMaven AntRun Mojo. This plugin provides the capability of calling Ant tasks from a POM by running the nested ant tasks inside the
其中build.xml定义如下:
Dependency Plugin提供了对项目依赖的artifacts的操作管理。
mvn dependency:tree -Dverbose -Dincludes=commons-collections
将看到
[INFO] [dependency:tree]
[INFO] org.apache.maven.plugins:maven-dependency-plugin:maven-plugin:2.0-alpha-5-SNAPSHOT
[INFO] +- org.apache.maven.reporting:maven-reporting-impl:jar:2.0.4:compile
[INFO] | - commons-validator:commons-validator:jar:1.2.0:compile
[INFO] | - commons-digester:commons-digester:jar:1.6:compile
[INFO] | - (commons-collections:commons-collections:jar:2.1:compile - omitted for conflict with 2.0)
[INFO] - org.apache.maven.doxia:doxia-site-renderer:jar:1.0-alpha-8:compile
[INFO] - org.codehaus.plexus:plexus-velocity:jar:1.1.3:compile
[INFO] - commons-collections:commons-collections:jar:2.0:compile
可以很清楚看到相关冲突信息
Assembly 插件提供了手动对某些文件和依赖打成各种包格式的功能。
assembly:helpDisplay help information on maven-assembly-plugin. Call
mvn assembly:help -Ddetail=true -Dgoal=
assembly:singleAssemble an application bundle or distribution from an assembly descriptor. This goal is suitable either for binding to the lifecycle or calling directly from the command line (provided all required files are available before the build starts, or are produced by another goal specified before this one on the command line).
<assemblyxmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>space-ws-jarid>
<includeBaseDirectory>falseincludeBaseDirectory>
<formats>
<format>jarformat>
formats>
<fileSets>
<fileSet>
<outputDirectory>/outputDirectory>
<directory>
${project.build.outputDirectory}
directory>
<includes>
<include>cn/bidlink/space/webservice/*.classinclude>
<include>cn/bidlink/space/follower/dto/*.classinclude>
<include>cn/bidlink/space/myspace/dto/*.classinclude>
<include>cn/bidlink/space/notice/dto/*.classinclude>
includes>
fileSet>
fileSets>
assembly>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>2.2.1version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/space-ws-jar.xmldescriptor>
descriptors>
<appendAssemblyId>falseappendAssemblyId>
<finalName>${space.ws.name}finalName>
configuration>
<executions>
<execution>
<id>make-space-ws-jarid>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
3)运行命令:mvn assembly:single就可以将制定文件达成jar包了。
usage: mvn [options] [
Options:
am,-also-make If project list is specified, also
build projects required by the
list
amd,-also-make-dependents If project list is specified, also
build projects that depend on
projects on the list
B,-batch-mode Run in non-interactive (batch)
mode
C,-strict-checksums Fail the build if checksums don't
match
c,-lax-checksums Warn if checksums don't match
cpu,-check-plugin-updates Ineffective, only kept for
backward compatibility
D,-define
e,-errors Produce execution error messages
emp,-encrypt-master-password
ep,-encrypt-password
f,-file Force the use of an alternate POM
file.
fae,-fail-at-end Only fail the build afterwards;
allow all non-impacted builds to
continue
ff,-fail-fast Stop at first failure in
reactorized builds
fn,-fail-never NEVER fail the build, regardless
of project result
gs,-global-settings
settings file
h,-help Display help information
l,-log-file
will go.
N,-non-recursive Do not recurse into sub-projects
npr,-no-plugin-registry Ineffective, only kept for
backward compatibility
npu,-no-plugin-updates Ineffective, only kept for
backward compatibility
nsu,-no-snapshot-updates Supress SNAPSHOT updates
o,-offline Work offline
P,-activate-profiles
to activate
pl,-projects
instead of all projects. A project
can be specified by
[groupId]:artifactId or by its
relative path.
q,-quiet Quiet output - only show errors
rf,-resume-from
project
s,-settings
settings file
T,-threads
where C is core multiplied
t,-toolchains
toolchains file
U,-update-snapshots Forces a check for updated
releases and snapshots on remote
repositories
up,-update-plugins Ineffective, only kept for
backward compatibility
V,-show-version Display version information
WITHOUT stopping build
v,-version Display version information
X,-debug Produce execution debug output
分析eoss-base的dependency,打开debug信息,并且输出到log.txt文件中
E:\workspaces\eossmvn\eoss-core>mvn dependency:tree -Dverbose -Dincludes=eoss-ba
se -X -l log.txt
使用线下编译方式,不从远程资源库下载包
mvn -o compile