• 仓库
    • mvnrepository.com
  • 依赖范围
    • compile # 默认,对编译、测试、运行有效
    • test # 对测试有效
    • runtime # 对测试和运行有效
    • provided # 编译和测试有效
    • system # 本地仓库
    • import
  • mvn # 相当于mvn compile
    • 全局
      • version # 版本
      • e # 错误详情
      • help:describe # help插件的describe
        • Dplugin=help # 显示help插件的详情
        • Dfull # 显示完整参数
      • help:effective-pom # 显示默认设置
    • 生成
      • archetype:create # 创建java项目
        • DgroupId=com.outrun
        • DartifactId=erp
        • Dversion=0.0.1-SNAPSHOT
        • DarchetypeArtifactId=maven-archetype-webapp # 指定模板为webapp
      • archetype:generate # 向导创建项目
      • site # 产生html文档
      • source:jar # 源码打包
      • generate-sources # 生成源码, 如xdoclet
      • eclipse:eclipse # 生成或转化成eclipse工程
      • eclipse:clean # 清除eclipse设置
      • idea:idea # 生成idea项目
      • install # compile, package后, 保存到本地仓库
        • X # 显示依赖
        • Dmaven.test.skip=true # 跳过测试
        • rf 模块名 # 从指定模块从新开始
    • 执行
      • validate # 项目验证
      • verify # 验证包
      • compile # 编译
        • exec:java # 编译完成后,执行java main方法
      • test-compile # 编译测试代码
      • test # 运行测试
        • skipping # 跳过
          • compile # 不编译
          • test-compile # 不编译测试
      • integration-test # 集成测试
      • package # 打包
        • Dmaven.test.skip=true # 跳过单元测试,不编译
        • DskipTests # 跳过单元测试,编译
      • clean # 清除编译
        • install-U # 强制更新
        • package # 编译成jar包
      • deploy # install后, 上传
      • jar:jar # 打jar包
    • 插件
      • jetty:run # 引入jetty-plugin后, 运行jetty
      • tomcat:run
    • 分析
      • dependency:list # 列出依赖
      • dependency:tree # 列出依赖树
      • dependency:analyze # 依赖分析, 未使用的做标记
      • dependency:resolve # 列出已解决的依赖
      • dependency:sources # 下载源码
      • dependency:copy-dependencies # 得到jar包
  • 常用
    • 分析包依赖
      • mvn dependency:tree -Dverbose -Dincludes=org.apache.commons:commons-lang3
    • 清理打包文件
      • mvn clean package -DskipTests
    • idea工具
      • 打包了带main方法的jar不能引用
      • 在父项目运行mvn package, model中运行会找不到其它model
      • model运行前先mvn package
    • 手动添加依赖
      • mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar -Dfile=ojdbc7.jar
  • 配置
    • <groupId> # 包名 - <artifactId> # 项目名 - <version> - <packaging> # 打包方式, war, jar - <parent> # 父模块 - <properties> - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> - <java.version>1.8</java.version> - <spring-cloud.version>Dalston.RELEASE</spring-cloud.version> - </properties> - <dependencies> # 子模块继承 - <dependency> - <groupId> - <artifactId> - <version> - LATEST - ${spring-cloud.version} 引用properties中定义的变量 - <scope> # 何时使用 - compile - provided # 类似compile - runtime - test - system
      • </dependency> - </dependencies> - <dependenciesManager> # 子模块不继承, 继承时需要声明 - <dependencies> - </dependenciesManager> - <build> - <plugins> - <plugin> - <groupId> - <artifactId> - </plugin> - </plugins> - </build>- 插件
    • 介绍
      • 按顺序执行,完成maven生命周期
      • 无配置时调默认插件
    • 生命周期(lifecycle)顺序
      • clean # 清除target目录
      • resources # 复制resources下文件到target/classes
      • complie # 包含resources, 编译java下文件到target/classes
      • testResources # 复制test/resources下文件到target/test-classes
      • testCompile # 包含testResources, 编译test/java下文件到target/test-classes
      • test # 包含resources, compile, testResources, testCompile, test
      • package
      • jar # 打包class文件, 配置文件, 不打包lib
      • install
    • maven-clean-plugin
    • maven-resources-plugin
      • <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-resources-plugin</artifactId> - <version>2.6</version> - <executions> - <execution> - <id>copy-resources</id> - <phase>validate</phase> - <goals> - <goal>copy-resources</goal> - </goals> - <configuration> - <outputDirectory>${project.build.outputDirectory}</outputDirectory> - <resources> - <resource> - <directory>src/main/${deploy.env}/applicationContext.xml</directory> - <excludes> - <exclude>WEB-INF/.</exclude> - </excludes> - <filtering>false</filtering> - </resource> - </resources> - </configuration> - <inherited></inherited> - </execution> - </executions> - </plugin> - maven-compiler-plugin
    • maven-surefire-plugin # 对应test, 单元测试
    • maven-dependency-plugin # 打包lib
    • maven-jar-plugin
      • <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-jar-plugin</artifactId> - <version>2.6</version> - <configuration> - <archive> - <manifest> - <addClasspath>true</addClasspath> - <classpathPrefix>lib/</classpathPrefix> - <mainClass>com.xxx.xxxService</mainClass> - </manifest> - </archive> - </configuration> - </plugin> - <plugin> # 单独打包lib - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-dependency-plugin</artifactId> - <version>2.10</version> - <executions> - <execution> - <id>copy-dependencies</id> - <phase>package</phase> - <goals> - <goal>copy-dependencies</goal> - </goals> - <configuration> - <outputDirectory>${project.build.directory}/lib</outputDirectory> - </configuration> - </execution> - </executions> - </plugin> - maven-assembly-plugin # 打包lib, 有bug缺失spring xds文件, 同级jar会冲突
      • <plugin> - <artifactId>maven-assembly-plugin</artifactId> - <configuration> - <descriptorRefs> - <descriptorRef>jar-with-dependencies</descriptorRef> - </descriptorRefs> - <archive> - <manifest> - <mainClass>com.xxx.xxxService</mainClass> - </manifest> - </archive> - </configuration> - <executions> - <execution> - <id>make-assembly</id> - <phase>package</phase> - <goals> - <goal>single</goal> - </goals> - </execution> - </executions> - </plugin> - maven-shade-plugin # 打包lib, 同级jar会冲突, 提示SF,DSA,RSA冲突,排除META-INF相关文件
      • <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-shade-plugin</artifactId> - <version>2.4.3</version> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>shade</goal> - </goals> - <configuration> - <filters> - <filter> - <artifact>:</artifact> - <excludes> - <exclude>META-INF/.SF</exclude> - <exclude>META-INF/.DSA</exclude> - <exclude>META-INF/*.RSA</exclude> - </excludes> - </filter> - </filters> - <transformers> - <transformer - implementation=“org.apache.maven.plugins.shade.resource.AppendingTransformer”> - <resource>META-INF/spring.handlers</resource> - </transformer> - <transformer - implementation=“org.apache.maven.plugins.shade.resource.AppendingTransformer”> - <resource>META-INF/spring.schemas</resource> - </transformer> - <transformer - implementation=“org.apache.maven.plugins.shade.resource.AppendingTransformer”> - <resource>META-INF/spring.tooling</resource> - </transformer> - <transformer - implementation=“org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”> - <mainClass>com.xxx.xxxInvoke</mainClass> - </transformer> - </transformers> - <minimizeJar>true</minimizeJar> - <shadedArtifactAttached>true</shadedArtifactAttached> - </configuration> - </execution> - </executions> - </plugin> - maven-install-plugin
    • spring-boot-maven-plugin
    • gradle-maven-plugin
    • protobuf-maven-plugin
    • build-helper-maven-plugin # 用于指定自定义目录
    • dockerfile-maven-plugin # root用户直接打包到docker images
      • <plugin> - <groupId>com.spotify</groupId> - <artifactId>dockerfile-maven-plugin</artifactId> - <version>1.4.10</version> - <configuration> - <repository>${project.artifactId}</repository> - <contextDirectory>./</contextDirectory> - <tag>${project.version}</tag> - <buildArgs> - <JAR_FILE>mqtt/target/*.jar</JAR_FILE> - </buildArgs> - </configuration> - </plugin> - ./Dockerfile
        • FROM primetoninc/jdk:1.8
        • ADD mqtt/target/*.jar app.jar
        • ARG JAR_FILE
        • COPY ${JAR_FILE} /opt/app.jar
        • ENTRYPOINT [“java”, “-jar”, “/app.jar”]
      • mvn package dockerfile:build
  • 方案
    • 新项目安装
      • mvn clean install -DskipTests
      • mvn install -rf :模块名 -DskipTests # 指定模块开始
    • ojdbc14本地加载
      • # oracle是收费的,所以不能直接下载到驱动
      • o mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=ojdbc14-10.2.0.4.0.jar
      • o 把ojdbc14-10.2.0.4.0.jar复制到目录下: /home/outrun/.m2/repository/com/oracle/ojdbc14/10.2.0.4.0/
      • o /home/outrun/.m2/repository/com/oracle/ojdbc14/下会产生maven-metadata-local.xml文件存放maven引入依赖
      • o 项目中引入本地依赖
        • <dependency> - <groupId>com.oracle</groupId> - <artifactId>ojdbc14</artifactId> - <version>10.2.0.4.0</version> - </dependency> - 代理
      • 复制$M2_HOME/conf/settings.xml到.m2/
      • settings.xml
        • <proxies> - <proxy> - <id>my-proxy</id> - <active>true</active> - <protocol>http</protocol> - <host>localhost</host> - <port>8123</port> - <!— - <username>admin</username> - <password>admin</password> - <nonProxyHosts>repository.mycom.com|*.google.com</nonProxyHosts> -
          • </proxy> - </proxies>