package、install、deploy区别

后者都比前者多一步。

mvn clean package依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)等7个阶段。
mvn clean install依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install等8个阶段。
mvn clean deploy依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install、deploy等9个阶段。

package target目录输出打好的Jar包
install target目录输出打好的Jar包,布署到本地maven仓库
deploy target目录输出打好的Jar包,布署到本地maven仓库和远程maven私服仓库

Clean

清理指定目录。默认是连目录一起删除

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-clean-plugin</artifactId>
  4. <version>3.1.0</version>
  5. <configuration>
  6. <filesets>
  7. <fileset>
  8. <directory>${basedir}/plugins</directory>
  9. </fileset>
  10. <fileset>
  11. <directory>${basedir}/sqlplugins</directory>
  12. </fileset>
  13. </filesets>
  14. </configuration>
  15. </plugin>

compiler

maven-compiler-plugin
指定版本,要不会默认成Java1.5,编译不通过。

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.apache.maven.plugins</groupId>
  5. <artifactId>maven-compiler-plugin</artifactId>
  6. <version>3.8.1</version>
  7. <configuration>
  8. <source>1.8</source>
  9. <target>1.8</target>
  10. </configuration>
  11. </plugin>
  12. </plugins>
  13. </build>

打包

maven-jar-plugin和maven-dependency-plugin

用于生成META-INF/MANIFEST.MF文件的部分内容,com.xxg.Main指定MANIFEST.MF中的Main-Class,true会在MANIFEST.MF加上Class-Path项并配置依赖包,lib/指定依赖包所在目录。
只是生成MANIFEST.MF文件还不够,maven-dependency-plugin插件用于将依赖包拷贝到${project.build.directory}/lib指定的位置,即lib目录下。

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.6</version>
  5. <configuration>
  6. <archive>
  7. <manifest>
  8. <addClasspath>true</addClasspath>
  9. <classpathPrefix>lib/</classpathPrefix>
  10. <mainClass>com.xxg.Main</mainClass>
  11. </manifest>
  12. </archive>
  13. </configuration>
  14. </plugin>
  15. <plugin>
  16. <groupId>org.apache.maven.plugins</groupId>
  17. <artifactId>maven-dependency-plugin</artifactId>
  18. <version>2.10</version>
  19. <executions>
  20. <execution>
  21. <id>copy-dependencies</id>
  22. <phase>package</phase>
  23. <goals>
  24. <goal>copy-dependencies</goal>
  25. </goals>
  26. <configuration>
  27. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  28. </configuration>
  29. </execution>
  30. </executions>
  31. </plugin>

maven-assembly-plugin

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-assembly-plugin</artifactId>
  4. <version>2.4.1</version>
  5. <configuration>
  6. <descriptorRefs>
  7. <descriptorRef>jar-with-dependencies</descriptorRef>
  8. </descriptorRefs>
  9. <archive>
  10. <manifest>
  11. <mainClass>org.gumiho.rest.App</mainClass>
  12. </manifest>
  13. </archive>
  14. </configuration>
  15. <executions>
  16. <execution>
  17. <id>make-assembly</id>
  18. <phase>package</phase>
  19. <goals>
  20. <goal>single</goal>
  21. </goals>
  22. </execution>
  23. </executions>
  24. </plugin>

maven-shade-plugin

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-shade-plugin</artifactId>
  4. <version>2.4.1</version>
  5. <executions>
  6. <execution>
  7. <phase>package</phase>
  8. <goals>
  9. <goal>shade</goal>
  10. </goals>
  11. <configuration>
  12. <transformers>
  13. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  14. <mainClass>com.xxg.Main</mainClass>
  15. </transformer>
  16. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  17. <resource>META-INF/spring.handlers</resource>
  18. </transformer>
  19. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  20. <resource>META-INF/spring.schemas</resource>
  21. </transformer>
  22. </transformers>
  23. </configuration>
  24. </execution>
  25. </executions>
  26. </plugin>

Jar

/META-INF/MANIFEST.MF
运行Jar时JVM会读这个文件,如果里面有Main-Class,就不用命令行指定主类了。
jar包还可以包含其它jar包,这个时候,就需要在MANIFEST.MF文件里配置classpath了。

  1. Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
  2. Main-Class: com.xxg.Main

frontend-maven-plugin

https://www.jianshu.com/p/b378dfad09f6

Test

https://stackoverflow.com/questions/12828416/generate-test-jar-along-with-jar-file-in-test-package

https://maven.apache.org/guides/mini/guide-attached-tests.html
跳过测试失败

  1. mvn clean test -Dmaven.test.failure.ignore=true

传递依赖的规则

依赖调节(Dependency mediation)

就近原则

如下图,甲间接依赖丁。根据就近原则会依赖最靠近自己的、层级最低的丁1.0版本。
image.png

  1. <mirror>
  2. <id>aliyunmaven</id>
  3. <mirrorOf>!apache snapshots</mirrorOf>
  4. <name>阿里云公共仓库</name>
  5. <url>https://maven.aliyun.com/repository/central</url>
  6. </mirror>
  7. <mirror>
  8. <id>aliyunmaven</id>
  9. <mirrorOf>apache snapshots</mirrorOf>
  10. <name>阿里云阿帕奇仓库</name>
  11. <url>https://maven.aliyun.com/repository/apache-snapshots</url>
  12. </mirror>

https://developer.aliyun.com/mvn/guide?spm=a2c6h.13651104.0.0.435836a4HkFEEU

-P 在PowerShell有问题

https://stackoverflow.com/questions/16792499/how-do-i-invoke-two-different-profiles-in-one-maven-command

某模块失败继续

  1. --fail-at-end

Maven shaded 不能反序列化问题 maven shade deserialize error
Invalid lambda deserialization
https://issues.apache.org/jira/browse/MSHADE-260