内置变量

  1. ${basedir}表示项目根目录,即包含pom.xml文件的目录;
  2. ${version}表示项目版本;
  3. ${project.basedir}同${basedir};
  4. ${project.baseUri}表示项目文件地址;
  5. ${maven.build.timestamp}表示项目构件开始时间;
  6. ${maven.build.timestamp.format}表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat
  7. ${project.build.directory}表示主源码路径;
  8. ${project.build.sourceEncoding}表示主源码的编码格式;
  9. ${project.build.sourceDirectory}表示主源码路径;
  10. ${project.build.finalName}表示输出文件名称;
  11. ${project.version}表示项目版本,与${version}相同;
  12. ${project.xxx} 当前pom文件的任意节点的内容
  13. ${env.xxx} 获取系统环境变量。
  14. ${settings.xxx} 指代了settings.xml中对应元素的值。

资源管理

  1. <resources>
  2. <resource>
  3. <directory>src/main/java</directory>
  4. <excludes>
  5. <exclude>**/*.java</exclude>
  6. </excludes>
  7. </resource>
  8. <resource>
  9. <directory>src/main/webapp</directory>
  10. <!--注意此次必须要放在此目录下才能被访问到 -->
  11. <targetPath>META-INF/resources</targetPath>
  12. <includes>
  13. <include>**/**</include>
  14. </includes>
  15. </resource>
  16. <resource>
  17. <directory>src/main/resources</directory>
  18. <includes>
  19. <include>**/**</include>
  20. </includes>
  21. <filtering>false</filtering>
  22. </resource>
  23. </resources>

插件

  • maven-compiler-plugin 编译插件
  • maven-jar-plugin Jar包插件
  • maven-source-plugin 资源插件
  • maven-dependency-plugin 依赖插件
  • maven-assembly-plugin 打包插件
  • maven-surefire-plugin TestCase插件,忽略测试用例
  • spring-boot-maven-plugin SpringBoot打包插件

    maven-compiler-plugin

    1. <!-- 设置maven编译的jdk版本,maven3默认用jdk1.5,maven2默认用jdk1.3 -->
    2. <plugin>
    3. <groupId>org.apache.maven.plugins</groupId>
    4. <artifactId>maven-compiler-plugin</artifactId>
    5. <version>3.1</version>
    6. <configuration>
    7. <source>1.8</source> <!-- 源代码使用的JDK版本 -->
    8. <target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
    9. <encoding>UTF-8</encoding><!-- 字符集编码 -->
    10. <skipTests>true</skipTests><!-- 跳过测试 -->
    11. </configuration>
    12. </plugin>

    maven-jar-plugin

    1. <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
    2. <plugin>
    3. <groupId>org.apache.maven.plugins</groupId>
    4. <artifactId>maven-jar-plugin</artifactId>
    5. <configuration>
    6. <classesDirectory>target/classes/</classesDirectory>
    7. <archive>
    8. <manifest>
    9. <mainClass>com.alibaba.dubbo.container.Main</mainClass>
    10. <!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
    11. <!--自动加载META-INF/spring目录下的所有Spring配置-->
    12. <useUniqueVersions>false</useUniqueVersions>
    13. <addClasspath>true</addClasspath>
    14. <classpathPrefix>lib/</classpathPrefix>
    15. </manifest>
    16. <manifestEntries>
    17. <Class-Path>.</Class-Path>
    18. </manifestEntries>
    19. </archive>
    20. </configuration>
    21. </plugin>

    maven-source-plugin

    1. <!-- 在多项目构建中,将source-plugin置于顶层或parent的pom中并不会发挥作用,必须置于具体项目的pom中。 -->
    2. <plugin>
    3. <groupId>org.apache.maven.plugins</groupId>
    4. <artifactId>maven-source-plugin</artifactId>
    5. <executions>
    6. <execution>
    7. <id>attach-sources</id>
    8. <goals>
    9. <goal>jar</goal>
    10. </goals>
    11. </execution>
    12. </executions>
    13. </plugin>

    maven-resource-plugin

    1. <!-- 该插件处理项目的资源文件拷贝到输出目录。可以分别处理main resources 和 test resources。 -->
    2. <plugin>
    3. <groupId>org.apache.maven.plugins</groupId>
    4. <artifactId>maven-resources-plugin</artifactId>
    5. <version>3.0.1</version>
    6. <configuration>
    7. <encoding>UTF-8</encoding>
    8. </configuration>
    9. </plugin>

    maven-dependency-plugin

    1. <!-- 自动拷贝jar包到target目录 -->
    2. <plugin>
    3. <groupId>org.apache.maven.plugins</groupId>
    4. <artifactId>maven-dependency-plugin</artifactId>
    5. <version>2.6</version>
    6. <executions>
    7. <execution>
    8. <id>copy-dependencies</id>
    9. <phase>compile</phase>
    10. <goals>
    11. <goal>copy-dependencies</goal>
    12. </goals>
    13. <configuration>
    14. <!-- ${project.build.directory}为Maven内置变量,缺省为target -->
    15. <outputDirectory>${project.build.directory}/lib</outputDirectory>
    16. <!-- 表示是否不包含间接依赖的包 -->
    17. <excludeTransitive>false</excludeTransitive>
    18. <!-- 表示复制的jar文件去掉版本信息 -->
    19. <stripVersion>true</stripVersion>
    20. </configuration>
    21. </execution>
    22. </executions>
    23. </plugin>

    maven-assembly-plugin

    ```xml maven-assembly-plugin 3.0.0 jar-with-dependencies make-assembly package single
  1. <a name="h57gC"></a>
  2. ## maven-surefire-plugin
  3. ```xml
  4. <plugin>
  5. <groupId>org.apache.maven.plugins</groupId>
  6. <artifactId>maven-surefire-plugin</artifactId>
  7. <version>2.16</version>
  8. <configuration>
  9. <skipTests>true</skipTests>
  10. </configuration>
  11. </plugin>
  12. -- mvn package -DskipTests
  13. -- mvn package -Dmaven.test.skip=true
  14. -- mvn test -Dtest=RandomGeneratorTest

spring-boot-maven-plugin

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. <version>1.4.2.RELEASE</version>
  5. <configuration>
  6. <mainClass>com.yunio.pandabank.mock.MockAppliction</mainClass>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <goals>
  11. <goal>repackage</goal>
  12. </goals>
  13. </execution>
  14. </executions>
  15. </plugin>