update
[linguofeng.github.com.git] / pages / tools / Maven.md
blob8a642a50366c5f8bf28223c49c75742e38e50f8f
1 ---
2 layout: default
3 title: "Maven"
4 description: "Maven安装与配置、插件与技巧"
5 navigation: [1.下载, 2.配置, 3.完成, 4.Hello Maven, 更多...]
6 ---
8 <section id="1">
9     <div class="page-header">
10         <h3>一、下载</h3>
11     </div>
13     <pre>
14         官方下载地址:http://maven.apache.org/download.html
16         解压至D:\Development\Apache
17     </pre>
18 </section>
20 <section id="2">
21     <div class="page-header">
22         <h3>二、配置</h3>
23     </div>
25     <pre>
26         M2_HOME = D:\Development\Apache\apache-maven-3.0.4
27         Path = .;%M2_HOME%\bin
29         添加一个MAVEN_OPTS环境变量来改变JVM的内存
30         MAVEN_OPTS = -Xms256m -Xmx512m
31     </pre>
32 </section>
34 <section id="3">
35     <div class="page-header">
36         <h3>三、完成</h3>
37     </div>
38     <pre>
39         打开命令行输入:mvn -version
41         Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
42         Maven home: D:\Development\Maven\apache-maven-3.0.4
43         Java version: 1.6.0_29, vendor: Sun Microsystems Inc.
44         Java home: C:\Program Files\Java\jdk1.6.0_29\jre
45         Default locale: zh_CN, platform encoding: GBK
46         OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
48         就表示成功了
49     </pre>
51 安装参考: "http://maven.apache.org/download.html#Installation":http://maven.apache.org/download.html#Installation
52 </section>
54 <section id="4">
55     <div class="page-header">
56         <h3>四、Hello Maven <small>第一个Maven工程</small></h3>
57     </div>
59 <pre>
60 // 1、输入命令
61 mvn archetype:generate
63 // 2、提示选择,直接按回车默认选择:maven-archetype-quickstart
64 Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 171:
66 // 3、之后提示选择maven-archetype-quickstart的版本
67 Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
68 1: 1.0-alpha-1
69 2: 1.0-alpha-2
70 3: 1.0-alpha-3
71 4: 1.0-alpha-4
72 5: 1.0
73 6: 1.1
74 Choose a number: 6: // 4、直接按回车默认选择第6项
76 // 5、接着提示输入groupId、artifactId、 version、以及包名package,如下
77 Define value for property 'groupId': : com.linguofeng
78 Define value for property 'artifactId': : helloworld
79 Define value for property 'version':  1.0-SNAPSHOT: :
80 Define value for property 'package':  com.linguofeng: : helloworld
81 Confirm properties configuration:
82 groupId: com.linguofeng
83 artifactId: helloworld
84 version: 1.0-SNAPSHOT
85 package: helloworld
86  Y: : Y // 6、最后确认
88 [INFO] ----------------------------------------------------------------------------
89 [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
90 [INFO] ----------------------------------------------------------------------------
91 [INFO] Parameter: groupId, Value: com.linguofeng
92 [INFO] Parameter: packageName, Value: helloworld
93 [INFO] Parameter: package, Value: helloworld
94 [INFO] Parameter: artifactId, Value: helloworld
95 [INFO] Parameter: basedir, Value: d:\mvn
96 [INFO] Parameter: version, Value: 1.0-SNAPSHOT
97 [INFO] project created from Old (1.x) Archetype in dir: d:\mvn\helloworld
98 [INFO] ------------------------------------------------------------------------
99 [INFO] BUILD SUCCESS
100 [INFO] ------------------------------------------------------------------------
101 [INFO] Total time: 6:44.171s
102 [INFO] Finished at: Thu Feb 16 13:41:16 CST 2012
103 [INFO] Final Memory: 9M/59M
104 [INFO] ------------------------------------------------------------------------
106  // 成功了
107 </pre>
109 * 2、编译打包
110 <hr />
112 <pre>
113 // 进行工程目录
114 cd helloworld
116 // 编译打包前需要修改pom.xml文件,在</dependencies>标签后增加
117 <build>
118   <plugins>
119     <plugin>
120       <groupId>org.apache.maven.plugins</groupId>
121       <artifactId>maven-shade-plugin</artifactId>
122       <version>1.5</version>
123       <configuration>
124         <transformers>
125           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
126             <manifestEntries>
127               <Main-Class>helloworld.App</Main-Class>
128             </manifestEntries>
129           </transformer>
130         </transformers>
131       </configuration>
132       <executions>
133         <execution>
134           <phase>package</phase>
135           <goals>
136             <goal>shade</goal>
137           </goals>
138         </execution>
139       </executions>
140     </plugin>
141   </plugins>
142 </build>
144 // 参考:http://maven.apache.org/plugins/maven-shade-plugin/usage.html
146 // 打包
147 mvn clean package
148 </pre>
150 * 3、运行
151 <hr />
153 <pre>
154 java -jar target\helloworld-1.0-SNAPSHOT.jar
155 </pre>
157 <section id="5">
158     <div class="page-header">
159         <h3>更多内容 <small>常用插件与技巧</small></h3>
160     </div>
161     <ul><h5>常用插件:</h5>
162         <li><a href="http://maven.apache.org/scm/maven-scm-plugin/index.html">maven-scm-plugin</a> 把工程打Tag后发布到SCM(Git、Svn)服务器上,SCM=Software Configuration Management软件配置管理</li>
163 <pre>
164 <scm>
165     <url>https://github.com/linguofeng/android-dynamic-loading-framework</url>
166     <connection>scm:git:git://github.com/linguofeng/android-dynamic-loading-framework.git</connection>
167     <developerConnection>scm:git:ssh://git@github.com/linguofeng/android-dynamic-loading-framework.git</developerConnection>
168 </scm>
170 <plugin>
171     <artifactId>maven-scm-plugin</artifactId>
172     <version>1.7</version>
173     <configuration>
174         <scmVersionType>branch</scmVersionType>
175         <scmVersion>master</scmVersion>
176     </configuration>
177 </plugin>
178 </pre>
179         <li><a href="http://maven.apache.org/plugins/maven-release-plugin/">maven-release-plugin</a> 版本发布插件</li>
180 <pre>
181 <distributionManagement>
182     <repository>
183         <id>nexus-releases</id>
184         <name>Nexus Release Repository</name>
185         <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>
186     </repository>
187     <snapshotRepository>
188         <id>nexus-snapshots</id>
189         <name>Nexus Snapshot Repository</name>
190         <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>
191     </snapshotRepository>
192 </distributionManagement>
194 <plugin>
195     <artifactId>maven-release-plugin</artifactId>
196     <version>2.3.2</version>
197     <configuration>
198         <autoVersionSubmodules>true</autoVersionSubmodules>
199         <scmCommentPrefix></scmCommentPrefix>
200     </configuration>
201 </plugin>
203 使用命令:
204 $ mvn release:prepare // 此命令会将工程发布到SCM服务器上,需要maven-scm-plugin插件的支持
205 $ mvn release:perform // 此命令会将工程发布到Maven仓库中
206 </pre>
207         <li><a href="https://github.com/jayway/maven-android-plugin">maven-android-plugin</a> 开发Android应用的插件,结合 <a href="/pages/favorites/eclipse.html#m2e-android">m2e-android</a> 一起使用</li>
208 <pre>
209 通过Maven命令行创建Android工程:https://github.com/rgladwell/m2e-android
211 $ mvn archetype:generate \
212   -DarchetypeArtifactId=android-quickstart \
213   -DarchetypeGroupId=de.akquinet.android.archetypes \
214   -DarchetypeVersion=1.0.8 \
215   -DgroupId=your.company \
216   -DartifactId=my-android-application
217 </pre>
218     </ul>
219 </section>