Maven问题总结 - 2

摘要: 一方库、二方库、三方库说明: 一方库:本工程中的各模块之前的相互依赖 二方库:公司内部的依赖库,一般指公司内部的其他项目发布的jar包 三方库:公司之外的开源库, 比如apache、ibm、google等发布的依赖 antx eclipse:使用antx生成eclipse工程,方便导入 参考:  http://tianya23.

一方库、二方库、三方库说明:
一方库:本工程中的各模块之前的相互依赖
二方库:公司内部的依赖库,一般指公司内部的其他项目发布的jar包
三方库:公司之外的开源库, 比如apache、ibm、google等发布的依赖
antx eclipse:使用antx生成eclipse工程,方便导入
(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
 
或者
<parent>
        <groupId>com.alibaba</groupId>
        <artifactId>pampas</artifactId>
        <version>1</version>
</parent>
 
(4)查询maven仓库
 

24、跳过单元测试
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
或使用命令:mvn install -Dmaven.test.skip=true

25、 发布我的jar包到我的remote repository
如何发布我的jar包到我的remote repository 
你需要在settings.xml中间设置server: 
<servers> 
<server> 
<id>mycompany-repository</id> 
<username>jvanzyl</username> 
<!--
 Default value is ~/.ssh/id_dsa --> 
<privateKey>/path/to/identity</privateKey> 
<passphrase>my_key_passphrase</passphrase> 
</server> 
</servers> 

然后在pom.xml中设置远程url: 

<distributionManagement> 
<repository> 
<id>mycompany-repository</id> 
<name>MyCompany Repository</name> 
<url>scp://repository.mycompany.com/repository/maven2</url> 
</repository> 
</distributionManagement>

26、拷贝所有的依赖, mvn dependency:copy-dependencies

会将工程的全部依赖(包括依赖的依赖等)全部拷贝到工程下面的目录 target\dependency下面, 以后就可以直接拷贝到Linux上面了。

27、Maven标准目录结构
src/main/javaApplication/Library sourcessrc/main/resourcesApplication/Library resourcessrc/main/filtersResource filter filessrc/main/assemblyAssembly descriptorssrc/main/configConfiguration filessrc/main/webappWeb application sourcessrc/test/javaTest sourcessrc/test/resourcesTest resourcessrc/test/filtersTest resource filter filessrc/siteSiteLICENSE.txtProject's licenseREADME.txtProject's readme

28、   

Maven内存不够怎么办?!

通常是PermSize不够,用下面参数一般可解决问题(注意:用上面的apt源安装的maven,已经正确设置了内存大小!):

在安装目录下面的bin目录下面的mvn.bat文件中添加如下内容:
set MAVEN_OPTS=-Xms256m -Xmx512m -XX:ReservedCodeCacheSize=64m -XX:MaxPermSize=128m
参考:  

 

29、Maven运行Junit4
项目中用到了Junit4.x中的一些新特性(通过Annotation定义setUp()@Before,testXXX()@Test)在Eclipse中可以正常run的unit test在Maven2中出来一些奇怪的错误,没有执行@Before标记的setUp()方法。

解决步骤:
方案1:
1.在pom中加入surefire-plugin 插件.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>        
</plugin>

2.在Dependency中加入surefire-junit bundle
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.5</version>
<scope>test</scope>
</dependency>
注意:surefire-junit bundle是一定需要加的,而且加时要注意artifactId,如果是Junit4.7的话artifactId是surefire-junit47.
 
方案2:(推荐方式)
 
 
添加test依赖

<dependency>              <groupId>com.alibaba.external</groupId>              <artifactId>test.junit</artifactId>              <version>4.8.1</version>          </dependency> 
再增加surefire对junit和testng的支持

<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>                      <excludes>                          <exclude>**/IT*.java</exclude>                          <exclude>**/*Bean.java</exclude>                          <exclude>**/TestHelper.java</exclude>                      </excludes>                  </configuration>              </plugin> 
结论:建议选用方案2

 

30、Maven版本仲裁原则
  (1)取路径最近的
  (2)路径相同,取写在最前面的
如果需要使用某一版本,在dependencyManagement进行声明
<dependencyManagement>
    <dependencies>
      <dependency>
        <artifactId>napoli.client</artifactId>
        <groupId>com.alibaba.napoli</groupId>
        <version>1.2.0</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

31、以debug方式启动jetty
正常的启动jetty是这样的:mvn jetty:run-war 
若想以debug方式来启动的话,就要用maven_home/bin下的mavenDebug命令,即: 
mvnDebug jetty:run-war 
关于Jetty的更多内容,参考:http://www.ibm.com/developerworks/cn/web/wa-lo-jetty/

32、调试mvn test

执行mvn test时查看是什么原因失败的

 
    
mvn -Dsurefire.useFile=false test
 

33、下载源代码
mvn -DdownloadSources=true -Declipse.addVersionToProjectName=true eclipse:eclipse生成eclipse项目文件,尝试从repository下载源代码,并且生成的项目名包含模块版本(注:如果用公共POM,上述的开关缺省已打开)

34、Eclipse中Maven缓存的问题
在elipse中,默认会缓存之前的依赖内容,当被依赖的内容发生变化时,重新打包到本地仓库,此时可能存在报错的现象,提示一些类无法识别。 非常的恼人,其实只需要Project -> Clean掉重新build即可。

35、  

更新当前项目及其 module 的版本

 
在一个多 module 的 maven 项目中,当项目升级的时候,我们需要修改项目的 version。如果项目中仅有几个 module,那我们可以手工修改,如果项目中有几十甚至更多 module 的时候,人肉去修改的话,第一可能会出错,第二可能会少修改一些 module。使用插件 versions:set 可以很容易地完成这件事情,只要运行如下命令即可:
mvn versions:set -DnewVersion=new_version
DZone 上有一篇介绍此插件的文章:Managing POM versions

36、  

Maven Autoconf使用

autoconf的具体使用参数参考:
插件添加到pom.xml
<plugin>
                <groupId>com.alibaba.maven.plugins</groupId>
                <artifactId>maven-autoconf-plugin</artifactId>
                <version>0.3-alpha-7</version>
                <executions>
                    <execution>
                        <id>autoconf</id>
                        <phase>package</phase>
                        <goals>
                            <goal>autoconf</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <path>${project.build.directory}/${project.build.finalName}.${project.packaging}</path>
                    <!-- -Dxxx=yyy to <xxx>yyy</xxx> -->
                </configuration>
            </plugin>

本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/364072,如需转载请自行联系原作者

猜你喜欢

转载自blog.csdn.net/u011490072/article/details/80651756