配置并执行Profile
在线下载
mvn clean package -Dmaven.test.skip=true -P integrated
离线下载
mvn install -Dcode.path=/usr1/Source/MAE_Pair/src/5G_MAE_COMM -Doffline -P off_line_compile
生命周期
| scope取值 | 有效范围(compile, runtime, test) | 依赖传递 | 例子 |
|---|---|---|---|
| compile默认 | all | 是 | spring-core |
| provided | compile, test(不打包) | 否 | servlet-api |
| runtime | runtime, test | 是 | JDBC驱动 |
| test | test | 否 | JUnit |
| system | compile, test(本地系统,不去maven仓库) | 是 | |
| import | 只在dependency Management | 否 |
Maven快速打包方法
多线程
# 用 4 个线程构建,以及根据 CPU 核数每个核分配 1 个线程进行构建
$ mvn -T 4 clean install
$ mvn -T 1C clean install
跳过测试
-DskipTests # 不执行测试用例,但编译测试用例类生成相应的 class 文件至 target/test-classes 下
-Dmaven.test.skip=true # 不执行测试用例,也不编译测试用例类
# 结合上文的`并行执行`
$ mvn -T 1C clean install -Dmaven.test.skip=true
# 如果还是阻塞: 资源管理器 - shutdown all java app
# 如果 jar 包过大,可以下载按照路径放在 repository 中,之后可能还需要 mvn clean 来下载 groovy-all-2.3.11.pom 文件 (mvnrepository.com)
D:\apps\maven\repository\org\codehaus\groovy\groovy-all\2.3.11\groovy-all-2.3.11.jar
编译失败后,接着编译
# 如果是 Apache Eagle 之类带有几十个子项目的工程,如果从头编译所有的模块,会很耗功夫
# 通过指定之前失败的模块名,可以继续之前的编译
$ mvn -rf :moduleName clean install
跳过失败的模块,编译到最后再报错
$ mvn clean install --fail-at-end
离线
idea中点离线图标,或者加-o参数,离线编译,免依赖下载
maven clean compile -o
循环依赖
解决原则
mvn解决间接依赖冲突,使用先声明的,路径短的
同级依赖:在第一级,谁后声明,使用谁
不在第一级,谁先声明,使用谁
查看方法
1.maven dependency:tree
2.idea可以查看manDirgram图表,标红为冲突
3.idea插件Dependency Analyzer图表查看
解决方法
1.使用Exclusions排除不需要的依赖
2.使用properties或【dependencyManagement】统一版本
mvn常用标签
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 指定了当前POM的版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目坐标信息 -->
<!-- 项目主标识,用于定义当前项目属于的实际项目,格式与项目创建的包是一样的,公司域名反写-->
<groupId>com.jsun.demo</groupId>
<!-- 项目名或模块名或项目名+模块名组成 -->
<artifactId>demo-maven01</artifactId>
<!-- 当前项目版本号,一般由三个数字组成,第一个0表示大版本号,第二个0表示分支版本号,第三个1表示小版本号 -->
<!-- SNAPSHOT代表当前版本类型为快照版本,还有alpha内部版本、beta公测版本、release发布版本、ga正式版本等 -->
<version>0.0.1-SNAPSHOT</version>
<!-- maven打包方式,默认为jar,还有:pom,maven-plugin,war,rar,zip -->
<packaging>jar</packaging>
<!-- 用在子模块中,实现对父模块的继承 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<!-- 聚合多个maven项目,同时对所有聚合项目进行编译 -->
<modules>
<module></module>
</modules>
<!-- 项目描述名,url,详细描述,产生项目文档使用 -->
<name>Maven01</name>
<url>http://maven.apache.org</url>
<description>测试maven项目</description>
<!-- 开发人员列表,项目发布使用 -->
<developers>
<!-- 某个项目开发者的信息 -->
<developer>
<!-- 项目开发者的唯一标识符 -->
<id>001</id>
<!-- 项目开发者的全名 -->
<name>jsun</name>
<!-- 项目开发者的email -->
<email> jsun@163.com </email>
<!-- 项目开发者的主页的URL -->
<url />
<!-- 项目开发者在项目中扮演的角色,角色元素描述了各种角色 -->
<roles>
<role>developer</role>
</roles>
<!-- 项目开发者所属组织 -->
<organization>com-jsun</organization>
<!-- 项目开发者所属组织的URL -->
<organizationUrl> http://demo.jsun.com/jsun</organizationUrl>
</developer>
</developers>
<!-- 许可证信息, -->
<licenses>
<license>
<name></name>
<!-- 官方的license正文页面的URL -->
<url></url>
<!-- 项目分发的主要方式:repo,可以从Maven库下载,manual,用户必须手动下载和安装依赖 -->
<distribution></distribution>
<!-- 关于license的补充信息 -->
<comments></comments>
</license>
</licenses>
<!-- 项目所属组织信息 -->
<organization>
<name></name>
<url></url>
</organization>
<!-- 属性列表,相当于定义的公共常量,引用方式比如:${project.build.sourceEncoding} -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>3.8.1</junit.version>
</properties>
<!-- 依赖列表 -->
<dependencies>
<!-- 具体依赖项,下面主要包含依赖的坐标、类型、范围等信息 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.2.6</version>
<!-- 依赖的类型 -->
<type>jar</type>
<!-- 项目如果要使用某个框架或依赖,需要把相关jar包引用到classpath中,maven项目提供了三个classpath:编译、测试、运行 -->
<!-- 依赖的范围用于控制依赖于三种classpath关系的,包括:compile、provided、runtime、test、system、import -->
<!--
compile:默认范围,编译、测试、运行都有效
provided:编译和测试有效,最后运行不会被加入
runtime:在测试和运行的时候有效,编译不会被加入,比如jdbc驱动jar
test:测试阶段有效,比如junit
system:与provided一致,编译和测试阶段有效,但与系统关联,可移植性差
import:导入的范围,它只是用在dependencyManagement中,表示从其它的pom中导入dependency的配置
-->
<!-- 表示当前依赖只能在测试代码中引用使用,在主代码中引用使用则报错 -->
<scope>test</scope>
<!-- 排除依赖传递列表,比如A依赖B,B依赖C,但是A并没有使用C的功能,可以把C排除-->
<exclusions>
<exclusion></exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<!-- 主动设置禁止自己被传递,只在当前项目中使用 -->
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<!-- 在相同版本下针对不同的环境或者jdk使用的jar,如果配置了这个元素,则会将这个元素名在加在最后来查找相应的jar,
具体解释查看:http://www.cnblogs.com/lovingprince/archive/2010/09/19/2166273.html -->
<classifier>jdk15</classifier>
<version>2.4</version>
</dependency>
</dependencies>
<!-- 使用dependencyManagement标签管理依赖,实际管理的是依赖的版本号,让
所有子项目中引用对应依赖而不用显式的列出版本号;
依赖并不会在当前项目引入 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 构建插件 -->
<build>
<!--
Maven定制化打包后的包名
Maven默认的包名为:<finalName>${project.artifactId}-${project.version}</finalName>
定制化想要的包名,如加上时间戳:<finalName>${project.artifactId}-${maven.build.timestamp}</finalName>
-->
<finalName>myProject</finalName>
<!-- 将src/main/java目录下src/main/resources目录下适配通配符的文件也打包打进去.因为默认src/main/java下打包时只有class文件,src/main/resources下打包时将各种xml,properites,xsd文件等打包jar或者war里面,防止遗漏部分文件,可以在下面的标签设置 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<!-- 插件列表 -->
<plugins>
<!-- Source attach plugin 发布的包或者打包的时候会将源码也同时打出来 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- 一般而言,target与source是保持一致的,但是,
有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),
会存在target不同于source的情况 -->
<source>1.8</source> <!-- 源代码使用的JDK版本 -->
<target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
<encoding>UTF-8</encoding><!-- 字符集编码 -->
<skipTests>true</skipTests><!-- 跳过测试 -->
<verbose>true</verbose>
<showWarnings>true</showWarnings>
<fork>true</fork><!-- 要使compilerVersion标签生效,还需要将fork设为true,用于明确表示编译版本配置的可用 -->
<executable><!-- path-to-javac --></executable><!-- 使用指定的javac命令,例如:<executable>${JAVA_1_4_HOME}/bin/javac</executable> -->
<compilerVersion>1.3</compilerVersion><!-- 指定插件将使用的编译器的版本 -->
<meminitial>128m</meminitial><!-- 编译器使用的初始内存 -->
<maxmem>512m</maxmem><!-- 编译器使用的最大内存 -->
<compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument><!-- 这个选项用来传递编译器自身不包含但是却支持的参数选项 -->
</configuration>
</plugin>
<!-- The configuration of maven-assembly-plugin; 其中设计的package.xml和pom.xml是同级目录结构文件,文件内容见下面的package.xml文件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly-platform</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>package.xml</descriptor>
</descriptors>
<finalName>patch</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
<!-- findbugs -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<configuration>
<!-- <excludeFilterFile>tools/findbugs/findbugs-exclude.xml</excludeFilterFile> -->
<threshold>High</threshold>
<effort>Default</effort>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlOutputDirectory>target/site/findbugs</findbugsXmlOutputDirectory>
</configuration>
</plugin>
</plugins>
<!-- 插件管理列表,与dependencyManagement标签作用相似,管理插件版本号,让子项目继承使用 -->
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 插件扩展配置 -->
<!-- 更详细的例子:http://my.oschina.net/zh119893/blog/276090 -->
<configuration>
<!-- 源代码编译版本 -->
<source>1.7</source>
<!-- 目标平台编译版本 -->
<target>1.7</target>
<!-- 设置编译字符集编码 -->
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>