---
title: "maven笔记"
author: "zhb8015"
date: "2015-07-13T19:06:15Z"
tags: []
categories: ["非技术"]
slug: "maven"
draft: false
lastmod: "2015-10-10T15:14:45Z"
id: 2227006
url: "https://peter8015.iteye.com/blog/2227006"
---

跳过测试阶段：

mvn package -DskipTests

临时性跳过测试代码的编译：

mvn package -Dmaven.test.skip=true

 

maven.test.skip同时控制maven-compiler-plugin和maven-surefire-plugin两个插件的行为，即跳过编译，又跳过测试。

 

指定测试类

mvn test -Dtest=RandomGeneratorTest

以Random开头，Test结尾的测试类

mvn test -Dtest=Random\*Test

用逗号分隔指定多个测试用例

mvn test -Dtest=ATest,BTest

 

指定即使没有任何测试用例也不要报错

test参数必须匹配至少一个测试类，否则会报错并导致构建失败。此时可使用以下配置来指定即使没有任何测试用例也不要报错。

mvn test -Dtest -DfailIfNoTests = false

 

POM文件配置包含与排除测试用例

 

使用\** / * Test.java 来匹配所有以Tests结尾的Java类。两个星号\*\*用来匹配任意路径，一个星号\*用来获取除路径风格符外的0个或多个字符。还可使用excludes来排除一些测试类。

 

 

\[html] view plaincopy

&lt;plugin&gt;  

    &lt;groupId&gt;org.apahce.maven.plugins&lt;groupId&gt;  

    &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;  

    &lt;version&gt;2.5&lt;/version&gt;  

    &lt;configuration&gt;  

        &lt;includes&gt;  

            &lt;include&gt;\*\*/\*Tests.java&lt;/include&gt;  

        &lt;/includes&gt;  

    &lt;/configuration&gt;          

&lt;/plugin&gt;  

 

**maven打资源包：**

一种  进入cmd命令行，进入项目工程pom.xml所在路径目录，运行  **mvn source:jar**

另一种用ide

 

**发布到本地Maven仓库中，在pom.xml添加如下      mvn install**

```
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>2.1.2</version>
      <executions>
        <execution>
          <id>attach-sources</id>
          <phase>verify</phase>
          <goals>
            <goal>jar-no-fork</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
```

 **4、Maven创建web project**

```
$ mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
```

 **e.g:**

```
$ mvn archetype:generate -DgroupId=com.javacodegeeks -DartifactId=SampleWebApplication -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

```

 

 5、maven repository

 http://www.ibiblio.org/maven2

 http://mirrors.ibiblio.org/pub/mirrors/maven2  

 http://repo.springsource.org/libs-milestone-local

 http://search.maven.org/ 

 http://mvnrepository.com/

 http://repository.jboss.org/maven2  

http://www.mvnsearch.org/maven2  

###### **6、创建项目**

###### **JAVA WEB项目：**

 

```
$ mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
```

 

```
$ mvn archetype:generate -DgroupId=com.javacodegeeks -DartifactId=SampleWebApplication -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
```

5、修改JDK编译版本

     修改MAVEN全局JDK：

```
<profiles>     
      <profile>       
           <id>jdk-1.7</id>       
           <activation>       
               <activeByDefault>true</activeByDefault>       
               <jdk>1.7</jdk>       
           </activation>       
           <properties>       
               <maven.compiler.source>1.7</maven.compiler.source>       
               <maven.compiler.target>1.7</maven.compiler.target>       
               <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>       
           </properties>       
   </profile>      
</profiles>   
```

   
   修改项目pom.xml

```
<build>
    <finalName>practise</finalName>
    <plugins>
    	<plugin>
    		<groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>2.0.2</version>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
            </configuration> 
    	</plugin>
    </plugins>
  </build>
```

 

 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

 1、Maven内置变量说明：

- ${basedir} 项目根目录
- ${project.build.directory} 构建目录，缺省为target
- ${project.build.outputDirectory} 构建过程输出目录，缺省为target/classes
- ${project.build.finalName} 产出物名称，缺省为${project.artifactId}-${project.version}
- ${project.packaging} 打包类型，缺省为jar
- ${project.xxx} 当前pom文件的任意节点的内容

2、一些命令：

- ```
  生成eclipse工程文件：mvn eclipse:clean eclipse:eclipse -DdownloadSources
  ```
- ```
  如果执行单元测试出错，用该命令可以在console输出失败的单元测试及相关信息：mvn -Dsurefire.useFile=false
  ```
- ```
  有时候，希望在mvn install时，对项目中的单元测试进行调试，使用该命令：mvn install -Dmaven.surefire.debug 。在使用该命令前，在你将要调试的代码中设置好断点，然后运行该命令。该命令执行一会之后，它的build过程会显示信息：Listening for transport dt_socket at address : 5005 。看到这个提示信息之后，在eclipse中，在Debug Configuration窗口，新建Remote Java Application，port设置为5005，然后点击“debug”按钮。之后，刚才的mvn命令将继续往下执行，直到运行到有断点的代码，则停留下来，这时在eclipse中可以调试运行的代码。
  ```

 3. maven jetty 插件的一些命令：（资料：[http://wiki.eclipse.org/Jetty/Feature/Jetty\_Maven\_Plugin](http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin)）

- 启动：mvn jetty:run    默认的端口号是8080。
- 启动时，设置端口号： mvn -Djetty.port=9999 jetty:run
- debug的方式启动：mvnDebug jetty:run    启动之后，会显示具体的debug端口

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

 

 

参考：

maven测试配置

[http://blog.csdn.net/wirelessqa/article/details/14057083](http://blog.csdn.net/wirelessqa/article/details/14057083)

安装 jar到本地或仓库

[http://www.blogjava.net/fancydeepin/archive/2012/06/12/380605.html](http://www.blogjava.net/fancydeepin/archive/2012/06/12/380605.html)

maven资源包打法

[http://www.tuicool.com/articles/NFvAbm](http://www.tuicool.com/articles/NFvAbm)

 maven专家

[http://www.juvenxu.com/about/](http://www.juvenxu.com/about/)

4、参照 创建项目

http://examples.javacodegeeks.com/enterprise-java/maven/create-web-application-project-with-maven-example/

5、专家博客

http://www.juvenxu.com/