Maven问题总结
(1)Maven archetype:generate
(2)mkdir src\main\resources,src\test\resources
(3)添加parent
<parent> <groupId>com.alibaba</groupId> <artifactId>pampas</artifactId> <version>3-RC1</version> </parent>
由于在3-RC1中的使用做了黑白名单的限制,具体限制为:
http://repo.alibaba-inc.com/mvn/wl.txt
com.alibaba.*
com.alifi.*
com.taobao.security
com.taobao.util
或者
(4)查询maven仓库
External仓库 :
http://svn.alibaba-inc.com/repos/binary/repository/
external的地址:
http://repo.alibaba-inc.com:9091/external , 它代理了maven的三方库,
http://svn.alibaba-inc.com/repos/binary/repository/
(5) 官方Plugin在仓库中的位置
.m2\repository\org\apache\maven\plugins
(6)一方库、二方库、三方库
一方库:本工程中的相互依赖
二方库:公司内部的依赖库,一般指公司内部的其他项目发布的jar包
三方库:公司之外的开源库, 比如apache、ibm、google等发布的依赖
(7)、maven插件源代码地址和jira地址汇总
1、Maven指定编译级别
用 maven来编译java类时,一个类总是出现不支持泛型的错误提示。泛型是从JDK5.0+才开始支持的,而我在eclipse中所用的JRE版本都是 5.0以上的。后来才发现必须在pom.xml文件中加入下一面一段语句来指定所用的版本,尽管在eclipse中配置了正确的“Build Path”。
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <target>1.6</target> <source>1.6</source> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>自定资源文件的编码设置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.3</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin>
指定使用的单元测试框架
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <junitArtifactName>com.alibaba.external:test.junit</junitArtifactName> <testNGArtifactName>com.alibaba.external:test.testng</testNGArtifactName> <version>2.5</version> <excludes> <exclude>**/IT*.java</exclude> <exclude>**/*Bean.java</exclude> <exclude>**/TestHelper.java</exclude> </excludes> </configuration> </plugin>
经过验证的排除单元测试实例:
<
exclude
>
com/alibaba/external/junit/*.java
</
exclude
>表示为 排除
com.alibaba.external.junit包下面的所有java文件。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <junitArtifactName>com.alibaba.external:test.junit </junitArtifactName> <testNGArtifactName>com.alibaba.external:test.testng </testNGArtifactName> <version>2.5</version> <excludes> <exclude>com/alibaba/external/junit/*.java</exclude> </excludes> </configuration> </plugin>
或者
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
<repositories> <repository> <id>args4j-repository</id> <url>http://download.java.net/maven/2/</url> </repository> </repositories>
父pom中自定义,子模块从此pom中继承:
<properties> <cms.groupId>com.alibaba.cms</cms.groupId> <cms.version>1.0.0</cms.version> </properties> <groupId>${cms.groupId}</groupId> <version>${cms.version}</version>
【注意】我们已经通过在项目的构建配置中加入 finalName 元素来自定义这个生成的
WAR 文件的名称。根据 simple-webapp 的 finalName ,package 阶段生成的 WAR 文件
为 target/simple-webapp.war 。
2、Eclipse的编译级别
project工程名->Properties->Java Compiler->Compiler compilance level使用1.5以上的
且勾上‘use default compliance settings’
3、Eclipse中出现maven的相关依赖
右键点击工程名 -> maven -> Update Project Configuration , 则会出现一个叫Maven Dependencies的libaray包。
4、Maven排除应用的jar包
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> </dependency>
5、在maven中可能存在如下的错误
写道
Failure executing javac, but could not parse the error:
错误:读取 C:\Documents and Settings\keju.wangkj\.m2\repository\com\sun\jdmk\jmxtools\1.2.1\jmxtools
-1.2.1.jar 时出错;error in opening zip file
错误:读取 C:\Documents and Settings\keju.wangkj\.m2\repository\com\sun\jmx\jmxri\1.2.1\jmxri-1.2.1.
jar 时出错;error in opening zip file
2 错误
错误:读取 C:\Documents and Settings\keju.wangkj\.m2\repository\com\sun\jdmk\jmxtools\1.2.1\jmxtools
-1.2.1.jar 时出错;error in opening zip file
错误:读取 C:\Documents and Settings\keju.wangkj\.m2\repository\com\sun\jmx\jmxri\1.2.1\jmxri-1.2.1.
jar 时出错;error in opening zip file
2 错误
解决方案:由于maven下载了空壳的jar包在仓库中,只有1k左右,删除之后让maven重新下载
6、Maven打包依赖
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
执行命令:mvn assembly:assembly , 执行完成之后会把相关的依赖一起打包
可直接使用:mvn install assembly:assembly
生成的jar中可能存在重复文件的问题,是版本的问题,参考:
http://www.wsria.com/archives/958 。解决方案如下:
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
7、创建resource文件
写道
mkdir -p src/{main/java/com/mycompany/webapp,test/java/com/mycompany/webapp,test/resources}
【注意】这是在linux下运行的, 而在windows下面使用'\',且不用'-p'
8、修改工程的仓库
1.Maven2的依赖库查询
http://www.mvnrepository.com/
2.更改默认的maven2的仓库,需在pom.xml文件中添加设置(如有私服须放置在前面)
http://www.mvnrepository.com/
2.更改默认的maven2的仓库,需在pom.xml文件中添加设置(如有私服须放置在前面)
< repositories> < repository> < id>ibiblio< /id> < name>Ibiblio Maven Repository< /name> < url>http://www.ibiblio.org/maven2< /url> < layout>default< /layout> < /repository> < repository> < id>maven< /id> < name>Slowly office site< /name> < url>http://repo1.maven.org/maven2< /url> < /repository> < /repositories> <repositories><repository> </repository></repositories>
------------------------------------------------------------------------------------------
修改%M2_HOME%/conf/settings.xml即可
确实是改settings.xml比较一劳永逸,每个项目的pom都去加repository对配置管理是不利的。
9、Maven版本差别造成的问题
在2.0.9 下编译完全没有问题, 而在2.0.10以 上的版本编译的哦时候出现以下问题:
写道
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error configuring: org.apache.maven.plugins:maven-antrun-plugin. Reason: java.lang.NoSuchMethodError: org.apache.tools.ant.util.FileUtils.close(Ljava/io/InputStream;)V
[INFO] ------------------------------------------------------------------------
[INFO] Error configuring: org.apache.maven.plugins:maven-antrun-plugin. Reason: java.lang.NoSuchMethodError: org.apache.tools.ant.util.FileUtils.close(Ljava/io/InputStream;)V
原理分析和解决方案:
由于多个包里面的引用的相同jar包的版本有问题,需要使用
<exlutions></exlutions>
10、Maven问题排查方式
如何诊断在Maven构建过程中碰到的问题?可以看一下这些手段:
1.
你正运行什么版本的Maven? 如 果不是Maven 2.0.9或以上,你需要升级。 Maven 2.0.9是第一个设置了核心插件版本的Maven版本。为什么这很重要呢?因为在2.0.9之前,Maven会自动下载核心插件的最新版本,比如jar 插件和compiler插件。而最新版本插件由于其不稳定性可能导致你的构建失败。因此,如果你还在使用2.0.9以下的Maven,升级吧。
2. 有时候遇到奇怪问题的时候,清理一下你的本地仓库。检查一下snapshots的更新策略。
3. 使用:
mvn help:effective-pom 查看有效POM
mvn help:effective-settings 查看有效settings
mvn help:active-profiles 查看激活的profile
这 些命令会检查所有的的pom文件,settings文件,profile文件,帮助你查看其中的差异和影响。可能有些人在~/.m2 /settings.xml 或者M2_HOME/conf/settings.xml做了自定义。(详细请参考http://www.sonatype.com/book /reference_zh/installation.html)
4. 你是否正使用公共仓库?时候有两台机器访问不同的仓库?如果你没有使用Nexus,
我建议你下载并运行使用它 ,它能让你更好的了解配置仓库,并且是一个很好的发布构件的地方。
-~-~—~-~-~-~-~
我再补充两点:
5. 使用-X参数查看Maven详细运行情况,如
mvn clean install -X ,让Maven打开debug日志。
6. 将完整的异常信息,pom.xml, settings.xml 发到我们的讨论组:
http://groups.google.com/group/maven-zh ,这里总有热心的人帮助你。
11、自定义Maven本地仓库的位置
可以在运行时指定本地仓库位置:
mvn clean install -Dmaven.repo.local=/home/juven/myrepo/
Maven缺省的本地仓库地址为
${user.home}/.m2/repository 。也就是说,一个用户会对应的拥有一个本地仓库。
你也可以自定义本地仓库的位置,修改
${user.home}/.m2/settings.xml :
<settings> ... <localRepository>D:\java\repository</localRepository> ... </settings>
12、执行main函数
java -cp target\helloworld-1.0-SNAPSHOT.jar org.marshal.App
13、为Web项目增加WTP支持
写道
mvn -Dwtpversion文字颜色=1.0 eclipse:eclipse
什么是eclipse wtp 中的wtp
wtp(web tools platform )项目在eclipse平台上进行扩展,是一个开发j2ee web应用程序的工具集。wtp包含以下工具:
* 一个源码编辑器
www.ssbbww.com 用来编辑html, javascript, css, jsp, sql, xml, dtd, xsd, 和wsdl。
* 一个图形编辑器用来编辑xsd与wsdl。
* j2ee项目构建器和一个j2ee向导工具。
* 一个web服务创建向导和管理器,和ws-i 测试工具。
* 一个数据库访问,查询工具等。
wtp由两个子项目构成:wst(web标准工具集) 与jst(j2ee标准工具集)8ttt8 wtp是什么意思 是什么东西_什么叫-什么是wtp
wtp支付愿意原则,用于分析社会成员为项目所产出的效益愿意支付的价值。
14、如何定制工程的目录结构
15、在Eclipse中Maven报错:
Maven中无法找 org.apache.maven:maven-artifact:jar:2.0.9,需要手动下载等
写道
Description Resource Path Location Type
Missing:
----------
1) org.apache.maven:maven-artifact:jar:2.0.9
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.apache.maven -DartifactId=maven-artifact -Dversion=2.0.9 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=org.apache.maven -DartifactId=maven-artifact -Dversion=2.0.9 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) org.apache.maven.plugins:maven-enforcer-plugin:maven-plugin:1.0-beta-1
2) org.apache.maven:maven-artifact:jar:2.0.9
Missing:
----------
1) org.apache.maven:maven-artifact:jar:2.0.9
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.apache.maven -DartifactId=maven-artifact -Dversion=2.0.9 -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=org.apache.maven -DartifactId=maven-artifact -Dversion=2.0.9 -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) org.apache.maven.plugins:maven-enforcer-plugin:maven-plugin:1.0-beta-1
2) org.apache.maven:maven-artifact:jar:2.0.9
解决方案:
1、在eclipse中检查自己使用的 maven版本和eclipse的版本
2、检查eclipse使用的maven的相关配置: Windows -> Preferences -> Maven -> Installations , 查看 本地user级别settings.xml, global级别settings.xml, 本地仓库 等
16、mvn assembly:assembly中出现重复的class文件
最近学习Maven遇到一个问题,使用
写道
mvn assembly:assembly
打包项目里面有重复的class文件,如图:
后来Google了一下也没有找到解决办法,在Maven群里面有人说版本问题,然后就指定了maven-assembly-plugin 插件的版本号,虽然是Beta版的,解决问题就可以了,配置如下:
17、使maven2在下载依赖包的同时下载其源代码包。
使maven2在下载依赖包的同时下载其源代码包的方法:
1. 使用maven命令:mvn dependency:sources 下载依赖包的源代码。
2. 使用参数: -DdownloadSources=true 下载源代码jar。 -DdownloadJavadocs=true 下载javadoc包。
18、生成目录结构
Windows下demo
写道
mkdir src\main\resources,src\test\resources
再使用mvn eclipse:eclipse之后,再导入eclipse中(如果已经在eclipse中的,只需要刷新整个eclipse工程即可)
windows下,创建完整的目录结构
写道
mkdir src\main\java\com\alibaba, src\main\resources, src\test\java\com\alibaba,src\test\resources
19、仓库依赖查询
20、自动报告的生成:mvn site
<reporting>
<outputDirectory>target/site</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <configuration> <instrumentation> <!--<ignore>.*</ignore> --> <excludes> <exclude>**/*Test.class</exclude> </excludes> </instrumentation> </configuration> <executions> <execution> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin>
在使用cobertura的时候有一个限制, 就是如果test跟要测试的project是分离的, 换句话说如果test是一个单独的project, 那么这个貌似是没法对被测试的project统计代码覆盖率, 而 clover 则没有这个限制, 要钱的就是不一样啊!
21、mvn autoconf
描述:autoconfig工具的封装,允许在编译时,或者编译后进行参数配置。 注 意:如果你使用了m2eclipse,发现eclipse进程挂起,请在eclipse.ini文件里面加上 -DinteractiveMode=false,这样eclipse在编译项目的时候就不会执行autoconfig了,需要执行 autoconfig,请在命令行使用mvn autoconf:config
写道
mvn autoconf:autoconf -Dproperties=/home/jiangyr/antx_subscription.properties
22、mvn帮助插件
写道
help:active-profiles
列出当前构建中活动的Profile(项目的,用户的,全局的)。
help:effective-pom
显示当前构建的实际POM,包含活动的Profile。
help:effective-settings
打印出项目的实际settings, 包括从全局的settings和用户级别settings继承的
配置。
help:describe
描述插件的属性。它不需要在项目目录下运行。但是你必须提供你想要描述插件
的 groupId 和 artifactId。
列出当前构建中活动的Profile(项目的,用户的,全局的)。
help:effective-pom
显示当前构建的实际POM,包含活动的Profile。
help:effective-settings
打印出项目的实际settings, 包括从全局的settings和用户级别settings继承的
配置。
help:describe
描述插件的属性。它不需要在项目目录下运行。但是你必须提供你想要描述插件
的 groupId 和 artifactId。
eg: mvn help:describe -Dplugin=assembly
23、Maven中的依赖关系查看
写道
mvn dependency:resolve
4、忽略测试失败
如果你有失败的单元
测试,但你仍然希望产生构建输出,你就必须告诉 Maven 让它忽略测试失败。 当
Maven 遇到一个测试失败,它默认的行为是停止当前的构建。 如果你希望继续构
建项目,即使 Surefire 插件遇到了失败的单元测试,你就需要设置 Surefire 的
testFailureIgnore 这个配置属性为 true。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin>也可以显示的使用如下命令 : mvn test -Dmaven.test.failure.ignore=true