maven整理
2013-09-26 21:36
323 查看
安装与配置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节点可以删除,留默认值.
六.依赖解析时,如果本地库不存在此依赖,就会从远程仓库下载,在这个神奇的国度,下载总有些时候会断开,根本不能访问,一般从命令输出控制台都可以看到下载的前缀(甚至全路径),比如显示https://repo.maven.apache.org/maven2,那么,建议你可以使用浏览器直接打开该地址,如果发觉不能打开,那这个时刻你要想下载,换一下远程仓库是有需要的.最简单的方法是在项目pom下的project子节点添加如下内容来覆盖默认的远程中央仓库.可以先在浏览器尝试能不能打开http://repo1.maven.org/maven2,剩下的你都懂.
安装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 -Dsources=ojdbc14-10.2.0.4.0-source.jar
maven的5种scope对于3种class是否有效:
左边为第一直接依赖,上边为第二直接依赖,则对应的依赖传递如下:
依赖调解两原则:
1.路径最近者优先
2.路径长度相同,解析依赖声明靠前优先
出现依赖冲突就是靠这两原则去解决:一般做法,将想要的依赖靠前在顶层声明.
找出maven下载出错的文件工具类(我170多M左右的maven库,一分钟左右就检查完毕,最终确实有几个下载不完整的文件)
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工具就可以了.
关于maven-war-plugin的几个实用的配置参数 archiveClasses: Whether a JAR file will be created for the classes in the webapp. Using this optional configuration parameter will make the compiled classes to be archived into a JAR file and the classes directory will then be excluded from the webapp 是否为webapp里的类文件创建一个jar文件.使用此选项配置参数将编译类归档进一个jar文件,并且类目录会从webapp中排除. 这个jar文件也会被放进war包的WEB-INF/lib目录下 attachClasses: Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the project as an additional artifact. By default the classifier for the additional artifact is 'classes'. You can change it with the <pre><![CDATA[<classesClassifier>someclassifier</classesClassifier>]]></pre> parameter. If this parameter true, another project can depend on the classes by writing something like: <pre><![CDATA[<dependency> <groupId>myGroup</groupId> <artifactId>myArtifact</artifactId> <version>myVersion</myVersion> <classifier>classes</classifier> </dependency>]]></pre> 类( WEB-INF/classes目录下的内容)是否应作为一个附加构件附加到项目.附加构件的默认classifier是classes.你可以使用<pre><classesClassifier>someclassifier</classesClassifier></pre>参数来更改.如果改了此参数,另一个项目可以通过像这样写:<pre><dependency> <groupId>myGroup</groupId> <artifactId>myArtifact</artifactId> <version>myVersion</myVersion> <classifier>classes</classifier> </dependency></pre>来依赖这些类 failOnMissingWebXml: 没有web.xml的web,打包时要改为false. overlays:这个是比较多的功能.详细看http://maven.apache.org/plugins/maven-war-plugin/overlays.html
常见错误:
一..在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节点可以删除,留默认值.
<plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> <executions> <execution> <id>default-war</id> <phase>package</phase> <goals> <goal>war</goal> </goals> </execution> </executions> </plugin> </plugins>
六.依赖解析时,如果本地库不存在此依赖,就会从远程仓库下载,在这个神奇的国度,下载总有些时候会断开,根本不能访问,一般从命令输出控制台都可以看到下载的前缀(甚至全路径),比如显示https://repo.maven.apache.org/maven2,那么,建议你可以使用浏览器直接打开该地址,如果发觉不能打开,那这个时刻你要想下载,换一下远程仓库是有需要的.最简单的方法是在项目pom下的project子节点添加如下内容来覆盖默认的远程中央仓库.可以先在浏览器尝试能不能打开http://repo1.maven.org/maven2,剩下的你都懂.
<repositories> <repository> <id>central</id> <name>Central Repository</name> <url>http://repo1.maven.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </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 -Dsources=ojdbc14-10.2.0.4.0-source.jar
maven的5种scope对于3种class是否有效:
compile test runtime example compile Y Y Y spring-core test - Y - junit provided Y Y - servlet-api runtime - Y Y jdbc驱动 system Y Y - 本地的,maven仓库之外的类库文件
左边为第一直接依赖,上边为第二直接依赖,则对应的依赖传递如下:
compile test provided runtime compile compile - - runtime test test - - test provided provided - provided provided runtime runtime - - runtime
依赖调解两原则:
1.路径最近者优先
2.路径长度相同,解析依赖声明靠前优先
出现依赖冲突就是靠这两原则去解决:一般做法,将想要的依赖靠前在顶层声明.
找出maven下载出错的文件工具类(我170多M左右的maven库,一分钟左右就检查完毕,最终确实有几个下载不完整的文件)
import java.io.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 找出maven下载出错的文件 * Created by xiejx618 on 14-4-1. */ public class TestUtil { private static MessageDigest messageDigest = null; static { try { messageDigest= MessageDigest.getInstance("SHA1"); }catch (NoSuchAlgorithmException e){ e.printStackTrace(); } } private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5','6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static final int BUFFER_SIZE = 4096; /** * 以16进制字符串形式返回数据的sha1 * @param data * @return */ public static String digestData(byte[] data) { messageDigest.update(data); data=messageDigest.digest(); int len = data.length; StringBuilder buf = new StringBuilder(len * 2); for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(data[j] >> 4) & 0x0f]).append(HEX_DIGITS[data[j] & 0x0f]); } return buf.toString(); } /** * 根据文件与编码以String形式返回文件的数据 * @param file * @param charset * @return */ public static String getFileString(File file, String charset){ InputStreamReader reader=null; StringBuilder out = null; try { reader = new InputStreamReader(new FileInputStream(file), charset); out = new StringBuilder(); char[] buffer = new char[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = reader.read(buffer)) != -1) { out.append(buffer, 0, bytesRead); } }catch (IOException e){ e.printStackTrace(); }finally { try { if (reader!=null) reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } return out.toString(); } /** * 根据文件以byte[]形式返回文件的数据 * @param file * @return */ public static byte[] getFileData(File file){ FileInputStream in=null; ByteArrayOutputStream out=null; try { in=new FileInputStream(file); out=new ByteArrayOutputStream(BUFFER_SIZE); int byteCount = 0; byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); byteCount += bytesRead; } out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try{ if (in!=null) in.close(); if (out!=null) out.close(); }catch (IOException ex){ ex.printStackTrace(); } } return out.toByteArray(); } /** * 凡是以.sha1的文件结尾的文件,先将文件名去掉.sha1找到文件sourcefile,获取sourcefile文件的真实的sha1和从这个.sha1文件获取sha1, * 进行比照,如果不匹配,输出实际的sha1和期望的sha1 * * @param file */ public static final void handle(File file){ String filename=file.getName(); if(filename.endsWith(".sha1")){ String sourcename=filename.substring(0, filename.lastIndexOf('.')); File sourcefile=new File(file.getParent(),sourcename); byte[] sourcedata=getFileData(sourcefile); String sha1Real=digestData(sourcedata); String content=getFileString(file,"UTF-8"); String sha1Check=content.split(" ")[0].trim(); if(!sha1Real.equalsIgnoreCase(sha1Check)){ System.out.println(sourcefile.getAbsolutePath()+"的实际SHA1:"+sha1Real); System.out.println(file.getAbsolutePath()+"的验证SHA1:"+sha1Check); } } } /** * 根据目录dir,递归所有的文件按handle方法处理 * @param dir */ public static final void showAllFiles(File dir){ File[] fs = dir.listFiles(); for(int i=0; i<fs.length; i++){ if(fs[i].isDirectory()){ showAllFiles(fs[i]); } handle(fs[i]); } } public static void main(String[] args) { showAllFiles(new File("E:/mvnrepo0329")); System.out.println("the end"); } }
相关文章推荐
- 《Maven权威指南》学习笔记四_创建一个简单的Maven项目
- 《Maven权威指南》学习笔记十七_一个多模块项目 3ff0
- 《Maven权威指南》学习笔记十八_多模块企业级项目 3ff0
- 《Maven权威指南》学习笔记二十_关于POM
- 近日maven学习概况
- Eclipse Maven Tomcat的利用配置
- (转)Vaadin 使用 Maven
- Maven和Eclipse联合开发
- Maven intall OR Maven build
- maven flexmojos 插件安装配置
- Maven依赖
- Maven的生命周期
- Spring AOP schema找不到报错
- myeclipse中maven3的使用,部署web项目到tomcat7
- Maven的使用,Nexus建立本地仓库以及Eclipse导入Maven项目(一): 准备篇
- Maven项目的配置和创建
- 初步认识Maven之POM文件
- Maven仓库之安装Nexus
- 2011-7-8