Maven學習

壹頁書發表於2016-06-19
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>  


參考:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2120430/,如需轉載,請註明出處,否則將追究法律責任。

相關文章