maven整理

安装与配置windows版maven(前提:先安装jdk)

1.到http://maven.apache.org下载最新maven3.1.1并解压到D盘,并将解压目录改名为maven3

2.修改D:\maven3\bin\mvn.bat,看脚本,在设置java.exe前设置JAVA_HOME(找到位置加上:set JAVA_HOME=d:\jdk7)

3.打开命令窗口,输入d:\maven3\bin\mvn --version,就可以看到maven信息.

4.输入d:\maven3\bin\mvn help:system,就可以看到下载相关东东到库.默认的库位置在当前用户主目录的.m2目录,按ctrl+c可以终止下载.

5.更改本地库到e:/repository,在默认的库位置所在目录C:\Users\<USER>\.m2,把 repository删了,然后复制D:\maven3\conf的settings.xml到C:\Users\<USER>\.m2,在 setting节点加入<localRepository>e:/repository</localRepository>就 OK,再次输入d:\maven3\bin\mvn help:system看结果


生成项目:

d:\maven3\bin\mvn archetype:generate -DinteractiveMode=false -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.test -DartifactId=testjava -Dversion=1.0-SNAPSHOT

d:\maven3\bin\mvn archetype:generate -DinteractiveMode=false -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.test -DartifactId=testweb -Dversion=1.0-SNAPSHOT


一般构建maven项目,我是不赞成使用上面的生成命令,太难记了,而是使用文本编辑器修改复制出来的pom.xml,再导入IDE工具就可以了.


常见错误:

一..在Eclipse导入已存在工作空间的项目,出现

情况1:

Scanning errors (1):
1 Could not read pom.xml

就是pom.xml出现了不能解析的配置,把不能解析的配置修改过来.例如去掉

 <build>

   <finalName>testweb</finalName>
 </build>

情况2:

No marketplace entries found to handle maven-compiler-plugin:2.3.2:testCompile in Eclipse.  

将Eclipse内置的Maven换成外部自己安装的Maven,重启Eclipse.然后,在命令行执行mvn clean install再导入项目.


二.导入项目后出现:

情况1:

Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix.

在Problems View的出错提示右键选Quick Fix,再按提示确定就OK.或者,右键项目->Maven->Update Project

情况2:

JavaServer Faces 2.2 can not be installed : One or more constraints have not been satisfied.
JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer.

先改web.xmlwebapp节点:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

然后关闭Eclipse,改项目下 的.settings\org.eclipse.wst.common.project.facet.core.xml.将版本改成为3.1,将成后 是<installed facet="jst.web" version="3.1"/>,再启动Eclipse.最后出现上面的情况1,按上面更新下配置就OK.


三.<addMavenDescriptor>false</addMavenDescriptor>在Eclipse无效,仍然会将pom.xml,pom.properties打包入jar.

在CLI(command-line interface)运行命令,此种情况拒绝使用Eclipse来执行命令(当然你测试打包入去没影响)


四.启动tomcat报错:java.util.zip.ZipException: invalid LOC header (bad signature),

启动jetty报错: java.util.zip.ZipException: invalid distance too far back 

以上两种错:一般是一个或多个下载的jar不完整.在eclipse的下载经常出现.而直接在cmd好像不会出错....

tomcat\会提示是那些类...就可推是那个jar.但jetty可能不会指出是那个jar.按常理都是调用外部的maven,并且下载后做文件指纹校验的.真的不明白会有这样的情况.

先用idea一段时间看看...至少eclipse4.3遇到两次了.我都清一次库了,抓狂啊.啊..啊....

对于抛出的异常还是找不到下载不完整的jar,我就写了一个工具.在文末我会贴上.


五.没用web.xml的web,打包成war文件,报错: Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) 

只需要在web模块下的pom.xml下的build节点增加以下代码,重写一下maven-war-plugin组件就可以.重点在于下面的 configuration.   plugins是build下在同的节点,与pluginManagement是兄弟节点.其中,下面的executions节点可以删除,留默认值.

  1. <plugins>  
  2.             <plugin>  
  3.                 <artifactId>maven-war-plugin</artifactId>  
  4.                 <version>2.2</version>  
  5.                 <configuration>  
  6.                     <failOnMissingWebXml>false</failOnMissingWebXml>  
  7.                 </configuration>  
  8.                 <executions>  
  9.                     <execution>  
  10.                         <id>default-war</id>  
  11.                         <phase>package</phase>  
  12.                         <goals>  
  13.                             <goal>war</goal>  
  14.                         </goals>  
  15.                     </execution>  
  16.                 </executions>  
  17.             </plugin>  
  18.         </plugins>  

