`

Maven常用命令、配置、POM、仓库

阅读更多
(一)常用命令
1)Maven 导出依赖lib 并打包
mvn clean dependency:copy-dependencies -DoutputDirectory=target/classes/lib  -DincludeScope=runtime package
 
2)忽略测试错误
mvn test -Dmaven.test.failure.ignore=true
 
3) 安装跳过单元测试
mvn install -Dmaven.test.skip=true
 
4)运行
mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main
好处:不用向classpath中添加所有的依赖
 
5)独立jar加入本地仓库
mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar
 
如:mvn install:install-file -DgroupId=org.apache.commons -DartifactId=commons-email -Dversion=1.3.2 -Dpackaging=jar -Dfile=E:\workspace\sca-crawler\lib\commons-email-1.3.2.jar
 
6)查看插件信息
mvn help:describe -Dplugin=exec -Dfull 
 
(二)插件定制
 
向插件传参数定制行为
 
1)设置主类

<build>
     <plugins>
          <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
               <version>2.4</version>
               <configuration>
                    <outputDirectory>target</outputDirectory>
                    <archive>
                         <manifest>
                              <addClasspath>true</addClasspath>
                              <classpathPrefix>./lib/</classpathPrefix>
                              <mainClass>com.ailk.tosd.ua.hadoop.tool.UAClassifierJob</mainClass>
                         </manifest>
                    </archive>
               </configuration>
          </plugin>
     </plugins>
</build> 
 
2)测试过程测试失败错误:mvn test
 
<build>
          <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                         <testFailureIgnore>true</testFailureIgnore>
                    </configuration>
               </plugin>
          </plugins>
</build>
 
3)安装过程跳过测试 :mvn install
 
<build>
          <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                         <skip>true</skip>
                    </configuration>
               </plugin>
          </plugins>
     </build>
 
4)编译插件编译环境1.6
 
<build>
          <pluginManagement>
               <plugins>
                    <plugin>
                         <groupId>org.apache.maven.plugins</groupId>
                         <artifactId>maven-compiler-plugin</artifactId>
                         <configuration>
                              <source>1.6</source>
                              <target>1.6</target>
                         </configuration>
                    </plugin>
               </plugins>
          </pluginManagement>
</build>
 
注:使用sun 专用api
 
<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                         <source>1.6</source>
                         <target>1.6</target>
                         <compilerArguments>
                              <verbose />
                              <bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
                         </compilerArguments>
                    </configuration>
               </plugin>
 
5)配置 copyDependency是只拷贝compile 作用域的
     <build>
          <pluginManagement>
               <plugins>
                    <plugin>
                         <groupId>org.apache.maven.plugins</groupId>
                         <artifactId>maven-dependency-plugin</artifactId>
                         <configuration>
                              <includeScope>runtime</includeScope>
                         </configuration>
                    </plugin>
               </plugins>
          </pluginManagement>
     </build>
  • runtime : runtime and compile dependencies,
  • compile : compile, provided, and system dependencies,
  • test (default) : all dependencies,
  • provided : provided dependencies,
  • system : system dependencies.
6)打包所有依赖的jar 中的class 打到同一个包里
<build>
          <plugins>
               <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                         <archive>
                              <manifest>
                                   <addClasspath>true</addClasspath>
                                   <classpathPrefix>./lib/</classpathPrefix>
                                   <!-- <mainClass>com.ailk.tosd.ua.hadoop.tool.UAClassifierJob</mainClass> -->
                              </manifest>
                         </archive>
                         <descriptorRefs>
                              <descriptorRef>jar-with-dependencies</descriptorRef>
                         </descriptorRefs>
                    </configuration>
               </plugin>
          </plugins>
     </build>
命令:mvn assembly:assembly  
说明:生成xxxx-1.0-jar-with-dependencies.jar的打包文件, 里面将包含所有的依赖文件
 
三 配置setting.xml
 
1)配置本地库
 
<localRepository>E:\Workspace\Java\Env\Maven\repositories</localRepository>
 
2)配置中央仓库镜像
 
<mirror>
      <id>maven.net.cn</id>
      <name>one of the central mirrors in china</name>
      <url>http://maven.net.cn/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
 </mirror>
 
3)配置其他仓库
 
<profile> 
      <id>repositoriesProfile</id> 
      <!-- repositories and pluginRepositories here--> 
      <repositories> 
              <repository> 
                <id>sonatype</id>  <!--id必须唯一-->
                <name>Sonatype Mirror</name>  <!--描述-->
                <url>http://repository.sonatype.org/content/groups/public/</url> 
                <releases> 
                  <enabled>true</enabled>  <!--可以下载releases版本-->
                </releases> 
                <snapshots> 
                  <enabled>false</enabled>   <!--禁止下载snapshots版本-->
                </snapshots> 
                  </repository> 
            </repositories> 
            <pluginRepositories> 
              <pluginRepository> 
                <id>sonatype</id> 
                <name>Sonatype Mirror</name> 
                <url>http://repository.sonatype.org/content/groups/public/</url> 
                <releases> 
                  <enabled>true</enabled> 
                </releases> 
                <snapshots> 
                  <enabled>false</enabled> 
                </snapshots>     
              </pluginRepository> 
            </pluginRepositories>
    </profile>  
 
激活仓库:
 
  <activeProfiles>
    <activeProfile>repositoriesProfile</activeProfile>
  </activeProfiles>
 
(三)常用参数
mvn -e 显示详细错误
mvn -U 强制更新snapshot类型的插件或依赖库(否则maven一天只会更新一次snapshot依赖)
mvn -o 运行offline模式,不联网更新依赖
 
(四)其他问题解决
 
1 存在*.lastUpdated文件无法更新jar
 
     使用maven构建的项目,当本地jar文件不存在时,maven会到中央仓库下载,如果存在网络等原因,jar文件下载未成功,相应目录会存在lastUpdated文件,因为有这个lastUpdated文件所以以后都不会真正下载对应ja了。 
     解决方法:
     1)在执行mvn compile 之前把lastUpdated 文件删除。
     2)使用 mvn -U clean install 强制更新
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics