Maven 使用点滴

项目坐标:

  <groupId>com.alibaba</groupId>
  <artifactId>dubbo-parent</artifactId>
  <version>2.5.4-SNAPSHOT</version>
  <packaging>jar</packaging>

项目的打包格式也是Maven坐标的重要组成部分,但是它不是项目唯一标识符的一个部
分。一个项目的groupId:artifactId:version使之成为一个独一无二的项目;你不能同
时有一个拥有同样的groupId, artifactId和version标识的项目。

项目依赖:

<dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-servlet_2.4_spec</artifactId>
      <version>1.0</version>
      <scope>provided</scope>
   </dependency>
</dependencies>

依赖范围三种:compile,test,和provided

compile(编译范围):compile是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的classpath中可用,同时它们也会被打包。

test(测试范围):test范围依赖 在一般的 编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

provided(已提供范围):provided依赖只有在当JDK或者一个容器已提供该依赖之后才使用。已提供范围的依赖在编译classpath(不是运行时)可用。它们不是传递性的,也不会被打包。

排除并替换一个传递性依赖:

<dependencies>
   <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate</artifactId>
      <version>3.2.5.ga</version>
      <exclusions>
         <exclusion>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
         </exclusion>
      </exclusions>
   </dependency>
   <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-jta_1.1_spec</artifactId>
      <version>1.1</version>
   </dependency>
</dependencies>


【编译插件】

<build>
    //默认测试源码目录和测试编译输出目录的位置,默认不用设置
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    //默认源码目录和编译输出目录的位置,默认不用设置
   <sourceDirectory>src/main/java</sourceDirectory>
   <outputDirectory>target/classes</outputDirectory>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-compiler-plugin</artifactId> 
         <version>2.3.2</version> 
         <configuration>
            <source>1.5</source>
            <target>1.5</target>
            <encoding>UTF-8</encoding> 
            <compilerVersion>1.5</compilerVersion> 
            <verbose>true</verbose>
         </configuration>
      </plugin>
   </plugins>
</build>

Java源代码遵循Java 1.5,目标为Java 1.5 JVM

【忽略单元测试失败】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
            <testFailureIgnore>true</testFailureIgnore>
         </configuration>
      </plugin>
   </plugins>
</build>

插件参数表达式:

mvn test -Dmaven.test.failure.ignore=true


【跳过单元测试】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
            <skip>true</skip>
         </configuration>
      </plugin>
   </plugins>
</build>

插件参数表达式:

mvn install -Dmaven.test.skip=true

