ITPub博客

首页 > 应用开发 > Java > Maven学习

Maven学习

原创 Java 作者:壹頁書 时间:2016-06-19 19:44:22 0 删除 编辑
Maven依赖范围
compile 编译依赖范围.Maven默认的依赖范围.编译,测试,运行均需要
test    仅仅针对测试的classpath有效.比如JUnit.
provided编译,测试有效.但在运行时无效.比如 servlet-api.jar.他运行时由中间件提供.
runtime 运行时依赖范围.针对测试和运行classpath有效,但是在编译主代码时无效.比如JDBC驱动实现.
system  系统依赖范围.指定本地路径.

依赖排除
  1. <dependency>    
  2.     <groupId>com.gc.user</groupId>    
  3.     <artifactId>user-core</artifactId>    
  4.     <version>0.0.1-SNAPSHOT</version>    
  5.     <exclusions>    
  6.         <!-- 排除user-core模块中关于log4j的依赖 -->    
  7.         <exclusion>    
  8.             <groupId>log4j</groupId>    
  9.             <artifactId>log4j</artifactId>    
  10.         </exclusion>    
  11.     </exclusions>    
  12. </dependency>    

依赖归类
  1. <dependencies>    
  2.   <dependency>    
  3.     <groupId>org.spring.framework</groupId>    
  4.     <artifactId>spring-core</artifactId>    
  5.     <version>${spring.version}</version>    
  6.   </dependency>    
  7.   <dependency>    
  8.     <groupId>org.spring.framework</groupId>    
  9.     <artifactId>spring-beans</artifactId>    
  10.     <version>${spring.version}</version>    
  11.   </dependency>    
  12.   <dependency>    
  13.     <groupId>org.spring.framework</groupId>    
  14.     <artifactId>spring-web</artifactId>    
  15.     <version>${spring.version}</version>    
  16.   </dependency>    
  17.   <dependency>    
  18.     <groupId>org.spring.framework</groupId>    
  19.     <artifactId>spring-mock</artifactId>    
  20.     <version>${spring.version}</version>    
  21.   </dependency>    
  22. </dependencies>    
  23.     
  24. <properties>    
  25.   <spring.version>2.5</spring.version>    
  26. </properties>    

依赖优化
mvn dependency:list
mvn dependency:tree
mvn dependency:analyze(查看声明但是未使用的依赖)

常用插件
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-compiler-plugin</artifactId>  
  4.     <version>3.0</version>  
  5.     <configuration>  
  6.         <source>1.7</source>  
  7.         <target>1.7</target>  
  8.     </configuration>  
  9. </plugin>  
  10. <plugin>  
  11.     <groupId>org.apache.maven.plugins</groupId>  
  12.     <artifactId>maven-surefire-plugin</artifactId>  
  13.     <version>2.12.4</version>  
  14.     <configuration>  
  15.         <!--是否跳过单元测试 -->  
  16.         <skipTests>true</skipTests>  
  17.         <!--是否忽略单元测试错误 -->  
  18.         <testFailureIgnore>true</testFailureIgnore>  
  19.     </configuration>  
  20. </plugin>  
  21. <plugin>  
  22.     <groupId>org.apache.maven.plugins</groupId>  
  23.     <artifactId>maven-archetype-plugin</artifactId>  
  24.     <version>2.3</version>  
  25. </plugin>  
  26. <plugin>  
  27.     <artifactId>maven-source-plugin</artifactId>  
  28.     <version>2.4</version>  
  29.     <configuration>  
  30.         <attach>true</attach>  
  31.     </configuration>  
  32.     <executions>  
  33.         <execution>  
  34.             <phase>compile</phase>  
  35.             <goals>  
  36.                 <goal>jar</goal>  
  37.             </goals>  
  38.         </execution>  
  39.     </executions>  
  40. </plugin>  

  1. <plugin>    
  2.   <artifactId>maven-resources-plugin</artifactId>    
  3.   <executions>    
  4.     <execution>    
  5.       <id>copy-resources</id>    
  6.       <phase>process-resources</phase>    
  7.       <goals>    
  8.         <goal>copy-resources</goal>    
  9.       </goals>    
  10.       <configuration>    
  11.         <outputDirectory>${basedir}/target/classes</outputDirectory>    
  12.         <resources>    
  13.           <resource>    
  14.             <directory>src/main/resources</directory>    
  15.             <includes>    
  16.               <include>jdbc.properties</include>    
  17.             </includes>    
  18.           </resource>    
  19.         </resources>    
  20.       </configuration>    
  21.     </execution>    
  22.   </executions>    
  23. </plugin>    

Maven内置变量
${basedir} 项目根目录
${project.build.directory} 构建目录,缺省为target
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
${project.packaging} 打包类型,缺省为jar
${project.xxx} 当前pom文件的任意节点的内容

聚合继承
主要消除各个模块间重复的引用
父类
  1. <project>  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.juvenxu.sample</groupId>  
  4.   <artifactId>sample-dependency-infrastructure</artifactId>  
  5.   <packaging>pom</packaging>  
  6.   <version>1.0-SNAPSHOT</version>  
  7.   <dependencyManagement>  
  8.     <dependencies>  
  9.         <dependency>  
  10.           <groupId>junit</groupId>  
  11.           <artifactid>junit</artifactId>  
  12.           <version>4.8.2</version>  
  13.           <scope>test</scope>  
  14.         </dependency>  
  15.         <dependency>  
  16.           <groupId>log4j</groupId>  
  17.           <artifactid>log4j</artifactId>  
  18.           <version>1.2.16</version>  
  19.         </dependency>  
  20.     </dependencies>  
  21.   </dependencyManagement>  
  22. </project>  

子类继承
  1. <dependencyManagement>  
  2.   <dependencies>  
  3.       <dependency>  
  4.         <groupId>com.juvenxu.sample</groupId>  
  5.         <artifactid>sample-dependency-infrastructure</artifactId>  
  6.         <version>1.0-SNAPSHOT</version>  
  7.         <type>pom</type>  
  8.         <scope>import</scope>  
  9.       </dependency>  
  10.   </dependencies>  
  11. </dependencyManagement>  
  12.   
  13. <dependency>  
  14.   <groupId>junit</groupId>  
  15.   <artifactid>junit</artifactId>  
  16. </dependency>  
  17. <dependency>  
  18.   <groupId>log4j</groupId>  
  19.   <artifactid>log4j</artifactId>  
  20. </dependency>  


参考:
http://www.infoq.com/cn/news/2011/01/xxb-maven-3-pom-refactoring

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2120430/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: IntelliJ-IDEA快捷键
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339966