1. 程式人生 > >Maven深入理解

Maven深入理解

Plugins

Plugin是Maven的核心,所有功能的實現是通過外掛完成。Maven提供了很多外掛,也有第三方提供。參考:,幾個比較常用的外掛:

  1. Maven Archetype Plugin
  2. Maven Eclipse Plugin
  3. Maven Compiler Plugin
  4. Maven xdoclet Plugin
  5. Maven Source Plugin
  6. Maven Javadoc Plugin
  7. Maven SCM
  8. Maven AntRun Plugin
  9. Maven Dependency Plugin
  10. Maven Release Plugin
  11. Maven Assembly Plugin

Maven Archetype Plugin

Goals:

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=<goal-name>
to display parameter details.

Usage:

建立簡單的maven專案

mvn archetype:create \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-webapp \
-DgroupId=com.mycompany.app \
-DartifactId=my-webapp

建立簡單的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 (
ven-plugins/)
45: internal -> cocoon-22-archetype-block (
ugins/)
46: internal -> cocoon-22-archetype-webapp (
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

Goals:

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:helpDisplay help information on maven-eclipse-plugin.

Usage:

在Elcipse使用本地的maven資源庫,通過如下命令將M2_REPO變數加入到eclipse

mvn eclipse:configure-workspace \
-Declipse.workspace=testworkspace \
-Declipse.workspaceCodeStylesURL=
(或者mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo)
在.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中。

建立eclipse專案配置檔案,如:.projectand .classpath files 等

2.1 Load Coding Styles into Workspace
mvn eclipse:eclipse -Declipse.projectDir=. \
-Declipse.workspaceCodeStyleURL=
或者在pom.xml中配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<workspace>${basedir}</workspace>
<workspaceCodeStylesURL></workspaceCodeStylesURL>
</configuration>
</plugin>
2.2 Configure Checkstyle
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<additionalBuildcommands>
<buildcommand>com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder</buildcommand>
</additionalBuildcommands>
<additionalProjectnatures>
<projectnature>com.atlassw.tools.eclipse.checkstyle.CheckstyleNature</projectnature>
</additionalProjectnatures>
<additionalConfig>
<file>
<name>.checkstyle</name>
<content>
<![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true">
<fileset name="all" enabled="true" check-config-name="<YOUR CHECKSTYLE CONFIG NAME HERE>" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
<filter name="NonSrcDirs" enabled="true"/>
</fileset-config>
]]>
</content>
</file>
</additionalConfig>
</configuration>
</plugin>

2.3 Multpile Module Project
2.3.1 垂直的工程結構

  1. 在eclipse中設定MO_REPO classpath變數
  2. 開啟window命令視窗,進入workspace目錄
  3. 通過命令建立一個maven父工程

mvn archetype:create -DgroupId=cn.bidlink -DartifactId=myproject

  1. 刪除src目錄,修改pom.xml檔案中的packaing為pom:<packaging>pom</packaging>
  2. 在父工程下建立多個maven子工程

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

  1. 父工程自動將子工程新增在自己的pom.xml中:

<modules>
<module>myproject-site</module>
<module>myproject-core</module>
<module>myproject-webapp</module>
</modules>

  1. 子工程的pom中自動繼承父工程:

<parent>
<groupId>cn.bidlink</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

  1. 子工程之間的依賴,將core子工程新增到myproject-webapp中

<dependency>
<groupId>cn.bidlink.core</groupId>
<artifactId>myproject-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

  1. 建立eclipse專案工程

mvn install
mvn eclipse:eclipse

  1. 在eclispe通過import匯入myproject,將會在eclipse建立myproject-webapp,module,site等三個工程,且module中自動會應用core工程。


2.3.2扁平的工程結構

  1. 在myproject中建立一個新目錄parent,將myproject下的pom.xml檔案移到該目錄下
  2. 修改pom.xml檔案的子工程

<modules>
<module>../myproject-site</module>
<module>../myproject-core</module> <module>../myproject-webapp</module>
</modules>

  1. 修改子工程pom.xml中的parent引用

