除了以下的几个faq条目之外,还有一些faq可以参考
maven 自己的FAQ
兄弟们如果有其他问题,欢迎跟帖提问!
6.1 依赖关系
1) 问:如何增加删除一个依赖关系?
答:直接在pom文件中加入一个dependency节点,如果要删除依赖,把对应的dependency节点删除即可。
2) 问:如何屏蔽一个依赖关系?比如项目中使用的libA依赖某个库的1.0版,libB依赖某个库的2.0版,现在想统一使用2.0版,如何去掉1.0版的依赖?
答:设置exclusion即可。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.5.ga</version>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
</exclusions>
</dependency>
3) 问:我有一些jar文件要依赖,但是我又不想把这些jar去install到mvn的repository中去,怎么做配置?
答:加入一个特殊的依赖关系,使用system类型,如下:
<dependency>
<groupId>com.abc</groupId>
<artifactId>my-tools</artifactId>
<version>2.5.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/lib/mylib1.jar</systemPath>
</dependency>
但是要记住,发布的时候不会复制这个jar。需要手工配置,而且其他project依赖这个project的时候,会报告警告。如果没有特殊要求,建议直接注册发布到repository。
4) 问:在eclipse环境中同时使用mavenbuilder和eclipse builder,并且设置项目依赖关系之后,为什么编译会出现artifact找不到错误,但是直接使用命令行mvn构建则一切正常?
答:在project属性中去掉java build path中对其他 project 的依赖关系,直接在pom中设置依赖关系即可
<!-- 依赖的其他项目 -->
<dependency>
<groupId>com.abc.project1</groupId>
<artifactId>abc-project1-common</artifactId>
<version>${project.version}</version>
</dependency>
另外,保证没有其他错误。
5) 问:我想让输出的jar包自动包含所有的依赖
答:使用 assembly 插件即可。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
6) 问:我的测试用例依赖于其他工程的测试用例,如何设置?
答:maven本身在发布的时候,可以发布单纯的jar,也可以同时发布xxx-tests.jar和xxx-javadoc.jar(大家经常在repository中可以看到类似的东西)。我们自己的项目A要同时输出test.jar可以做如下的设置
<!-- 用于把test代码也做成一个jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
然后在其他需要引用的工程B中做如下的dependency设置
<dependency>
<groupId>com.abc.XXX</groupId>
<artifactId>工程A</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
7)如何让maven将工程依赖的jar复制到WEB-INF/lib目录下?
8)我刚刚更新了一下我的nexus库,但是我无法在eclipse中用m2eclipse找到我新增的库文件
修改pom.xml文件,将旧版jar的依赖内容中的版本直接修改为新版本即可。
9)我要的jar最新版不在maven的中央库中,我怎么办?
将依赖的文件安装到本地库,用如下命令可以完成:
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the groupthat the file should be registered under
<artifact-id> the artifactname for the file
<version> the versionof the file
<packaging> thepackaging of the file e.g. jar
10)
6.2 变量
1) 问:如何使用变量替换?项目中的某个配置文件比如jdbc.properties使用了一些pom中的变量,如何在发布中使用包含真实内容的最终结果文件?
答:使用资源过滤功能,比如:
<project>
...
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost:3306/development_db</jdbc.url>
<jdbc.username>dev_user</jdbc.username>
<jdbc.password>s3cr3tw0rd</jdbc.password>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
...
<profiles>
<profile>
<id>production</id>
<properties>
<jdbc.driverClassName>oracle.jdbc.driver.OracleDriver</jdbc.driverClassName>
<jdbc.url>jdbc:oracle:thin:@proddb01:1521:PROD</jdbc.url>
<jdbc.username>prod_user</jdbc.username>
<jdbc.password>s00p3rs3cr3t</jdbc.password>
</properties>
</profile>
</profiles>
</project>
2) 问: maven-svn-revision-number-plugin插件说明
答: maven-svn-revision-number-plugin 可以从 SVN 中获取版本号,并将其变成环境变量,交由其他插件或者profile使用,详细帮助在这里。一般和resource的filter机制同时使用
<plugins>
<plugin>
<groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
<artifactId>maven-svn-revision-number-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<entries>
<entry>
<prefix>prefix</prefix>
</entry>
</entries>
</configuration>
</plugin>
</plugins>
这段代码负责把resource文件中的内容替换成适当内容
repository = ${prefix.repository}
path = ${prefix.path}
revision = ${prefix.revision}
mixedRevisions = ${prefix.mixedRevisions}
committedRevision =${prefix.committedRevision}
status = ${prefix.status}
specialStatus = ${prefix.specialStatus}
3)我的程序有些单元测试有错误,如何忽略测试步骤?
有好几种方法都可以实现跳过单元测试步骤,一种是给mvn增加命令行参数 -Dmaven.test.skip=true 或者 -DskipTests=true ;另外一种是给surefire插件增加参数,如下:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
4) 如果只想运行单个测试用例,能否实现?
可以,运行时候增加命令行参数 -Dtest=MyTest 即可,其中MyTest是所需要运行的单元测试用例名称,但是不需要包含package部分。
6.3 编译
1) 问:如何给插件指派参数?比如我要设置一些编译参数
答:以下内容设定编译器编译java1.5的代码
<project>
...
<build>
...
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
...
</build>
...
</project>
要设置其他插件的参数也可以,请参考对应插件的帮助信息
2) 问:我的目录是非标准的目录结构,如何设置让maven支持?
答:指定source目录和test-source目录即可。
<build>
<directory>target</directory>
<sourceDirectory>src</sourceDirectory>
<scriptSourceDirectory>js/scripts</scriptSourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<outputDirectory>bin</outputDirectory>
<testOutputDirectory>bin</testOutputDirectory>
</build>
这个例子把源代码设置成了src目录,测试代码在test目录,所以输出到bin目录。这里要注意,directory如果也设置成bin目录的话,maven打包的时候会引起死循环,因为directory是所有工作存放的地方,默认包含outputDirectory定义的目录在内。
3) 我源代码是UTF8格式的,我如何在maven中指定?
设置一个变量即可
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>
{color:blue}以上是官方给出的解决方案,但是经过尝试这样只能影响到resource处理时候的编码{color},真正有用的是如下配置:
{code:xml}
<build>
...
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
...
</build>
{code}
. 问:我的项目除了main/java目录之外,还加了其他的c++目录,想要一并编译,如何做?
答:使用native插件,具体配置方法参考[http://mojo.codehaus.org/maven-native/native-maven-plugin/]
{code:xml}
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>native-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
</plugin>
{code}
. 问:我想要把工程的所有依赖的jar都一起打包,怎么办?
答:首先修改maven的配置文件,给maven-assembly-plugin增加一个jar-with-dependencies的描述。
{code:xml}
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
{code}
然后使用命令打包即可:
mvn assembly:assembly
. 问:我想把main/scripts中的内容一起打包发布,如何做?
答:在pom中配置额外的资源目录。如果需要的话,还可以指定资源目录的输出位置
{code:xml}
<build>
...
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/command</directory>
<includes>
<include>run.bat</include>
<include>run.sh</include>
</includes>
<targetPath>/abc</targetPath>
</resource>
<resource>
<directory>src/main/scripts</directory>
</resource>
</resources>
...
</build>
{code}
. 问:我有多个源代码目录,但是maven只支持一个main src和一个test src,怎么办?
答:使用另外一个插件,并仿照如下配置pom
{code:xml}
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/config/java</source>
<source>src/main/java</source>
<source>src/member/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
{code}
. 问:我的源代码目录中有一部分文件我不想让maven编译,怎么做?
答:使用一个maven插件,然后使用includes和excludes。同理,也可以处理资源的过滤。
{code:xml}
<build>
<sourceDirectory>http://www.cnblogs.com/src/java</sourceDirectory>
<plugins>
<plugin>
<groupId>com.sun.enterprise</groupId>
<artifactId>hk2-maven-plugin</artifactId>
<configuration>
<includes>
<include>com/sun/logging/LogDomains.*</include>
<include>com/sun/enterprise/util/OS.java</include>
<include>com/sun/enterprise/util/io/FileUtils.java</include>
<include>com/sun/enterprise/util/zip/**</include>
<include>com/sun/enterprise/util/i18n/**</include>
<include>com/sun/enterprise/deployment/backend/IASDeploymentException.java</include>
</includes>
<excludes>
<exclude>com/sun/enterprise/config/ConfigBeansFactory.java</exclude>
<exclude>com/sun/enterprise/config/clientbeans/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>http://www.cnblogs.com/src/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
{code}
. 问:我的项目是一个纯的html组成的项目,没有任何的java代码,怎么跳过编译过程?
答:配置如下
{code:xml}
<build>
<sourceDirectory>src/java</sourceDirectory>
<plugins>
<plugin>
<groupId>com.sun.enterprise</groupId>
<artifactId>hk2-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
{code}
. 问:我的工程里用hibernate,想在编译时候自动生成ddl,如何做?
答:添加插件
hibernate3-maven-plugin
,按照如下配置:
{code:xml}
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>annotationconfiguration</implementation>
</component>
</components>
</configuration>
<dependencies>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
</dependency>
</dependencies>
</plugin>
{code}
. 问:我能用maven支持eclipse RCP项目吗?
答:当然可以,你可以使用插件 Tycho,详细内容可以参考这里[http://mattiasholmqvist.se/2010/02/building-with-tycho-part-1-osgi-bundles/].
<plugin>
<groupid>org.sonatype.tycho</groupid>
<artifactid>target-platform-configuration</artifactid>
<version>0.7.0</version>
<configuration>
<resolver>p2</resolver>
</configuration>
</plugin>
另外,老牌的pde-maven-plugin就不要用了,已经好几年没见更新了。
6.4 ant互动
1) 如何在maven编译时候运行ant脚本?
使用专门的antrun插件,并且在target标签内部加入ant的代码
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase> <!-- 生命周期阶段 --> </phase>
<configuration>
<target>
<!-- 加入target内部的代码 -->
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
2)如何在ant脚本中引用maven的classpath?
maven给每一个依赖都生成了一个属性,格式为"groupId:artifactId[:classifier]:type",比如,如果一下例子就显示依赖的org.apache.common-util的jar文件路径
<echo message="Dependency JAR Path:${org.apache:common-util:jar}"/>
另外,maven还预定义了四个classpath的引用,他们是
maven.compile.classpath
maven.runtime.classpath
maven.test.classpath
maven.plugin.classpath
3)如何使用antrun插件运行外部的build文件?
很简单,直接在antrun里边使用ant指令即可,如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<!-- 同时传递内置的classpath给外部ant文件 -->
<propertyname="compile_classpath" refid="maven.compile.classpath"/>
<propertyname="runtime_classpath"refid="maven.runtime.classpath"/>
<propertyname="test_classpath" refid="maven.test.classpath"/>
<propertyname="plugin_classpath" refid="maven.plugin.classpath"/>
<antantfile="${basedir}/build.xml">
<targetname="test"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
. 问:如何在ant中使用maven的功能?
答:使用ant的[maven task|http://maven.apache.org/ant-tasks/index.html],不过只有ant 1.6以上和jdk 1.5环境才支持。
h4. 测试相关
. 问:如何忽略某个阶段的结果?比如单元测试不一定要全正确
答:给插件增加testFailureIgnore参数,并设置为false。如果要屏蔽该阶段,则用
<skip>true</skip>
{code:xml}
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
{code}
. 问:我如何在maven中加入PMD,CheckStyle,JDepend等检查功能?
答:加入PMD检查,以下代码如果在
reporting
节点中加入则在
mvn site
中执行,如果在
build
节点中加入,则在build的时候自动运行检查。详细配置参考[pmd插件使用说明|http://maven.apache.org/plugins/maven-pmd-plugin/]
{code:xml}
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugins>
{code}
加入 checkstyle 检查,详细配置参考[checkstyle插件使用说明|http://maven.apache.org/plugins/maven-checkstyle-plugin/],同样注意放置在reporting和build节点中的区别(所有报表类插件都要同样注意):
{code:xml}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.5</version>
</plugin>
{code}
加入 simian 的支持,simian是一个支持代码相似度检查的工具,目前有maven插件,也有checkstyle的插件。它不仅可以检查java,甚至可以支持文本文件的检查。详细帮助信息参考[这里|http://www.redhillconsulting.com.au/products/simian/]。simian 的 maven插件在[这里|http://mojo.codehaus.org/simian-report-maven-plugin/introduction.html]
{code:xml}
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>simian-maven-plugin</artifactId>
<version>1.6.1</version>
</plugin>
</plugins>
...
</build>
{code}
加入 jdepend 检查,详细配置参考[jdepend使用说明|http://mojo.codehaus.org/jdepend-maven-plugin/],
{code:xml}
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jdepend-maven-plugin</artifactId>
<version>2.0-beta-2</version>
</plugin>
{code}
加入 findbugz 检查,详细配置参考[findbugz使用说明|http://mojo.codehaus.org/findbugs-maven-plugin/usage.html],
{code:xml}
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
</plugin>
{code}
加入javadoc生成,详细配置参考[javadoc usage|http://maven.apache.org/plugins/maven-javadoc-plugin/usage.html]
{code:xml}
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.7</version>
<configuration>
...
</configuration>
</plugin>
{code}
加入 jxr 支持,JXR是一个生成java代码交叉引用和源代码的html格式的工具,详细配置信息参考[jxr usage|http://maven.apache.org/plugins/maven-jxr-plugin/]。注意,jxr没有必要在build阶段运行。
{code:xml}
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</reporting>
{code}
加入 Cobertura 支持,它是一个代码覆盖率工具,可以用来评估具有相应测试的源代码的比率。详细帮助在[这里|http://mojo.codehaus.org/cobertura-maven-plugin/index.html]。另外一个功能相似的软件是[EMMA|http://emma.sourceforge.net/samples.html],详细的帮助在[这里|http://mojo.codehaus.org/emma-maven-plugin/usage.html]。两个产品的比较文章在[这里|http://www.topcoder.com/tc?module=Static&d1=features&d2=030107],个人倾向于都要用,因为给出的指标不一样,都有参考作用。
{code:xml|title=Cobertura }
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<check>
<branchRate>85</branchRate>
<lineRate>85</lineRate>
<haltOnFailure>true</haltOnFailure>
<totalBranchRate>85</totalBranchRate>
<totalLineRate>85</totalLineRate>
<packageLineRate>85</packageLineRate>
<packageBranchRate>85</packageBranchRate>
<regexes>
<regex>
<pattern>com.example.reallyimportant.*</pattern>
<branchRate>90</branchRate>
<lineRate>80</lineRate>
</regex>
<regex>
<pattern>com.example.boringcode.*</pattern>
<branchRate>40</branchRate>
<lineRate>30</lineRate>
</regex>
</regexes>
</check>
</configuration>
<executions>
<execution>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
{code}
{code:xml|title=EMMA}
<reporting>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>1.0-alpha-3-SNAPSHOT</version>
</plugin>
...
</plugins>
...
</reporting>
{code}
添加 javaNCSS 插件,它是一个java代码的度量工具,详细参考在[这里|http://mojo.codehaus.org/javancss-maven-plugin/]。
{code:xml}
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javancss-maven-plugin</artifactId>
<version>2.0-beta-2</version>
</plugin>
</plugins>
</reporting>
{code}
h4. profile相关
. 问:profile能够设置为某个变量不存在的条件下激活?
答:使用!前缀,请看示例:
{code:xml}
<activation>
<property>
<name>!environment.type</name>
</property>
</activation>
{code}
h4. 部署相关
. 问:其他部署到服务器的方式和配置怎么配?
答:本文摘自[http://blog.csdn.net/zyxnetxz/archive/2009/05/18/4199348.aspx]{panel}*Distribution Management* 用于配置分发管理,配置相应的产品发布信息,主要用于发布,在执行mvn deploy后表示要发布的位置 *# 配置到文件系统
{code:xml}
<distributionManagement>
<repository>
<id>proficio-repository<id>
<name>Proficio Repository<name>
<url>file://${basedir}/target/deploy<url>
<repository>
<distributionManagement>
{code}
*# 使用ssh2配置
{code:xml}
<distributionManagement>
<repository>
<id>proficio-repository<id>
<name>Proficio Repository<name>
<url>scp://sshserver.yourcompany.com/deploy<url>
<repository>
<distributionManagement>
{code}
*# 使用sftp配置
{code:xml}
<distributionManagement>
<repository>
<id>proficio-repositoryi<d>
<name>Proficio Repository<name>
<url>sftp://ftpserver.yourcompany.com/deploy<url>
<repository>
<distributionManagement>
{code}
*# 使用外在的ssh配置编译扩展用于指定使用wagon外在ssh提供,用于提供你的文件到相应的远程服务器。
{code:xml}
<distributionManagement>
<repository>
<id>proficio-repository<id>
<name>Proficio Repository<name>
<url>scpexe://sshserver.yourcompany.com/deploy<url>
<repository>
<distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon<groupId>
<artifactId>wagon-ssh-external<artifactId>
<version>1.0-alpha-6<version>
<extension>
<extensions>
<build>
{code}
*# 使用ftp配置
{code:xml}
<distributionManagement>
<repository>
<id>proficio-repository<id>
<name>Proficio Repository<name>
<url>ftp://ftpserver.yourcompany.com/deploy<url>
<repository>
<distributionManagement>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagongroupId>
<artifactId>wagon-ftpartifactId>
<version>1.0-alpha-6version>
<extension>
<extensions>
<build>
{code}
{panel} h4. 插件配置
. 问:我用maven输出site,如何设置输出为utf8编码?
答:配置site插件的编码设置
{code:xml}
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.0-beta-6</version>
<configuration>
<outputEncoding>UTF-8</outputEncoding>
</configuration>
</plugin>
...
{code}