六.依赖解析时,如果本地库不存在此依赖,就会从远程仓库下载,在这个神奇的国度,下载总有些时候会断开,根本不能访问,一般从命令输出控制台都可以看到下载的前缀(甚至全路径),比如显示https://repo.maven.apache.org/maven2,那么,建议你可以使用浏览器直接打开该地址,如果发觉不能打开,那这个时刻你要想下载,换一下远程仓库是有需要的.最简单的方法是在项目pom下的project子节点添加如下内容来覆盖默认的远程中央仓库.可以先在浏览器尝试能不能打开http://repo1.maven.org/maven2,剩下的你都懂.

  1. <repositories>  
  2.     <repository>  
  3.         <id>central</id>  
  4.         <name>Central Repository</name>  
  5.         <url>http://repo1.maven.org/maven2</url>  
  6.         <layout>default</layout>  
  7.         <snapshots>  
  8.             <enabled>false</enabled>  
  9.         </snapshots>  
  10.     </repository>  
  11. </repositories>  

 

安装jar到本地maven库例子.

d:\maven\bin\mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc14-10.2.0.4.0.jar

maven的5种scope对于3种class是否有效:

  1.         compile test    runtime example  
  2. compile     Y   Y   Y   spring-core  
  3. test        -   Y   -   junit  
  4. provided    Y   Y   -   servlet-api  
  5. runtime     -   Y   Y   jdbc驱动  
  6. system      Y   Y   -   本地的,maven仓库之外的类库文件  


左边为第一直接依赖,上边为第二直接依赖,则对应的依赖传递如下:

  1.         compile     test        provided    runtime  
  2. compile     compile     -       -       runtime  
  3. test        test        -       -       test  
  4. provided    provided    -       provided    provided  
  5. runtime     runtime     -       -       runtime  



依赖调解两原则:

1.路径最近者优先

2.路径长度相同,解析依赖声明靠前优先


出现依赖冲突就是靠这两原则去解决:一般做法,将想要的依赖靠前在顶层声明.