<parent>
<artifactId>myproject</artifactId>
<groupId>cn.bidlink</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
注意:扁平的工程結構有很多maven plguin 如:release外掛不支援。
2.4 Help
通過執行mvn eclipse:help -Ddetail=true -Dgoal=<goal-name>可以獲取goal的引數的詳細資訊。

Maven Compiler Plugin

Goals:

compiler:compileCompiles application sources
compiler:helpDisplay help information on maven-compiler-plugin. Call
mvn compiler:help -Ddetail=true -Dgoal=<goal-name>
to display parameter details.

Usage:

更改java版本和指定javac路徑

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>mavencompilerplugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_HOME}/bin/javac</executable>
</configuration>
</plugin>

Maven xdoclet Plugin

Xodoclet plugin用來建立hibernate的pojo對應的mapping檔案
參考:

Goals:xdoclet
Usage:
配置如下:
<plugin>
<groupId>xdoclet</groupId>
<artifactId>maven2-xdoclet2-plugin</artifactId>
<version>2.0.5</version>
<executions>
<execution>
<id>xdoclet</id>
<phase>generate-sources</phase>
<goals>
<goal>xdoclet</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>xdoclet-plugins</groupId>
<artifactId>xdocletpluginhibernate</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>nanocontainer</groupId>
<artifactId>nanocontainer</artifactId>
<version>1.0-beta-3</version>
</dependency>
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xdoclet</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>generama</groupId>
<artifactId>generama</artifactId>
<version>1.2.1</version>
<exclusions>
<exclusion>
<artifactId>freemarker</artifactId>
<groupId>freemarker</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>picocontainer</groupId>
<artifactId>picocontainer</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>nanocontainer</groupId>
<artifactId>nanocontainer-ant</artifactId>
<version>1.0-beta-3</version>
</dependency>
<dependency>
<groupId>generama</groupId>
<artifactId>qdox</artifactId>
<version>20051211.114207</version>
</dependency>
<dependency>
<groupId>commons-jelly</groupId>
<artifactId>commons-jelly</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-jelly</groupId>
<artifactId>commons-jelly-tags-define</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-jelly</groupId>
<artifactId>commons-jelly-tags-xml</artifactId>
<version>20050823.222913</version>
</dependency>
<dependency>
<groupId>commons-jexl</groupId>
<artifactId>commons-jexl</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-jelly</groupId>
<artifactId>commons-jelly-tags-jsl</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>saxpath</groupId>
<artifactId>saxpath</artifactId>
<version>1.0-FCS</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1-beta-4</version>
</dependency>
</dependencies>
<configuration>
<configs>
<config>
<components>
<component>
<classname>org.xdoclet.plugin.hibernate.HibernateMappingPlugin</classname>
<params>
<version>3.0</version>
<force>true</force>
<destdir>${project.build.outputDirectory}</destdir>
</params>
</component>
</components>
<includes>cn/bidlink/*/model/.java</includes>
</config>
</configs>
</configuration>
</plugin>

Maven Source Plugin

Goals:

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=<goal-name>
to display parameter details.

Usage:

1.指定輸出路徑及檔名稱

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<configuration>
<outputDirectory>/absolute/path/to/the/output/directory</outputDirectory>
<finalName>filename-of-generated-jar-file</finalName>
<attach>false</attach>
</configuration>
</plugin>
</plugins>
</build>

Maven Javadoc Plugin

Goals:

  1. javadoc:javadocgenerates the Javadoc files for the project. It executes the standard Javadoc tool and supports the parameters used by the tool.
  2. javadoc:test-javadocgenerates the test Javadoc files for the project. It executes the standard Javadoc tool and supports the parameters used by the tool.
  3. javadoc:aggregategenerates the Javadoc files for an aggregator project. It executes the standard Javadoc tool and supports the parameters used by the tool.
  4. javadoc:test-aggregategenerates the test Javadoc files for an aggregator project. It executes the standard Javadoc tool and supports the parameters used by the tool.
  5. javadoc:jarcreates an archive file of the generated Javadocs. It is used during the release process to create the Javadoc artifact for the project's release. This artifact is uploaded to the remote repository along with the project's compiled binary and source archive.
  6. javadoc:test-jarcreates an archive file of the generated Test Javadocs.
  7. javadoc:aggregate-jarcreates an archive file of the generated Javadocs for an aggregator project.
  8. javadoc:test-aggregate-jarcreates an archive file of the generated Test Javadocs for an aggregator project.
  9. javadoc:fixis an interactive goal which fixes the Javadoc documentation and tags for the Java files.
  10. javadoc:test-fixis an interactive goal which fixes the Javadoc documentation and tags for the test Java files.

Usage:

對多個專案工程建立javadoc

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<!-- Default configuration for all reports -->
...
</configuration>
<executions>
<execution>
<id>aggregate</id>
<goals>
<goal>aggregate</goal>
</goals>
<phase>site</phase>
<configuration>
<!-- Specific configuration for the aggregate report -->
...
</configuration>
</execution>
...
</executions>
</plugin>
或者
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
<!-- Default configuration for all reports -->
...
</configuration>
<reportSets>
<reportSet>
<id>non-aggregate</id>
<configuration>
<!-- Specific configuration for the non aggregate report -->
...
</configuration>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
<reportSet>
<id>aggregate</id>
<configuration>
<!-- Specific configuration for the aggregate report -->
...
</configuration>
<reports>
<report>aggregate</report>
</reports>
</reportSet>
...
</reportSets>
</plugin>


Maven SCM Plugin

SCM(Source Control Manager)plugin是maven提供的可以結合版本控制工具(CVS,SVN等)使用的外掛。

Goals:

  1. scm:branch- branch the project
  2. scm:validate- validate the scm information in the pom
  3. scm:add- command to add file
  4. scm:unedit- command to stop editing the working copy
  5. scm:export- command to get a fresh exported copy
  6. scm:bootstrap- command to checkout and build a project
  7. scm:changelog- command to show the source code revisions
  8. scm:list- command for get the list of project files
  9. scm:checkin- command for commiting changes
  10. scm:checkout- command for getting the source code
  11. scm:status- command for showing the scm status of the working copy
  12. scm:update- command for updating the working copy with the latest changes
  13. scm:diff- command for showing the difference of the working copy with the remote one
  14. scm:update-subprojects- command for updating all projects in a multi project build
  15. scm:edit- command for starting edit on the working copy
  16. scm:tag- command for tagging a certain revision
  17. scm:helpDisplay help information on maven-scm-plugin. Call
  18. mvn scm:help -Ddetail=true -Dgoal=<goal-name>
  19. to display parameter details.

Usage:

1.配置 SCM

<scm>
<developerConnection>scm:cvs:pserver:@192.168.0.249:/cvsroot/EOSS:base</developerConnection>
</scm>
配置scm連結後,可以在外掛中引用
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.4</version>
<configuration>
<connectionType>developerConnection</connectionType>
<goals>install</goals>
<username>liangbingyan</username>
<password>654321</password>
<providerImplementations>
<cvs>cvs_native</cvs>
</providerImplementations>
</configuration>
</plugin>
配置好cvs,username,password等後可以通過執行scm的相關目標命令。如:
mvn scm:bootstrap 從cvs下載程式碼並執行install操作

scm的命令應用

2.1顯示cvs提交修改記錄
mvn -DstartDate=YYYY-MM-DD -DendDate=YYYY-MM-DD scm:changelog
2.2顯示不同修改版本之間的不同
mvn -DstartRevision=<revision> -DendRevision=<revision> scm:diff
2.3打tag標記
mvn -Dtag="<tag name>" scm:tag
2.4驗證scm配置的正確
mvn -DscmConnection="<scm url>" -DscmDeveloperConnection="<scm url>" scm:validate
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

Maven AntRun Plugin

Antrun plugin提供了執行ant中的task的功能,可以將ant的task放在pom中。

Goals:

antrun:helpDisplay help information on maven-antrun-plugin. Call
mvn antrun:help -Ddetail=true -Dgoal=<goal-name>
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 <tasks/> parameter. It is encouraged to move the actual tasks to a separate build.xml file and call that file with an <ant/> task.

Usage:

1.在pom中使用ant的task

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<property name="compile_classpath" refid="maven.compile.classpath"/>
<property name="runtime_classpath" refid="maven.runtime.classpath"/>
<property name="test_classpath" refid="maven.test.classpath"/>
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
<echo message="compile classpath: ${compile_classpath}"/>
<echo message="runtime classpath: ${runtime_classpath}"/>
<echo message="test classpath: ${test_classpath}"/>
<echo message="plugin classpath: ${plugin_classpath}"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>推薦將ant的task單獨放在一個build.xml檔案中,通過如下方式使用:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<property name="compile_classpath" refid="maven.compile.classpath"/>
<property name="runtime_classpath" refid="maven.runtime.classpath"/>
<property name="test_classpath" refid="maven.test.classpath"/>
<property name="plugin_classpath" refid="maven.plugin.classpath"/>
<ant antfile="${basedir}/build.xml">
<target name="test"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
其中build.xml定義如下:
<?xml version="1.0"?>
<project name="test6">
<target name="test">
<echo message="compile classpath: ${compile_classpath}"/>
<echo message="runtime classpath: ${runtime_classpath}"/>
<echo message="test classpath: ${test_classpath}"/>
<echo message="plugin classpath: ${plugin_classpath}"/>
</target>
</project>

使用自定義的task

<configuration>
<property name="log4j.log4j.jar.path" value="${log4j:log4j:jar}" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
</taskdef>
<taskdef name="for" classname="net.sf.antcontrib.logic.For">
</taskdef>
<for list="${log4jproperties}" delimiter="," param="propertykey">
<sequential>
<propertycopy property="propertyvalue" from="@{propertykey}" override="true"/>
<propertyfile file="src/main/resources/log4j.properties">
<entry key="@{propertykey}" value="${propertyvalue}"/>
</propertyfile>
<echo>@{propertykey}=${propertyvalue}</echo>
</sequential>
</for>
</configuration>

Maven Dependency Plugin

Dependency Plugin提供了對專案依賴的artifacts的操作管理。

Goals:

  1. dependency:copytakes a list of artifacts defined in the plugin configuration section and copies them to a specified location, renaming them or stripping the version if desired. This goal can resolve the artifacts from remote repositories if they don't exist in local.
  2. dependency:copy-dependenciestakes the list of project direct dependencies and optionally transitive dependencies and copies them to a specified location, stripping the version if desired. This goal can also be run from the command line.
  3. dependency:unpacklike copy but unpacks.
  4. dependency:unpack-dependencieslike copy-dependencies but unpacks.
  5. dependency:resolvetells Maven to resolve all dependencies and displays the version.
  6. dependency:listis an alias for dependency:resolve
  7. dependency:sourcestells Maven to resolve all dependencies and their source attachments, and displays the version.
  8. dependency:resolve-pluginsTells Maven to resolve plugins and their dependencies.
  9. dependency:listalias for resolve that lists the dependencies for this project.
  10. dependency:go-offlinetells Maven to resolve everything this project is dependent on (dependencies, plugins, reports) in preparation for going offline.
  11. dependency:purge-local-repositorytells Maven to clear all dependency-artifact files out of the local repository, and optionally re-resolve them.
  12. dependency:build-classpathtells Maven to output the path of the dependencies from the local repository in a classpath format to be used in java -cp. The classpath file may also be attached and installed/deployed along with the main artifact.
  13. dependency:analyzeanalyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused and declared.
  14. dependency:analyze-onlyis the same as analyze, but is meant to be bound in a pom. It does not fork the build and execute test-compile.
  15. dependency:analyze-dep-mgtanalyzes your projects dependencies and lists mismatches between resolved dependencies and those listed in your dependencyManagement section.
  16. dependency:treedisplays the dependency tree for this project.

Usage:

使用依賴關係的tree結構解決依賴衝突

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
可以很清楚看到相關衝突資訊

Maven Assembly Plugin

Assembly 外掛提供了手動對某些檔案和依賴打成各種包格式的功能。

Goals:

assembly:helpDisplay help information on maven-assembly-plugin. Call
mvn assembly:help -Ddetail=true -Dgoal=<goal-name>
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).

Usage:

打web service客戶端包

  1. 建立assembly配置檔案,如:src/main/assembly/space-ws-jar.xml

<assembly xmlns="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.2 ">
<id>space-ws-jar</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>
${project.build.outputDirectory}
</directory>
<includes>
<include>cn/bidlink/space/webservice/*.class</include>
<include>cn/bidlink/space/follower/dto/*.class</include>
<include>cn/bidlink/space/myspace/dto/*.class</include>
<include>cn/bidlink/space/notice/dto/*.class</include>
</includes>
</fileSet>
</fileSets>
</assembly>

  1. 配置Assembly 外掛

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/space-ws-jar.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${space.ws.name}</finalName>
</configuration>
<executions>
<execution>
<id>make-space-ws-jar</id>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
3)執行命令:mvn assembly:single就可以將制定檔案達成jar包了。

mvn命令使用說明

usage: mvn [options] [<goal(s)>] [<phase(s)>]
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 <arg> Define a system property
e,-errors Produce execution error messages
emp,-encrypt-master-password <arg> Encrypt master security password
ep,-encrypt-password <arg> Encrypt server 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 <arg> Alternate path for the global
settings file
h,-help Display help information
l,-log-file <arg> Log file to where all build output
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 <arg> Comma-delimited list of profiles
to activate
pl,-projects <arg> Build specified reactor 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 <arg> Resume reactor from specified
project
s,-settings <arg> Alternate path for the user
settings file
T,-threads <arg> Thread count, for instance 2.0C
where C is core multiplied
t,-toolchains <arg> Alternate path for the user
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

例1:

分析eoss-base的dependency,開啟debug資訊,並且輸出到log.txt檔案中
E:\workspaces\eossmvn\eoss-core>mvn dependency:tree -Dverbose -Dincludes=eoss-ba
se -X -l log.txt

例2:

使用線下編譯方式,不從遠端資源庫下載包
mvn -o compile

相關推薦

Maven深入理解

Plugins Plugin是Maven的核心,所有功能的實現是通過外掛完成。Maven提供了很多外掛,也有第三方提供。參考:,幾個比較常用的外掛: Maven Archetype Plugin Maven Eclipse Plugin Maven Compiler Plu

maven深入理解《三》

前面兩篇是對maven的基礎的介紹,這一篇更側重於實際的運用 1.maven設定編譯級別的兩種方式 用外掛的形式: <plugin> <groupId>org.apache.maven.plugins</groupId>

Maven學習總結(18)——深入理解Maven倉庫

一.本地倉庫(Local Repository) 本地倉庫就是一個本機的目錄,這個目錄被用來儲存我們專案的所有依賴(外掛的jar包還有一些其他的檔案),簡單的說,當你build一個Maven專案的時候,所有的依賴檔案都會放在本地倉庫裡,倉庫供所有專案都可以使用 預設情

深入理解maven與應用(二):靈活的構建

 一個優秀的構建系統必須足夠靈活,應該能夠讓專案在不同的環境下都能成功構建。maven為了支援構建的靈活性,內建了三大特性,即:屬性、profile和資源過濾。 1、maven屬性  maven屬性分6類:     1、內建屬性:如${basedir}表示專案根目錄,$

maven深入理解《二》

1.maven與ant與make的基本區別: 最早出現的是make的makeFile,但是強依賴與編寫的作業系統,不是跨平臺,於是ant出現,因為ant用java寫的,所以ant跨平臺了,但是ant和make一樣都是過程式開發,在不同的工程中重複性勞動多,於是

深入理解maven及應用

 在專案裡用了快一年的maven了,最近突然發現maven專案在eclipse中build時非常慢,因為經常用clean install命令來build專案,也沒有管那麼多,但最近實在受不了烏龜一樣的build速度,於是下定決心再看看《maven實戰》吧,        對於

(轉存 作者未知)深入理解HTML協議

期望 intern 屬於 公告欄 機制 被拒 定向 圖片 工具欄 深入理解HTML協議 http協議學 習系列 1. 基礎概念篇 1.1 介紹 HTTP是Hyper Text Transfer Protocol(超文本傳輸協議)的縮寫。它的發展是萬維網協會(World

深入理解hostname

network 配置文件 release linux 知識點 當我覺得對Linux系統下修改hostname已經非常熟悉的時候,今天碰到了幾個個問題,這幾個問題給我好好上了一課,很多知識點,當你覺得你已經掌握的時候,其實你了解的還只是皮毛。技術活,切勿淺嘗則止!實驗環境:Red Hat E

javascript深入理解js閉包

bag 思考 2個 表達式 proto window對象 來看 連接 第一次 閉包(closure)是Javascript語言的一個難點,也是它的特色,很多高級應用都要依靠閉包實現。 一、變量的作用域 要理解閉包,首先必須理解Javascript特殊的變量作用域。

JSON 的深入理解

jos 數據轉換 不同 宋體 ges 分隔 blog 結構 集合 JSON 知識 JSON(JavaScript Object Notataion)javascript的對象表示形式,但是目前已經發展為一種輕量級的數據交互格式。 特點:完全獨立於語言的文本格式,跨平臺,有結

深入理解 CSS3 彈性盒布局模型

分辨率 top 應用 時間 控制 用戶 lock fire 應用開發 彈性盒布局模型(Flexible Box Layout)是 CSS3 規範中提出的一種新的布局方式。該布局模型的目的是提供一種更加高效的方式來對容器中的條目進行布局、對齊和分配空間。這種布局

深入理解javascript之設計模式

rip 是我 解決問題 不想 接受 button move center 常識 設計模式 設計模式是命名、抽象和識別對可重用的面向對象設計實用的的通用設計結構。設計模式確定類和他們的實體、他們的角色和協作、還有他們的責任分配。 每個設計模式都聚焦於一個面向對象的設計難題

(轉)final關鍵字的深入理解

多線程 body error app nds ann this tar order 轉自http://www.importnew.com/7553.html Java中的final關鍵字非常重要,它可以應用於類、方法以及變量。這篇文章中我將帶你看看什麽是final關鍵字?將

深入理解計算機系統-作業2.10

oid 位置 pla borde 作業2 nbsp body 開始 width 1 void inplace_swap(int *x, int *y){ 2 *y = *x ^ *y;/*step1*/ 3 *x = *x ^ *y;/*step2*/ 4

