maven实战笔记-3

第三章 Maven使用入门

    3.1  pom.xml示例

 

    <project xmlns="http://maven.apache.org/POM/4.0.0"    

             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 

             http://maven.apache.org/xsd/maven-4.0.0.xsd">

         <modelVersion>4.0.0</modelVersion>

             <groupId>com.wangsy</groupId>

        <artifactId>testMaven</artifactId>

        <version>0.0.1-SNAPSHOT</version>

        <packaging>jar</packaging>

 

        <name>testMaven</name>

        <url>http://maven.apache.org</url>

 

        <properties>

            <project.build.sourceEncoding>

                 UTF-8

            </project.build.sourceEncoding>

        </properties>

 

       <dependencies>

           <dependency>

               <groupId>junit</groupId>

               <artifactId>junit</artifactId>

               <version>3.8.1</version>

               <scope>test</scope>

          </dependency>

       </dependencies>

    </project>

 

    解析:

      groupId定义了项目属于哪个组,这个组往往和项目所在的组织或公司存在关联。

      artifactId定义了当前Maven项目在组中唯一的ID

      version指定了版本。SNAPSHOT指快照,说明该项目还处于开发中,是不稳定的版本。

 

   3.2  执行编译

         mvn clean compile

 

   3.3  执行测试

         mvn clean test

 

   3.4  配置maven-compiler-plugin支持java5

     <build>

         <plugins>

             <plugin>

                 <groupId>org.apache.maven.plugins</groupId>

                 <artifactId>maven-compiler-plugin</artifactId>

                 <configuration>

                     <source>1.5</source>

                     <target>1.5</target>

                 </configuration>

             </plugin>

         </plugins>

      </build>

 

   3.5  打包和运行

        mvn clean package

 

        mvn clean install

 

   3.6  配置运行类

     <plugin>

           <groupId>org.apache.maven.plugins</groupId>

           <artifactId>maven-shade-plugin</artifactId>

           <version>1.2.1</version>

           <executions>

               <execution>

                  <phase>package</phase>

                  <goals>

                      <goal>shade</goal>

                  </goals>

                  <configuration>

                      <transformers>

                         <transformer

                           implementation="org.apache.maven.plugins.shade.

 

                             resource.ManifestResourceTransformer">

                              

                             <mainClass>

                                 com.wangsy.testMaven.App

                             </mainClass>

                         </transformer>

                      </transformers>

                  </configuration>

               </execution>

           </executions>

        </plugin>

 

   3.7  运行jar

        java –jar target/testMaven-0.0.1-SNAPSHOT.jar

 

   3.8  使用Archetype生成项目骨架

     如果是Maven3,简单地运行:

        mvn archetype:generate

     如果是Maven2,最好运行如下命令:

         mvn org.apache.maven.plugins:

                 maven-archetype-plugin:2.0-alpha-5:generate

 

      会看到一段长长的输出,有很多可用的Archetype供选择,包括著名的Appfuse项目的ArchetypeJPA项目的 Archetype等。每一个Archetype前面都会对应有一个编号,同时命令行会提示一个默认的编号,其对应的Archetypemaven-archetype-quickstart,直接加车以选择该Archetype,紧接着Maven会提示输入 要创建项目的groupIdartifactIdversion以及包名package。如下图所示:


         Archetype会迅速构建起项目的骨架。

你可能感兴趣的:(maven,职场,学习笔记,休闲,maven笔记)