【打包war项目】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <version>2.1.1</version>
         <configuration>
            <webResources>
               <resource>
                  <directory>src/main/webapp</directory>
                  <excludes>
                     <exclude>**/*.jpg</exclude>
                  </excludes>
               </resource>
            </webResources>
         </configuration>
      </plugin>
   </plugins>
</build>

【生成可执行jar】

<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.hainiubl.App</mainClass>
                       </transformer>
                    </transformers>
                 </configuration>
             </execution>
        </executions>
       </plugin>
   </plugins>
</build>

可以让用户配置Main-Class的值,然后在打包的时候将值填入/META-INF/MANIFEST.MF文件。关于项目的依赖,它将依赖JAR文件全部解压后,再将得到的.class文件连同当前项目.class文件一起合并到最终的jar包。 可以通过java -jar app.jar 直接执行jar包

【制作项目分发包】

<build>
   <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptors>
                <descriptor>src/main/assemble/assembly.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
   </plugins>
</build>

assembly.xml 文件:

<assembly 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/assembly-1.0.0.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

该分发包可能包含了项目的可执行文件、源代码、readme、平台脚本等等。支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的。
使用一个名为assembly.xml的元数据文件来表述打包,它的single目标可以直接在命令行调用,也可以被绑定至生命周期。

【运行任何本地的系统程序】

<build>
   <plugins>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <version>1.2.1</version>
         <executions>
            <execution>
               <goals>
                  <goal>java</goal>
               </goals>
            </execution>
         </executions>
         <configuration>
            <mainClass>com.hainiubl.App</mainClass>
         </configuration>
      </plugin>
   </plugins>
</build>

它能让你运行任何本地的系统程序,就是使用 mvn exec:exec。除了exec目标之外,exec-maven-plugin还提供了一个java目标,该目标要求你提供一个mainClass参数,然后它能够利用当前项目的依赖作为classpath,在同一个JVM中运行该mainClass。你可以在POM中配置好exec-maven-plugin的相关运行参数,然后直接在命令运行 mvn exec:java 以查看运行效果。

【生成javadoc】

<build>
   <plugins>
      <plugin>         
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-javadoc-plugin</artifactId>
         <version>2.7</version>
         <executions>
            <execution>
               <id>attach-javadocs</id>
               <goals>
                  <goal>jar</goal>
               </goals>
            </execution>
         </executions>
      </plugin>   
   </plugins>
</build>

【生成项目源码包】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-source-plugin</artifactId>
         <version>2.1.2</version>
         <executions>
            <execution>
               <id>attach-sources</id>
               <phase>verify</phase>
               <goals>
                  <goal>jar-no-fork</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>


Maven默认生命周期阶段:

1.validate :验证项目是否正确,以及所有为了完整构建必要的信息是否可用
2.generate-sources :生成所有需要包含在编译过程中的源代码
3.process-sources :处理源代码,比如过滤一些值
4.generate-resources :生成所有需要包含在打包过程中的资源文件
5.process-resources :复制并处理资源文件至目标目录,准备打包
6.compile :编译项目的源代码
7.process-classes :后处理编译生成的文件,例如对Java类进行字节码增强
8.generate-test-sources :生成所有包含在测试编译过程中的测试源码
9.process-test-sources :处理测试源码,比如过滤一些值
10.generate-test-resources :生成测试需要的资源文件
11.process-test-resources :复制并处理测试资源文件至测试目标目录
12.test-compile :编译测试源码至测试目标目录
13.test :使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布
14.prepare-package :在真正的打包之前,执行一些准备打包必要的操作。这通常会产生一个包的展开的处理过的版本
15.package :将编译好的代码打包成可分发的格式,如JAR,WAR,或者EAR
16.pre-integration-test :执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境
17.integration-test :如果有必要的话,处理包并发布至集成测试可以运行的环境
18.post-integration-test :执行一些在集成测试运行之后需要的动作。如清理集成测试环境。
19.verify :执行所有检查,验证包是有效的,符合质量规范
20.install 安装包至本地仓库,以备本地的其它项目作为依赖使用
21.deploy 复制最终的包至远程仓库,共享给其它开发人员和项目


打包类型相关生命周期:

   1.JAR打包类型:JAR是默认的打包类型

   生命周期阶段            目标
   process-resources       resources:resources
   compile                 compiler:compile
   process-test-resources  resources:testResources
   test-compile            compiler:testCompile
   test                    surefire:test
   package                 jar:jar
   install                 install:install
   deploy                  deploy:deploy

   2.POM打包类型:

   生命周期阶段            目标
   package                 site:attach-descriptor
   install                 install:install
   deploy                  deploy:deploy

   3.WAR打包类型:

   生命周期阶段            目标
   process-resources       resources:resources
   compile                 compiler:compile
   process-test-resources  resources:testResources
   test-compile            compiler:testCompile
   test                    surefire:test
   package                 war:war
   install                 install:install
   deploy                  deploy:deploy

   4.EAR打包类型:

   生命周期阶段            目标
   generate-resources      ear:generate-application-xml
   process-resources       resources:resources
   package                 ear:ear
   install                 install:install
   deploy                  deploy:deploy





猜你喜欢

转载自maosheng.iteye.com/blog/2367629
今日推荐