深入理解Activity啟動流程(二)–Activity啟動相關類的類圖

b- ive ava ani affinity server 組織 詳細 pac 本文原創作者:Cloud Chou. 歡迎轉載,請註明出處和本文鏈接 本系列博客將詳細闡述Activity的啟動流程,這些博客基於Cm 10.1源碼研究。 在介紹Activity的詳細啟動流程

深入理解HTTP協議

不想 session 位與 裏的 ec2 只需要 空間 html文檔 動態 1. 基礎概念篇 1.1 介紹 HTTP是Hyper Text Transfer Protocol(超文本傳輸協議)的縮寫。它的發展是萬維網協會(World Wide Web Consortiu

深入理解Java虛擬機- 學習筆記 - 虛擬機類加載機制

支持 pub eth 獲取 事件 必須 string 沒有 字節碼 虛擬機把描述類的數據從Class文件加載道內存,並對數據進行校驗,轉換解析和初始化,最終形成可以被虛擬機直接使用的Java類型,這就是虛擬機的類加載機制。在Java裏,類型的加載、連接和初始化過程都是在程序

深入理解C指針》學習筆記(1)--- 指針之外

結構 def form 學習 編程 stdlib.h struct 一個 char   C語言從誕生之初就非常善於和硬件打交道,經過這麽多年的發展之後,其靈活性和超強的特征是受到幾乎所有程序員的肯定。C語言的這種靈活性很大一部分程度來源與C指針,指針為C語言動態操控內存提供

深入理解AsyncTask

需要 cancel ngs trac 下載 深入理解 回收 生命 str 眾所周知,在Android中如果要執行耗時的操作,一般是在子線程中處理,使用new Thread的方法實現是最常見的方法之一。今天,我們要講的是另外一個,Android提供的異步任務類AsyncTas

深入理解 JBoss 7/WildFly Domain 模式啟動過程

ket builder 代碼調試 ref finished rop mas stopped rgs 概述JBoss 7/WildFly 以 domain 模式啟動時會啟動多個 JVM。比如例如以下通過啟動腳本啟動 domain 模式:./domain.sh啟動後我們查看進