找出maven下载出错的文件工具类(我170多M左右的maven库,一分钟左右就检查完毕,最终确实有几个下载不完整的文件)

  1. import java.io.*;  
  2. import java.security.MessageDigest;  
  3. import java.security.NoSuchAlgorithmException;  
  4.   
  5. /** 
  6.  * 找出maven下载出错的文件 
  7.  * Created by xiejx618 on 14-4-1. 
  8.  */  
  9. public class TestUtil {  
  10.     private static MessageDigest messageDigest = null;  
  11.     static {  
  12.         try {  
  13.             messageDigest= MessageDigest.getInstance("SHA1");  
  14.         }catch (NoSuchAlgorithmException e){  
  15.             e.printStackTrace();  
  16.         }  
  17.     }  
  18.     private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5','6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };  
  19.     private static final int BUFFER_SIZE = 4096;  
  20.   
  21.     /** 
  22.      * 以16进制字符串形式返回数据的sha1 
  23.      * @param data 
  24.      * @return 
  25.      */  
  26.     public static String digestData(byte[] data) {  
  27.         messageDigest.update(data);  
  28.         data=messageDigest.digest();  
  29.         int len = data.length;  
  30.         StringBuilder buf = new StringBuilder(len * 2);  
  31.         for (int j = 0; j < len; j++) {  
  32.             buf.append(HEX_DIGITS[(data[j] >> 4) & 0x0f]).append(HEX_DIGITS[data[j] & 0x0f]);  
  33.         }  
  34.         return buf.toString();  
  35.     }  
  36.     /** 
  37.      * 根据文件与编码以String形式返回文件的数据 
  38.      * @param file 
  39.      * @param charset 
  40.      * @return 
  41.      */  
  42.     public static String getFileString(File file, String charset){  
  43.         InputStreamReader reader=null;  
  44.         StringBuilder out = null;  
  45.         try {  
  46.             reader = new InputStreamReader(new FileInputStream(file), charset);  
  47.             out = new StringBuilder();  
  48.             char[] buffer = new char[BUFFER_SIZE];  
  49.             int bytesRead = -1;  
  50.             while ((bytesRead = reader.read(buffer)) != -1) {  
  51.                 out.append(buffer, 0, bytesRead);  
  52.             }  
  53.         }catch (IOException e){  
  54.             e.printStackTrace();  
  55.         }finally {  
  56.             try {  
  57.                 if (reader!=null)  reader.close();  
  58.             } catch (IOException ex) {  
  59.                 ex.printStackTrace();  
  60.             }  
  61.         }  
  62.         return out.toString();  
  63.     }  
  64.   
  65.     /** 
  66.      * 根据文件以byte[]形式返回文件的数据 
  67.      * @param file 
  68.      * @return 
  69.      */  
  70.     public static byte[] getFileData(File file){  
  71.         FileInputStream in=null;  
  72.         ByteArrayOutputStream out=null;  
  73.         try {  
  74.             in=new FileInputStream(file);  
  75.             out=new ByteArrayOutputStream(BUFFER_SIZE);  
  76.             int byteCount = 0;  
  77.             byte[] buffer = new byte[BUFFER_SIZE];  
  78.             int bytesRead = -1;  
  79.             while ((bytesRead = in.read(buffer)) != -1) {  
  80.                 out.write(buffer, 0, bytesRead);  
  81.                 byteCount += bytesRead;  
  82.             }  
  83.             out.flush();  
  84.         } catch (IOException e) {  
  85.             e.printStackTrace();  
  86.         } finally {  
  87.             try{  
  88.                 if (in!=null) in.close();  
  89.                 if (out!=null) out.close();  
  90.             }catch (IOException ex){  
  91.                 ex.printStackTrace();  
  92.             }  
  93.         }  
  94.         return out.toByteArray();  
  95.     }  
  96.   
  97.     /** 
  98.      * 凡是以.sha1的文件结尾的文件,先将文件名去掉.sha1找到文件sourcefile,获取sourcefile文件的真实的sha1和从这个.sha1文件获取sha1, 
  99.      * 进行比照,如果不匹配,输出实际的sha1和期望的sha1 
  100.      * 
  101.      * @param file 
  102.      */  
  103.     public static final void handle(File file){  
  104.         String filename=file.getName();  
  105.         if(filename.endsWith(".sha1")){  
  106.             String sourcename=filename.substring(0, filename.lastIndexOf('.'));  
  107.             File sourcefile=new File(file.getParent(),sourcename);  
  108.             byte[] sourcedata=getFileData(sourcefile);  
  109.             String sha1Real=digestData(sourcedata);  
  110.             String content=getFileString(file,"UTF-8");  
  111.             String sha1Check=content.split(" ")[0].trim();  
  112.             if(!sha1Real.equalsIgnoreCase(sha1Check)){  
  113.                 System.out.println(sourcefile.getAbsolutePath()+"的实际SHA1:"+sha1Real);  
  114.                 System.out.println(file.getAbsolutePath()+"的验证SHA1:"+sha1Check);  
  115.             }  
  116.         }  
  117.     }  
  118.   
  119.     /** 
  120.      * 根据目录dir,递归所有的文件按handle方法处理 
  121.      * @param dir 
  122.      */  
  123.     public static final void showAllFiles(File dir){  
  124.         File[] fs = dir.listFiles();  
  125.         for(int i=0; i<fs.length; i++){  
  126.             if(fs[i].isDirectory()){  
  127.                 showAllFiles(fs[i]);  
  128.             }  
  129.             handle(fs[i]);  
  130.         }  
  131.     }  
  132.     public static void main(String[] args) {  
  133.         showAllFiles(new File("E:/mvnrepo0329"));  
  134.         System.out.println("the end");  
  135.     }  
  136. }  


posted @ 2016-04-27 13:52  向南君  阅读(147)  评论(0)    收藏  举报