`

Maven POM 配置技巧(一)

阅读更多
一、排除传递依赖的某个jar,以避免版本冲突。例如: 开发工程中需要引用struts2(2.0.11.2)和freemarker(2.3.16),但该struts2默认依赖的是freemarker(2.3.8),可参考以下方式编写:

view plain
<dependency>  
    <groupId>org.apache.struts</groupId>  
    <artifactId>struts2-core</artifactId>  
    <version>2.0.11.2</version>  
    <type>jar</type>  
    <scope>compile</scope>  
    <exclusions>  
      <exclusion> <!-- 排除freemarker,以避免版本冲突 -->  
    <groupId>freemarker</groupId>  
    <artifactId>freemarker</artifactId>  
   </exclusion>  
 </exclusions>  
</dependency>  
<dependency>  
    <groupId>org.freemarker</groupId>  
    <artifactId>freemarker</artifactId>  
    <version>2.3.16</version>  
    <type>jar</type>  
    <scope>compile</scope>  
</dependency>  
 

 

二、发布jar包同时发布源码

view plain
    <plugin>  
<groupId>org.apache.maven.plugins</groupId>  
<artifactId>maven-source-plugin</artifactId>  
<version>2.1.2</version>  
<configuration>  
    <attach>true</attach>  
</configuration>  
<executions>  
    <execution>  
        <phase>compile</phase>  
        <goals>  
            <goal>jar</goal>  
        </goals>  
    </execution>  
</executions>           
    </plugin>  
 

 

三、Eclipse开发时,为方便使用内置tomcat调试,需将所依赖的jar包复制到WEB-INF/lib目录下。可通过以下配置:

view plain
<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-dependency-plugin</artifactId>  
    <version>2.1</version>  
    <executions>  
           <execution>  
               <id>copy</id>  
               <phase>package</phase>  
               <goals>  
                   <goal>copy-dependencies</goal>  
               </goals>  
               <configuration>  
                   <outputDirectory>src/main/webapp/WEB-INF/lib</outputDirectory>  
               </configuration>  
           </execution>  
        </executions>   
</plugin>  
 
配置完成后。每次有添加jar包依赖时,需运行一次mvn package命令。

 

 

四、有时工程中想复用其它工程中的静态内容(image/css/js),为方便开发调试,需将相应的静态内容,解压缩到当前工程的webapp目录下。可通过以下配置:
view plain
<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-dependency-plugin</artifactId>  
    <version>2.1</version>  
    <executions>  
           <execution>  
               <id>copy-statics</id>  
               <phase>generate-resources</phase>  
               <goals>  
                   <goal>unpack</goal>  
               </goals>  
               <configuration>  
                <artifactItems>  
              <artifactItem>  
        <groupId>com.yihaodian.front</groupId>  
        <artifactId>front-global</artifactId>  
        <version>1.0-SNAPSHOT</version>  
        <classifier>statics</classifier>  
        <type>zip</type>  
                 <overWrite>true</overWrite>  
                 <outputDirectory>src/main/webapp</outputDirectory>  
              </artifactItem>  
            </artifactItems>  
               </configuration>  
           </execution>  
        </executions>  
</plugin>  
 

配置完成后,需运行一次mvn generate-resources命令。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics