maven常用命令

环境变量配置

1、确认JAVA_HOME 环境变量配置eg:C:\Program Files\Java\jdk1.8.0_102
2、配置MAVEN_HOME 环境变量配置eg:C:\adeng\soft\apache-maven-3.3.9
3、新增Path变量 eg:%MAVEN_HOME%\bin

配置本地仓库地址:
在形如:C:\adeng\soft\apache-maven-3.3.9\conf 目录下编辑setting.xml 搜索 localrepository 关键字
<localRepository>/path/to/local/repo</localRepository> 相对路径,相对于maven安装的磁盘
<localRepository>c:/path/to/local/repo</localRepository>指定磁盘和路径

高速仓库,系好安全带:http://www.cnblogs.com/keitsi/p/6000649.html
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

安装jar到本地仓库:mvn install:install-file -DgroupId=pinyin4clj -DartifactId=pinyin4clj -Dversion=0.3.0 -Dpackaging=jar -Dfile=E:\tmp\pinyin4clj-0.3.0.jar -DgeneratePom=true

安装jar到项目:mvn install:install-file -DgroupId=com.imooc.maven -DartifactId=maven01 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=D:\java\m2\repository\com\imooc\maven\maven01\1.0-SNAPSHOT\maven01-1.0-SNAPSHOT.jar -DgeneratePom=true -DlocalRepositoryPath=C:\adeng\work\kukii\corejava\src\main\resources\lib2\

maven 构建框架慢 添加 archetypeCatalog=internal

查看maven版本:mvn -version
怎么查看maven本地仓库地址: mvn help:effective-settings

 查看激活的profile:mvn help:active-profiles

激活指定的profilemvn -P dev package

filtering using resources:http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

指定不filter的二进制文件:http://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html

Getting a report of dependencies:(Apache Maven Cookbook chapter5)

    mvn dependency:list

    mvn dependency:tree

Getting into dependency and avoiding dependency hell:

    mvn dependency:tree

Downloading dependencies into a folder:(download dependencies in the target/dependency folder)

    mvn dependency:copy-dependencies

    mvn dependency:copy-dependencies -DoutputDirectory=target/dependencies    --change to the designation folder

    mvn dependency:copy-dependencies -Dmdep.copyPom=true -Dmdep.useRepositoryLayout=true    ---keep the folder layout

detect the unused/undeclared dependencies:

    mvn dependency:analyze

Chapter6:Code Quality Plugins

jacoco:

若没有生成测试报告(${basedir}/target/site/jacoco) 请确保maven-surefire-plugin 的 skipTests = true

若遇到如下错误,说明二进制文件被损坏了,如下错误

java.util.zip.ZipException: invalid literal/length code in JacocoPublisher

大神的解决方案:http://www.cnblogs.com/llxrl/p/5884978.html

原因:maven从resources中读取资源时默认会进行过滤,其中包含了重新编码,因此如果资源文件与maven的编码环境不同时,会导致文件损坏。

解决:(方案1)在pom.xml中设置filtering为true时则过滤

<filtering>true</filtering>

因此将其置false。

(方案2)将maven-resources-pligin中的encoding改为源文件的编码方式,这个比较复杂,因为我们有时候不知道源文件的编码方式,尤其是在使用第三方jar的时候

<encoding>源文件的编码方式(比如IS0-8859-1)</encoding>

 

(方案3)把资源文件zip解压后放入resources,但是要是二进制文件的话这招也不好使

我的终极解决方案:(指定二进制类型不被过滤,一劳永逸)

 1             <plugin>
 2                 <groupId>org.apache.maven.plugins</groupId>
 3                 <artifactId>maven-resources-plugin</artifactId>
 4                 <version>3.0.2</version>
 5                 <configuration>
 6                     <encoding>UTF-8</encoding>
 7                     <resources>
 8                         <resource>
 9                             <directory>${basedir}/src/main/resources</directory>
10                         </resource>
11                         <resource>
12                             <directory>${basedir}/src/main/resources</directory>
13                             <includes>
14                                 <include>*.properties</include>
15                             </includes>
16                             <filtering>true</filtering>//这只指定过滤的文件类型,把影响降低到最小
17                         </resource>
18                         <resource>
19                             <directory>${basedir}/src/main/java</directory>
20                             <includes>
21                                 <include>**/*.properties</include>
22                             </includes>
23                         </resource>
24                     </resources>
25                     <!--指定替换文件的原文件-->
26                     <filters>
27                         <filter>${basedir}/src/main/resources/my-filter-values.properties</filter>
28                     </filters>
29                     <nonFilteredFileExtensions>
30                         <nonFilteredFileExtension>jar</nonFilteredFileExtension>  //指定jar文件不被过滤,当然也可以添加其他的二进制文件
31                     </nonFilteredFileExtensions>
32                 </configuration>
33             </plugin>

 另:插件生成测试类http://www.cnblogs.com/adeng/p/7181418.html

 cobertura:

mvn cobertura:cobertura

可设置测试代码覆盖率低于多少自动失败,下面的设置是,测试代码覆盖率低于85%自动失败

命令:mvn cobertura:check

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<check>
<branchRate>85</branchRate>
<lineRate>85</lineRate>
<haltOnFailure>true</haltOnFailure>
</check>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

 PMD:

  mvn pmd:pmd pmd:cpd

Copy/Paste Detector(CPD) that finds duplicated code.
发现重复代码利器

 CheckStyle:

  mvn checkstyle:checkstyle

 FindBugsy:

   mvn clean compile findbugs:findbugs

 Generating source references with Maven JXR Plugin:

   mvn jxr:jxr

the jxr generates the cross-reference ,Once the cross reference exists.code quality tools, such as PMD and CheckStyle,link to this reference automatically by using  the following steps:

  mvn jxr:jxr checkstyle:checkstyle

SonarQube:

 这个最牛逼

  1、下载:https://www.sonarqube.org/downloads/

  2、解压安装启动,访问默认网址:http://localhost:9000

 Use the following step to analyze the code with the Maven SonarQube plugin:

  mvn sonar:sonar

 

Chapter7:Reporting and Documentation

Documenting with the Maven Site plugin:

  mvn site

  用到的插件:

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.9</version>
            </plugin>

 

Generating Javadoc for a Site:

  mvn site

  用到的插件:

   <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <configuration>
                    <charset>UTF-8</charset>
                    <encoding>UTF-8</encoding>
                    <docencoding>UTF-8</docencoding>
                </configuration>
            </plugin>
        </plugins>
    </reporting>

 

若输出到控制台的信息乱码:
做如下配置:

 

 哎,命运多舛,一直折腾。

另,idea自带的也有生成javadoc的工具:Tools-->Generate JavaDoc

乱码处理:Other command line arguments 中填写:-encoding utf-8 -charset utf-8  但是没有maven-javadoc-plugin输出的信息详细。

 Generating source cross-reference for a site:

1、 add the following code to the reporting section of pom.xml file:  

        <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.5</version>
                <reportSets>
                    <reportSet>
                        <id>aggregate</id>
                        <reports>
                            <report>aggregate</report>
                            <!--是否生成测试源码引用-->
                            <!--<report>test-aggregate</report>-->
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>

 

 

 

       

 

注:若使用阿里云仓库,贴入上面的代码后,maven可能不识别,做法是先在dependencis 中贴入jar包的依赖,下载jar后就可以了。 

Generting unit test reports for a site:

1、 add the following code to the reporting section of pom.xml file:  

            <!--unit test reports-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>2.20</version>
            </plugin>

 

  mvn site

Generating code coverage reports for a site:

 1、Add the following code in the <build> section of the pom.xml fle:

        <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

 

2、Add the following code in the reporting section of the pom.xml fle:

            <!--code coverage-->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
            </plugin>

 

  mvn test site 

Generating code quality reports for a site:

1、Add the following code to the reporting section of the pom.xml fle:

            <!--code quality-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.8</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.4</version>
            </plugin>

   mvn test site

 

 


    

posted @ 2017-07-01 09:30  练好本领,报国杀敌  阅读(1068)  评论(0编辑  收藏  举报