大龙的博客

常用链接

统计

最新评论

Maven POM 配置技巧

Java代码  收藏代码
  1. 一、排除传递依赖的某个jar,以避免版本冲突。例如: 开发工程中需要引用struts2(2.0.11.2)和freemarker(2.3.16),但该struts2默认依赖的是freemarker(2.3.8),可参考以下方式编写:  
  2.   
  3. view plain  
  4. <dependency>    
  5.     <groupId>org.apache.struts</groupId>    
  6.     <artifactId>struts2-core</artifactId>    
  7.     <version>2.0.11.2</version>    
  8.     <type>jar</type>    
  9.     <scope>compile</scope>    
  10.     <exclusions>    
  11.       <exclusion> <!-- 排除freemarker,以避免版本冲突 -->    
  12.     <groupId>freemarker</groupId>    
  13.     <artifactId>freemarker</artifactId>    
  14.    </exclusion>    
  15.  </exclusions>    
  16. </dependency>    
  17. <dependency>    
  18.     <groupId>org.freemarker</groupId>    
  19.     <artifactId>freemarker</artifactId>    
  20.     <version>2.3.16</version>    
  21.     <type>jar</type>    
  22.     <scope>compile</scope>    
  23. </dependency>    
  24.    
  25.   
  26.    
  27.   
  28. 二、发布jar包同时发布源码  
  29.   
  30. view plain  
  31.     <plugin>    
  32. <groupId>org.apache.maven.plugins</groupId>    
  33. <artifactId>maven-source-plugin</artifactId>    
  34. <version>2.1.2</version>    
  35. <configuration>    
  36.     <attach>true</attach>    
  37. </configuration>    
  38. <executions>    
  39.     <execution>    
  40.         <phase>compile</phase>    
  41.         <goals>    
  42.             <goal>jar</goal>    
  43.         </goals>    
  44.     </execution>    
  45. </executions>             
  46.     </plugin>    
  47.    
  48.   
  49.    
  50.   
  51. 三、Eclipse开发时,为方便使用内置tomcat调试,需将所依赖的jar包复制到WEB-INF/lib目录下。可通过以下配置:  
  52.   
  53. view plain  
  54. <plugin>    
  55.     <groupId>org.apache.maven.plugins</groupId>    
  56.     <artifactId>maven-dependency-plugin</artifactId>    
  57.     <version>2.1</version>    
  58.     <executions>    
  59.            <execution>    
  60.                <id>copy</id>    
  61.                <phase>package</phase>    
  62.                <goals>    
  63.                    <goal>copy-dependencies</goal>    
  64.                </goals>    
  65.                <configuration>    
  66.                    <outputDirectory>src/main/webapp/WEB-INF/lib</outputDirectory>    
  67.                </configuration>    
  68.            </execution>    
  69.         </executions>     
  70. </plugin>    
  71.    
  72. 配置完成后。每次有添加jar包依赖时,需运行一次mvn package命令。  
  73.   
  74.    
  75.   
  76.    
  77.   
  78. 四、有时工程中想复用其它工程中的静态内容(image/css/js),为方便开发调试,需将相应的静态内容,解压缩到当前工程的webapp目录下。可通过以下配置:  
  79. view plain  
  80. <plugin>    
  81.     <groupId>org.apache.maven.plugins</groupId>    
  82.     <artifactId>maven-dependency-plugin</artifactId>    
  83.     <version>2.1</version>    
  84.     <executions>    
  85.            <execution>    
  86.                <id>copy-statics</id>    
  87.                <phase>generate-resources</phase>    
  88.                <goals>    
  89.                    <goal>unpack</goal>    
  90.                </goals>    
  91.                <configuration>    
  92.                 <artifactItems>    
  93.               <artifactItem>    
  94.         <groupId>com.yihaodian.front</groupId>    
  95.         <artifactId>front-global</artifactId>    
  96.         <version>1.0-SNAPSHOT</version>    
  97.         <classifier>statics</classifier>    
  98.         <type>zip</type>    
  99.                  <overWrite>true</overWrite>    
  100.                  <outputDirectory>src/main/webapp</outputDirectory>    
  101.               </artifactItem>    
  102.             </artifactItems>    
  103.                </configuration>    
  104.            </execution>    
  105.         </executions>    
  106. </plugin>    
  107.    
  108.   
  109. 配置完成后,需运行一次mvn generate-resources命令。 

posted on 2012-08-31 10:25 大龙 阅读(1233) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理