Maven使用入门(二)

来源:互联网 发布:通达信软件电脑版 编辑:程序博客网 时间:2024/05/01 15:32

1.新建项目文件夹hello-world,在hello-world目录下新建pom.xml文件

<?xml version="1.0" encoding="UTF-8" ?> 
<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/mavenv4_0_0.xsd">               
               <modelVersion>4.0.0</modelVersion>               
               <groupId>com.juvenxu.mvnbook</groupId>                
               <artifactId>hello-world</artifactId>               
               <version>1.0-SNAPSHOT</version>               
               <name>Maven Hello World Project</name>  
</project>

2.编写并编译源文件

在hello-world\src\main\java\com\juvenxu\mvnbook\helloworld目录下创建HelloWorld.java文件,如下:

package com.juvenxu.mvnbook.helloworld;
public class HelloWorld { 
 public String sayHello(){ 
  return "Hello Maven"; 
 } 
 public static void main(String[] args){ 
  System.out.print( new HelloWorld().sayHello() ); 
 }
}

 

进入项目根目录,即hello-world目录下,在命令行下执行 mvn clean compile,cmd输出如下信息:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project[INFO]    task-segment: [clean, compile]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}][INFO] Deleting directory D:\project\hello-world\target
[INFO] [resources:resources{execution: default-resources}][WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\main\resources
[INFO] [compiler:compile{execution: default-compile}][INFO] Compiling 1 source file to D:\project\hello-world\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second[INFO] Finished at: Tue Mar 27 16:57:39 CST 2012
[INFO] Final Memory: 9M/20M[INFO] ------------------------------------------------------------------------

 clean告诉Maven清理输出目录target/,compile告诉Maven编译项目主代码,从输出中可以看出maven依次执行了如下任务:clean:clean,resource:resource,compiler:compile,他们对应了一些Maven插件及插件目标,比如clean:clean是clean插件的clean目标,compiler:compile是compiler插件的compile目标。


3.编写执行单元测试                    
1).编写测试类hello-world\src\test\java\com\juvenxu\mvnbook\helloworld\HelloWorldTest.java   package com.juvenxu.mvnbook.helloworld;
  import static org.junit.Assert.*;
  import org.junit.Test;
  public class HelloWorldTest {
    @Test
    public void testSayHello(){
      HelloWorld helloWorld = new HelloWorld();
      String result = helloWorld.sayHello();
      assertEquals( "Hello Maven", result );
    }
  }

     2).在pom.xml中引入Junit依赖
 <?xml version="1.0" encoding="UTF-8" ?> 
 <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/maven-v4_0_0.xsd">
              <modelVersion>4.0.0</modelVersion> 
              <groupId>com.juvenxu.mvnbook</groupId> 
              <artifactId>hello-world</artifactId> 
              <version>1.0-SNAPSHOT</version> 
              <name>Maven Hello World Project</name> 
              <dependencies>
                     <dependency>
                                  <groupId>junit</groupId> 
                                  <artifactId>junit</artifactId> 
                                  <version>4.7</version> 
                                  <scope>test</scope><!--表示此依赖只对测试范围有效,默认值为compile,表示该代码对主代码和测试代码都有效-->
                     </dependency>
              </dependencies>
 </project>   在命令行主目录下运行mvn clean test,打印信息如下:[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project
[INFO]    task-segment: [clean, test]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory D:\project\hello-world\target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\test-classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
D:\project\hello-world\src\test\java\com\juvenxu\mvnbook\helloworld\HelloWorldTest.java:[3,7] -source 1.3 中不支持静态导入声明
(请使用 -source 5 或更高版本以启用静态导入声明)import static org.junit.Assert.*;
D:\project\hello-world\src\test\java\com\juvenxu\mvnbook\helloworld\HelloWorldTest.java:[8,2] -source 1.3 中不支持注释
(请使用 -source 5 或更高版本以启用注释)@Test
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Mar 27 17:21:42 CST 2012
[INFO] Final Memory: 9M/18M
[INFO] ------------------------------------------------------------------------

构建失败了,命令行输入mvn clean test,maven执行了如下任务:clean:clean,resource:resources,compiler:compile,resources:testResources以及compiler:testCompile.主要含义是清除targer,主资源处理,主代码编译,测试资源处理,测试代码编译。从输出中我们可以看出来构建在执行compiler:testCompile任务的时候失败了,Maven输出提示我们需要使用-source 5或更高版本以启动注释,这是因为Maven的核心插件之一compiler插件默认只支持编译Java1.3,因此我们需要配置该插件使其支持Java5.<?xml version="1.0" encoding="UTF-8"?>
<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.0http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.juvenxu.mvnbook</groupId>
 <artifactId>hello-world</artifactId>
 <version>1.0-SNAPSHOT</version>
 <name>Maven Hello World Project</name>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.7</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin><!--通过此配置修改maven插件的默认配置信息-->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin>

  </plugins>
 </build>
</project>

在命令行中重新执行mvn clean test,输出信息如下:[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project
[INFO]    task-segment: [clean, test]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory D:\project\hello-world\target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: D:\project\hello-world\target\surefire-reports


-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Mar 27 17:36:53 CST 2012
[INFO] Final Memory: 13M/24M
[INFO] ------------------------------------------------------------------------

surefire:test任务主要运行测试并输出测试报告。

4.打包

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project
[INFO]    task-segment: [clean, package]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory D:\project\hello-world\target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: D:\project\hello-world\target\surefire-reports


-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: D:\project\hello-world\target\hello-world-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Tue Mar 27 17:47:03 CST 2012
[INFO] Final Memory: 15M/28M
[INFO] ------------------------------------------------------------------------
我们发现在target/输出目录中,有一个名叫hello-world-1.0-SNAPSHOT.jar的文件,它是根据artifact-version.jar的规则生成的。如果需要我们还可以通过finalName来定义该文件名称。

5.安装install

为了让其它的maven管理的项目可以使用到hello-world-1.0-SNAPSHOT.jar,我们还需要一个安装的步骤,执行mvn clean install命令,输出如下:[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory D:\project\hello-world\target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\main\resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\project\hello-world\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to D:\project\hello-world\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: D:\project\hello-world\target\surefire-reports


-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: D:\project\hello-world\target\hello-world-1.0-SNAPSHOT.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing D:\project\hello-world\target\hello-world-1.0-SNAPSHOT.jar to
C:\Users\360buy\.m2\repository\com\juvenxu\mvnbook\hello-world\1.0-SNAPSHOT\hello-world-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Tue Mar 27 17:54:02 CST 2012
[INFO] Final Memory: 15M/27M
[INFO] ------------------------------------------------------------------------

在打包之后,我们又执行了安装任务install:install,从输出中我们可以看出该任务将项目输出的jar安装到了Maven本地仓库中,这样其它的Maven项目就可以使用它们了。

 

6.运行Main方法
借助maven-shade-plugin插件将main方法的类信息添加到manifest中(在jar包中的META-INF/MANIFEST.MF文件,加入Main-Class一行)

在pom.xml中添加如下配置:

   <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.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>
        </transformer>
       </transformers>
      </configuration>
     </execution>
    </executions>
   </plugin>

plugin元素在POM中的相对位置应该在<project><build><plugins>下面,我们配置了mainClass为com.juvenxu.mvnbook.helloworld.HelloWorld,项目在打包时会将该信息放到MANIFEST中,现在执行mvn clean install,待构建完成之后打开target/目录,我们可以看到hello-world-1.0-SNAPSHOT.jar和original-hello-world-1.0-SNAPSHOT.jar,前者是带有Main-Class信息的可运行jar,后者是原始的jar。

 

7.Archetype生成项目骨架

Hello World项目中有一些Maven的约定:在项目的根目录中放置pom.xml,在src/main/java目录中放置项目的主代码,在src/test/java中放置项目的测试代码。我之所以一步一步地展示这些步骤,是为了能让可能是Maven初学者的你得到最实际的感受。我们称这些基本的目录结构和pom.xml文件内容称为项目的骨架,当你第一次创建项目骨架的时候,你还会饶有兴趣地去体会这些默认约定背后的思想,第二次,第三次,你也许还会满意自己的熟练程度,但第四、第五次做同样的事情,就会让程序员恼火了,为此Maven提供了Archetype以帮助我们快速勾勒出项目骨架。

还是以Hello World为例,我们使用maven archetype来创建该项目的骨架,离开当前的Maven项目目录。

如果是Maven 3,简单的运行:

mvn archetype:generate

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

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-5:generate

很多资料会让你直接使用更为简单的 mvn archetype:generate 命令,但在Maven2中这是不安全的,因为该命令没有指定archetype插件的版本,于是Maven会自动去下载最新的版本,进而可能得到不稳定的SNAPSHOT版本,导致运行失败。然而在Maven 3中,即使用户没有指定版本,Maven也只会解析最新的稳定版本,因此这是安全的,具体内容见7.7小节。

我们实际上是在运行插件maven-archetype-plugin,注意冒号的分隔,其格式为 groupId:artifactId:version:goal ,org.apache.maven.plugins 是maven官方插件的groupId,maven-archetype-plugin 是archetype插件的artifactId,2.0-alpha-5 是目前该插件最新的稳定版,generate是我们要使用的插件目标。

紧接着我们会看到一段长长的输出,有很多可用的archetype供我们选择,包括著名的Appfuse项目的archetype,JPA项目的archetype等等。每一个archetype前面都会对应有一个编号,同时命令行会提示一个默认的编号,其对应的archetype为maven-archetype-quickstart,我们直接回车以选择该archetype,紧接着Maven会提示我们输入要创建项目的groupId、artifactId、 version、以及包名package,如下输入并确认:

 

Java代码 

1. Define value for groupId: : com.juvenxu.mvnbook   

2. Define value for artifactId: : hello-world   

3. Define value for version:  1.0-SNAPSHOT: :   

4. Define value for package:  com.juvenxu.mvnbook: : com.juvenxu.mvnbook.helloworld   

5. Confirm properties configuration:   

6. groupId: com.juvenxu.mvnbook   

7. artifactId: hello-world   

8. version: 1.0-SNAPSHOT   

9. package: com.juvenxu.mvnbook.helloworld   

10.  Y: : Y  

Define value for groupId: : com.juvenxu.mvnbook

Define value for artifactId: : hello-world

Define value for version:  1.0-SNAPSHOT: :

Define value for package:  com.juvenxu.mvnbook: : com.juvenxu.mvnbook.helloworld

Confirm properties configuration:

groupId: com.juvenxu.mvnbook

artifactId: hello-world

version: 1.0-SNAPSHOT

package: com.juvenxu.mvnbook.helloworld

 Y: : Y

Archetype插件将根据我们提供的信息创建项目骨架。在当前目录下,Archetype插件会创建一个名为hello-world(我们定义的artifactId)的子目录,从中可以看到项目的基本结构:基本的pom.xml已经被创建,里面包含了必要的信息以及一个junit依赖;主代码目录src/main/java已经被创建,在该目录下还有一个Java类com.juvenxu.mvnbook.helloworld.App,注意这里使用到了我们刚才定义的包名,而这个类也仅仅只有一个简单的输出Hello World!的main方法;测试代码目录src/test/java也被创建好了,并且包含了一个测试用例com.juvenxu.mvnbook.helloworld.AppTest。

Archetype可以帮助我们迅速地构建起项目的骨架,在前面的例子中,我们完全可以在Archetype生成的骨架的基础上开发Hello World项目以节省我们大量时间。

此外,我们这里仅仅是看到了一个最简单的archetype,如果你有很多项目拥有类似的自定义项目结构以及配置文件,你完全可以一劳永逸地开发自己的archetype,然后在这些项目中使用自定义的archetype来快速生成项目骨架,本书后面的章节会详细阐述如何开发Maven Archetype。

 

8:查看POM.xml默认设置

mvn help:effective-pom 

<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
ttp://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.juvenxu.mvnbook</groupId>
  <artifactId>hello-world</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Hello World Project</name>
  <build>
    <sourceDirectory>D:\project\hello-world\src\main\java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>D:\project\hello-world\src\test\java</testSourceDirect
ry>
    <outputDirectory>D:\project\hello-world\target\classes</outputDirectory>
    <testOutputDirectory>D:\project\hello-world\target\test-classes</testOutput
irectory>
    <resources>
      <resource>
        <mergeId>resource-0</mergeId>
        <directory>D:\project\hello-world\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <mergeId>resource-1</mergeId>
        <directory>D:\project\hello-world\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>D:\project\hello-world\target</directory>
    <finalName>hello-world-1.0-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-2</version>
        </plugin>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-ear-plugin</artifactId>
          <version>2.3.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-ejb-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-javadoc-plugin</artifactId>
          <version>2.5</version>
        </plugin>
        <plugin>
          <artifactId>maven-plugin-plugin</artifactId>
          <version>2.4.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-rar-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0-beta-8</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>2.0-beta-7</version>
        </plugin>
        <plugin>
          <artifactId>maven-source-plugin</artifactId>
          <version>2.0.4</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.4.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.1-alpha-2</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
      <plugin>
        <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.res
urce.ManifestResourceTransformer">
                  <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainCla
s>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-help-plugin</artifactId>
        <version>2.1.1</version>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>libs-releases</name>
      <url>http://artifactory.360buy-develop.com/libs-releases</url>
    </repository>
    <repository>
      <snapshots />
      <id>snapshots</id>
      <name>libs-snapshots</name>
      <url>http://artifactory.360buy-develop.com/libs-snapshots</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>plugins-releases</name>
      <url>http://artifactory.360buy-develop.com/plugins-releases</url>
    </pluginRepository>
    <pluginRepository>
      <snapshots />
      <id>snapshots</id>
      <name>plugins-snapshots</name>
      <url>http://artifactory.360buy-develop.com/plugins-snapshots</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <reporting>
    <outputDirectory>D:\project\hello-world\target/site</outputDirectory>
  </reporting>
</project>

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Mar 27 22:35:48 CST 2012
[INFO] Final Memory: 6M/12M
[INFO] ------------------------------------------------------------------------

9. 生命周期简介
    生命周期是一系列阶段的序列,每个阶段可能绑定了零个或者多个目标。Maven执行一个阶段的时候,它首先会有序的执行前面的所有阶段,到命令行指定的那个阶段为止。当我们没有进行任何插件配置或者定制时,如果执行以package为结尾的默认生命周期的时候,下面的目标按顺序被执行。
      resources:resources  
 Resources插件的resources目标绑定到了process-resources阶段。这个目标复制src/main/resources下的所有资源和其它任何配置的资源目录,到输出目录。
      compiler:compile
 Compiler插件的compile目标绑定到了compile阶段。这个目标编译src/main/java下的所有源代码和其他任何配置的资源目录,到输出目录。
      resources:testResources
 Resources插件的testResources目标绑定到了process-test-resources 阶段。这个目标复制src/test/resources下的所有资源和其它任何的配置的测试资源目录,到测试输出目录。
    compiler:testCompile
 Compiler插件的testCompile目标绑定到了test-compile 阶段。这个目标编译src/test/java下的测试用例和其它任何的配置的测试资源目录,到测试输出目录。
    compiler:testCompile
 Compiler插件的testCompile目标绑定到了test-compile 阶段。这个目标编译src/test/java下的测试用例和其它任何的配置的测试资源目录,到测试输出目录。
    jar:jar
 Jar插件的jar目标绑定到了package 阶段。这个目标把输出目录打包成JAR文件。

10.依赖捆绑
 当为项目创建JAR文件的时候,它的依赖不会被捆绑在生成的构件中,他们只是用来编译。当用Maven来创建WAR或者EAR,你可以配置Maven让它在生成的构件中捆绑依赖,你也可以配置Maven,使用provided范围,让它排除WAR文件中特定的依赖。provided范围告诉Maven一个依赖在编译的时候需要,但是它不应该被捆绑在构建的输出中。当你开发web应用的时候provided范围变得十分有用,你需要通过Servlet API来编译你的代码,但是你不希望Servlet API的JAR文件包含在你web应用的WEB-INF/lib目录中。

原创粉丝点击