(一)常用命令
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中添加所有的依赖
好处:不用向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)设置主类
2)测试过程测试失败错误:mvn test
<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> |
<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模式,不联网更新依赖
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 强制更新
相关推荐
-D传入属性参数-P使用pom中指定的配置-e显示maven运行出错的信息-o离线执行命令,即不去远程仓库更新包-X显示maven允许的debug信息-U强制去远程参考更新snapshot包例如mvninstall-Dmaven.test.skip=true-Poracle其他...
maven项目的配置POM配置文件,主要是maven项目的配置,直接从中央仓库下载内容。
对于maven,你不应该只知道在pom中如何引入jar包,还应该了解maven一些常用的命令
一、maven常用命令: 1.mvn clean 清空产生的项目( target里) 2.mvn compile 编译源代码 3.mvn install 在本地repository中安装jar(包含mvn compile,mvn package,然后上传到本地仓库) 4.mvn deploy 上传到私服...
Maven常用命令 Maven Maven学习
一、Idea关联的maven本地仓库配置文件settings.xml (1)必须使用默认文件名 D:\developsoft\javaweb\commonPlugins\maven\apache-maven-3.8.1_first\conf\settings.xml 二、Myeclipse关联的maven本地仓库配置文件...
pom.xml配置文件,包含数据库,仓库地址配置等等
maven多环境部署pom文件实例,简单实用。
Maven的pom.xml配置文件详细配置说明 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=...
maven常用的命令
maven常用命令以及以个测试项目方便测试
maven常用命令
本文主要阐述了maven build是用来干什么的,以及对build标签中各子元素配置的作用
maven仓库清理命令
主要描述了maven的一些常用命令,以备您不时之需
Maven_pom.xml常用配置解析
maven配置阿里云镜像仓库,maven配置阿里云镜像仓库,maven配置阿里云镜像仓库。
maven的setting配置私有仓库 包含:阿里云的配置+本地私有的配置多个仓库
小镜子配置之maven_repsitory之pom