代码语言:JavaScript代码运行次数:0运行复制
export maven_OPTS="-Xms512m -Xmx1024m"
Windows:如下图

(2) 用户级别的maven配置
把MAVEN_HOME/conf/seettings.xml cp 到 ~/.m2/下,在.m2下的settings.xml中所作的配置就是用户级别的配置,而直接编辑MAVEN_HOME/conf/seettings.xml所作的配置是全局的配置
2. 涉及的maven命令代码语言:javascript代码运行次数:0运行复制
[mvn -v]: 查看maven版本[mvn -h]: 查看帮助[mvn help:system]: 查看java的系统属性和环境变量[mvn help:active-profiles]: 查看激活的profile[mvn help:all-profiles]: 列出所有的profile[mvn help:effective-pom]: 查看项目的所有pom元素(包括父元素中的默认配置)[mvn clean/compile/test/package/install/deploy]: maven的不同阶段的命令,可以互相组合使用[mvn archetype:generate]: 创建项目骨架[mvn dependency:list]: 显示项目中所有已解析依赖[mvn dependency:tree]: 查看当前项目的依赖树[mvn dependency:analyze]: 分析项目依赖[mvn clean install-U]: 强制maven检查更新[mvn help:describe -Dplugin=org.apache.maven.plugins:maven-source-plugin:3.0.1 -Ddetail]: 查看某插件的详细信息[mvn help:describe -Dplugin=source]: 上一个命令的简写[mvn help:describe -Dplugin=compile -Dgoal=compile]: 只查看goal相关的信息[mvn install -Dmaven.test.skip=true]: 构建时跳过测试(不编译测试代码,不运行测试代码)[mvn package -DskipTests]: 构建时跳过测试(编译测试代码,不运行测试代码)[mvn clean install -pl module1,module2]: 构建指定的模块[mvn clean install -pl module -am]: 构建指定模块及其依赖的模块[mvn clean install -pl module -amd]: 构建指定模块及依赖其的模块[mvn clean install -rf module]: 在完整的反应堆构建顺序基础上指定从哪个模块开始构建[mvn clean install -pl module1 -amd -rf module2]: 构建module1以及依赖其的模块,并且从module2开始构建[mvn clean install -P dev]: 激活id为dev的profile[mvn clean install -P profile1,profile2]: 激活多个profile[mvn clean install -Dkey=value]: 设置系统属性值[mvn test -Dtest=A]: 测试A类[mvn test -Dtest=A*B]: 测试以A开头B结尾的类[mvn test -Dtest=A,B]: 测试A类和B类[mvn test -Dtest=A*B,C]: 测试以A开头B结尾的类以及C类[mvn test -Dtest -DfailIfNoTests=false]: 即使没有运行测试也不要报错[mvn cobertura:cobertura]: 生成测试覆盖率报告,文件为target/site/cobertura/index.html[mvn jetty:run]: 使用jetty-maven-plugin进行测试[mvn jetty:run -Djetty.port=9999]: 启动jetty服务进行测试,并指定端口号[mvn cargo:start]: 发布war包到本地服务器[mvn cargo:deploy]: 发布war包到远程服务器[mvn release:clean]: 发布新版本前clean[mvn release:prepare]: 发布新版本前的准备工作[mvn release:prepare -DautoVersionSubmodules=true]: 子模块与父模块使用相同的版本[mvn release:rollback]: prepare后如果发现错误,可以回滚[mvn release:perform]: 发布新版本[mvn release:branch -DbranchName=0.0.1 -DupdateBranchVersions=true -DupdateWorkingCopyVersions=false]: 创建分支,分支使用新版本,不更新主干(trunk)的代码[mvn clean deploy -Dgpg.passphrase=passwd]: 发布已经签名的构件[mvn gpg:sign-and-deploy-file -DgroupId=com.bonc.rdpe -DartifactId=maven001 -Dversion=0.0.1-SNAPSHOT -Dpomfile=target/maven001-0.0.1-SNAPSHOT.pom -Dfile=target/maven001-0.0.1-SNAPSHOT.jar -Durl=http://192.168.138.129:8081/repository/maven-snapshots/ -DrepositoryId=nexus-snapshots]: 发布没有签名的文件并对其签名[mvn site]: 生成站点[mvn site:stage -DstagingDirectory=D:tmp]: 把站点预发布到某个临时文件夹下
3. settings.xml文件内容总结代码语言:javascript代码运行次数:0运行复制
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><!--自定义本地仓库路径 --><localrepository>path/to/maven/repo</localrepository><!-- maven是否与用户交互 --><interactivemode>true</interactivemode><!-- 离线模式 --><offline>false</offline><!-- 设置自定义的解析插件元数据的地址,主要用于得到插件的前缀,简化mvn命令 --><plugingroups><!-- 当插件的groupId为org.apache.maven.plugins和org.codehaus.mojo时 插件的命令支持简写的形式,例如: mvn help:system 其他插件如果你想要使用简写的命令,需要在这里声明该插件的groupId 这里声明了jetty-maven-plugin的groupId,所以你可以简写该插件的命令,例如: mvn jetty:run 如果不声明,完整的命令是这样的: mvn jetty:run -Dplugin=org.mortbay.jetty:jetty-maven-plugin:8.1.16.v20140903 --><plugingroup>org.eclipse.jetty</plugingroup><plugingroup>com.your.plugins</plugingroup></plugingroups><!-- 设置HTTP代理 --><proxies><proxy><id>dev3-proxy</id><active>true</active><protocol>http</protocol><username>root</username><password>root</password><host>172.16.13.185</host><port>9999</port><nonproxyhosts>https://repo.maven.apache.org/maven2|*.google.com</nonproxyhosts></proxy></proxies><!-- 配置仓库的认证信息 --><servers><server><id>nexus-releases</id><username>hadoop</username><password>hadoop</password></server><server><id>nexus-snapshot</id><username>hadoop</username><password>hadoop</password></server></servers><mirrors><!-- 配置远程仓库的镜像:这是一个私服 --><mirror><id>Public Repositories</id><name>Public Repositories</name><!-- * 代表这个私服可以作为所有远程仓库的镜像 --><mirrorof>*</mirrorof><!-- <mirrorOf>external:*</mirrorOf> : 匹配所有不在本机上的远程仓库 <mirrorOf>repo1,repo2</mirrorOf> : 只匹配repo1和repo2这两个仓库 <mirrorOf>*,!repo1</mirrorOf> : 匹配除了repo1之外的所有远程仓库 --><url>http://172.16.13.185:9000/nexus/content/groups/public/</url></mirror><!-- 阿里云的镜像 --><mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorof>central</mirrorof></mirror></mirrors><!-- 自定义的配置 --><profiles><!-- 激活方式1: 默认激活 --><!-- 设置maven编译时的jdk版本 --><profile><id>jdk-1.8</id><activation><!--默认为激活状态--><activebydefault>true</activebydefault><jdk>1.8</jdk></activation><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.compilerversion>1.8</maven.compiler.compilerversion></properties></profile><!-- 激活方式2: 在activeProfiles中激活 --><!-- 这是一个自定义的数据库配置 --><profile><id>profile1</id><properties><db.driver>com.mysql.jdbc.Driver</db.driver><db.url>jdbc:mysql://192.168.138.101:3306/test</db.url><db.username>dev</db.username><db.password>dev-pwd</db.password></properties></profile><!--激活方式3: 当系统属性的值为指定值时激活--><!-- 下面这个profile在系统属性(one.of.system.property)为指定值(expected.value)时被激活 你可以在命令行设置系统属性值: mvn clean install -Done.of.system.property=expected.value --><profile><id>profile2</id><activation><property><name>one.of.system.property</name><value>expected.value</value></property></activation><properties><!-- ...... --></properties></profile><!-- 激活方式4: 根据操作系统的环境激活 --><profile><id>profile3</id><activation><os><name>Windows 10</name><family>Windows</family><arch>amd64</arch><version>10.0</version></os></activation><properties><!-- ...... --></properties></profile><!-- 激活方式5: 基于文件存在与否激活: x.properties存在激活,y.properties不存在激活 --><profile><id>profile4</id><activation><file><exists>x.properties</exists><missing>y.properties</missing></file></activation><properties><!-- ...... --></properties></profile><!-- 说明: 1. 如果有任意一个profile通过非默认的方式激活了,那么默认激活的profile就会失效 2. 在settings.xml中设置的profile不允许修改大部分pom元素,比如声明依赖或修改插件,而在项目中pom.xml中的profile中,可以修改pom原始的配置,比如maven的那些默认路径配置 --></profiles><!-- 激活已经配置的profile --><activeprofiles><activeprofile>dev</activeprofile></activeprofiles></settings>
4. pom.xml文件内容总结代码语言:javascript代码运行次数:0运行复制
<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"><modelversion>4.0.0</modelversion><groupid>com.bonc.rdpe</groupid><artifactid>maven001</artifactid><version>0.0.1-SNAPSHOT</version><!-- 打包类型为jar,默认为jar,web项目应该是war,其他还有: maven-plugin/ear/pom等 --><packaging>jar</packaging><!--附属构件,不能直接定义 --><classifier></classifier><name>maven001</name><!-- 自定义maven属性 --><properties><hadoop.version>2.7.6</hadoop.version><junit.version>4.7</junit.version><spark.version>2.3.0</spark.version><springframework.version>2.5.6</springframework.version></properties><repositories><!-- maven中央仓库,不需要配置,这里是为了说明各配置的功能 --><repository><!-- id值必须唯一,否则会覆盖 --><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url><layout>default</layout><releases><!-- 下载发布版构件 --><enabled>true</enabled><!-- Maven 从远程仓库更新的频率 never(从不) daily(每天一次,默认) always(每次构建都检查更新) interval:X(每隔X分钟检查一次,X为任意整数) --><updatepolicy>never</updatepolicy><!-- 检验文件的策略,如果校验失败: warn(默认):输出警告信息 fail: 构建失败) ignore: 忽略 --><checksumpolicy>warn</checksumpolicy></releases><snapshots><!-- 不从中央仓库下载快照版本的构件 --><enabled>false</enabled></snapshots></repository><!-- JBOSS Maven 仓库 --></repositories><!-- 配置下载插件的远程仓库,各配置和repositories中的意义是一样的 --><pluginrepositories><pluginrepository><id></id><name></name><url></url><layout></layout><releases><updatepolicy></updatepolicy></releases><snapshots><enabled></enabled></snapshots></pluginrepository></pluginrepositories><!-- 项目输出的构件部署的远程仓库 --><distributionmanagement><!-- 发布版的构件部署的仓库 --><repository><id>dev3-repository</id><name>Dev3 Release Repository</name><url>http://172.16.13.185:9000/nexus/content/repositories/releases/</url></repository><!-- 快照版的构件部署的仓库 --><snapshotrepository><id>dev3-snapshots-repository</id><name>Dev3 Snapshot Repository</name><url>http://172.16.13.185:9000/nexus/content/repositories/snapshots/</url></snapshotrepository></distributionmanagement><!-- 在这里声明的依赖会被子模块继承,但是,不是声明了就一定会被继承,如果子模块不引入这些依赖,就不会强制加给子模块 --><dependencymanagement><dependencies><dependency><groupid>org.springframework</groupid><artifactid>spring-core</artifactid><version>${springframework.version}</version></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-beans</artifactid><version>${springframework.version}</version></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-context</artifactid><version>${springframework.version}</version></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-context-support</artifactid><version>${springframework.version}</version></dependency><dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>${junit.version}</version><scope>test</scope></dependency><!-- 合并其他maven工程中的dependencyManagement下定义的依赖到本模块的dependencyManagement下 --><dependency><groupid>com.bonc.rdpe</groupid><artifactid>account-xxx</artifactid><version>0.0.1-SNAPSHOT</version><!--import 只能用在dependencyManagement中,且目标POM的packaging必须为pom,这里的type也只能为pom--><type>pom</type><scope>import</scope></dependency></dependencies></dependencymanagement><!-- 本模块使用的构件 --><dependencies><dependency><groupid>org.apache.spark</groupid><artifactid>spark-sql_2.11</artifactid><version>${spark.version}</version><!-- 依赖类型,默认是jar,而且绝大部分为jar,不需要配置 --><type>jar</type><!-- 依赖范围,默认compile,可以是test/provided/runtime/system,当为system时需要systemPath指定jar包路径--><scope>system</scope><systempath>${JAVA_HOME}/lib/rt.jar</systempath><!-- 是否可选,默认false,如果是ture,则当其他项目依赖此项目时,其他项目不会依赖这个jar包,不建议在项目中使用可选依赖 --><optional>true</optional><exclusions><!--排除掉spark-sql_2.11-2.3.0.jar依赖的hadoop-client-x.x.x.jar --><exclusion><groupid>org.apache.hadoop</groupid><artifactid>hadoop-client</artifactid></exclusion></exclusions></dependency><!-- 排除掉spark-sql依赖的jar后,需要手动添加项目需要的版本的hadoop-client包 --><dependency><groupid>org.apache.hadoop</groupid><artifactid>hadoop-client</artifactid><version>${hadoop.version}</version></dependency></dependencies><!-- maven工程需要的额外配置 --><!-- 以下例子为: 配置了两套不同的数据库环境 开发人员和测试人员使用时可以根据自己的角色激活对应profile --><profiles><profile><id>dev</id><properties><db.driver>com.mysql.jdbc.Driver</db.driver><db.url>jdbc:mysql://192.168.138.101:3306/dev</db.url><db.username>dev</db.username><db.password>dev-pwd</db.password></properties></profile><profile><id>dev</id><properties><db.driver>com.mysql.jdbc.Driver</db.driver><db.url>jdbc:mysql://192.168.138.101:3306/test</db.url><db.username>test</db.username><db.password>test-pwd</db.password></properties></profile></profiles><build><!-- 在这里声明的插件会被子模块继承 --><pluginmanagement><plugins><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-resources-plugin</artifactid><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins></pluginmanagement><plugins><!-- 以下是一些maven的默认路径配置,你可以自定义,但不建议 --><!-- 主输出目录 --><directory>${project.basedir}/target</directory><!-- 主代码输出目录 --><outputdirectory>${project.build.directory}/classes</outputdirectory><!-- 最终构建的名称格式 --><finalname>${project.artifactId}-${project.version}</finalname><!-- 测试代码输出目录 --><testoutputdirectory>${project.build.directory}/test-classes</testoutputdirectory><!-- 主源码目录 --><sourcedirectory>${project.basedir}/src/main/java</sourcedirectory><!-- 脚本源码目录 --><scriptsourcedirectory>src/main/scripts</scriptsourcedirectory><!-- 测试源码目录 --><testsourcedirectory>${project.basedir}/src/test/java</testsourcedirectory><!-- 主资源目录 --><resources><resource><directory>${project.basedir}/src/main/resources</directory><!-- 开启资源过滤,作用: 开启后,资源目录下的配置为文件中就可以识别profile中定义的maven属性 --><filtering>true</filtering></resource></resources><!-- 测试资源目录 --><testresources><testresource><directory>${project.basedir}/src/test/resources</directory><filtering>true</filtering></testresource></testresources><!-- maven编译插件 --><plugin><!-- 如果插件的groupId=org.apache.maven.plugins,可以省略 --><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><!-- 如果没有设置插件版本,maven会获取最新的发布版 --><configuration><source>1.8</source><target>1.8</target><!-- 是否跳过测试代码的编译,默认为false,不建议设置为true --><skip>true</skip></configuration></plugin><!-- maven源码生成插件 --><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-source-plugin</artifactid><!-- 对于自定义绑定的插件,建议手动指定一个非快照的版本 --><version>3.0.1</version><executions><!-- 每个execution用来配置一个任务 --><execution><id>attach-source</id><!-- 绑定到default生命周期的verify阶段 --><phase>verify</phase><!-- 任务目标 --><goals><goal>jar-no-fork</goal></goals></execution></executions></plugin><!-- maven打包插件: 相对于默认的maven-jar-plugin,这个插件打的包是可执行的 --><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-shade-plugin</artifactid><version>3.1.1</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><!--设置主类--><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainclass>com.bonc.rdpe.maven001.HelloWorld</mainclass></transformer></transformers></configuration></execution></executions></plugin><!-- maven默认的打包插件 --><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-jar-plugin</artifactid><version>3.1.0</version><executions><execution><goals><!-- 设置打包的时候把测试代码也打包,默认为jar(即只打包主代码) --><goal>test-jar</goal></goals></execution></executions></plugin><!-- web项目打包插件 --><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-war-plugin</artifactid><version>3.2.2</version><configuration><webresources><webresource><directory>src/main/webapp</directory><filtering>true</filtering><!-- 所有的css文件和js文件或被打到war包中 --><includes><include>**/*.css</include><include>**/*.js</include></includes></webresource></webresources></configuration></plugin><!-- maven测试插件 --><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-surefire-plugin</artifactid><version>2.22.0</version><configuration><!-- 修改了测试报告的存放为位置,默认为${project.build.directory}/surefire-reports --><reportsdirectory>${project.build.directory} est-reports</reportsdirectory><!-- 是否跳过测试代码的运行,默认为false,不建议设置为true --><skiptests>true</skiptests><!-- 默认情况下,maven会测试以Test开头和结尾、以TestCase结尾的类 你可以设置让maven测试不符合测试类命名规范的类 **/*Tests.java: 测试任意路径下的以Tests结尾的类 --><includes><include>**/*Tests.java</include></includes><!-- 当然,也可以设置排除掉一些测试类 **/*ServiceTest.java: 任意路径下的ServiceTest结尾的类都不会测试 **/ArrayTest.java: 任意路径下的ArrayTest这个类都不会被测试 --><excludes><exclude>**/*ServiceTest.java</exclude><exclude>**/ArrayTest.java</exclude></excludes><!-- 可以在外部自定义的xml文件来设置测试哪些类,这里的配置用来声明该外部文件的位置 这个功能需要使用TestNG这个jar包,而不是JUnit --><suitexmlfiles><suitexmlfile>testng.xml</suitexmlfile></suitexmlfiles><!-- @Test方法可以声明其属于哪个组,这个设置测试哪些组中的方法,例如 @Test(groups = {"group1"}) public void testXXX() {...} 这个功能需要使用TestNG这个jar包,而不是JUnit --><groups>group1</groups></configuration></plugin></plugins></build></project>
5. 超级pom代码语言:javascript代码运行次数:0运行复制
<?xml version="1.0" encoding="UTF-8"?><project><modelversion>4.0.0</modelversion><repositories><repository><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginrepositories><pluginrepository><id>central</id><name>Central Repository</name><url>https://repo.maven.apache.org/maven2</url><layout>default</layout><snapshots><enabled>false</enabled></snapshots><releases><updatepolicy>never</updatepolicy></releases></pluginrepository></pluginrepositories><build><directory>${project.basedir}/target</directory><outputdirectory>${project.build.directory}/classes</outputdirectory><finalname>${project.artifactId}-${project.version}</finalname><testoutputdirectory>${project.build.directory}/test-classes</testoutputdirectory><sourcedirectory>${project.basedir}/src/main/java</sourcedirectory><scriptsourcedirectory>${project.basedir}/src/main/scripts</scriptsourcedirectory><testsourcedirectory>${project.basedir}/src/test/java</testsourcedirectory><resources><resource><directory>${project.basedir}/src/main/resources</directory></resource></resources><testresources><testresource><directory>${project.basedir}/src/test/resources</directory></testresource></testresources><pluginmanagement><plugins><plugin><artifactid>maven-antrun-plugin</artifactid><version>1.3</version></plugin><plugin><artifactid>maven-assembly-plugin</artifactid><version>2.2-beta-5</version></plugin><plugin><artifactid>maven-dependency-plugin</artifactid><version>2.8</version></plugin><plugin><artifactid>maven-release-plugin</artifactid><version>2.5.3</version></plugin></plugins></pluginmanagement></build><reporting><outputdirectory>${project.build.directory}/site</outputdirectory></reporting><profiles><profile><id>release-profile</id><activation><property><name>performRelease</name><value>true</value></property></activation><build><plugins><plugin><inherited>true</inherited><artifactid>maven-source-plugin</artifactid><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><artifactid>maven-javadoc-plugin</artifactid><executions><execution><id>attach-javadocs</id><goals><goal>jar</goal></goals></execution></executions></plugin><plugin><inherited>true</inherited><artifactid>maven-deploy-plugin</artifactid><configuration><updatereleaseinfo>true</updatereleaseinfo></configuration></plugin></plugins></build></profile></profiles></project>