Maven QuickStart

Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,即pom.xml配置文件,报告和文档的软件项目管理工具。

个人理解类似C下面的Makefile,用于编译打包jar,解决包依赖。

官网:

http://maven.apache.org/

文档:

http://maven.apache.org/install.html

安装

解压到 /usr/local/server/maven

将 /usr/local/server/maven/bin 写入PATH

检测安装成功与否,这个需要在项目目录下执行可能才正常:

mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /usr/local/server/maven
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: ISO8859-1
OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"

主要配置

修改本地仓库位置,以及使用aliyun的仓库源:

...
<localRepository>/usr/local/server/maven/repo</localRepository>
...
<mirrors>
    <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
    </mirror>
<mirrors>
...

插件

Webapp插件

maven-assembly-plugin

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.niuchaoqun.example.Example</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

maven-shade-plugin:

<build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <version>1.4</version>
           <executions>
               <execution>
                   <phase>package</phase>
                   <goals>
                       <goal>shade</goal>
                   </goals>
                   <configuration>
                       <transformers>
                           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                               <mainClass>com.niuchaoqun.example.Example</mainClass>
                           </transformer>
                       </transformers>
                   </configuration>
               </execution>
           </executions>
       </plugin>
   </plugins>
</build>