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
清理指定目录。默认是连目录一起删除
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/plugins</directory>
</fileset>
<fileset>
<directory>${basedir}/sqlplugins</directory>
</fileset>
</filesets>
</configuration>
</plugin>
compiler
maven-compiler-plugin
指定版本,要不会默认成Java1.5,编译不通过。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
打包
maven-jar-plugin和maven-dependency-plugin
用于生成META-INF/MANIFEST.MF文件的部分内容,
只是生成MANIFEST.MF文件还不够,maven-dependency-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.xxg.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.gumiho.rest.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
maven-shade-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.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.xxg.Main</mainClass>
</transformer>
<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>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Jar
/META-INF/MANIFEST.MF
运行Jar时JVM会读这个文件,如果里面有Main-Class,就不用命令行指定主类了。
jar包还可以包含其它jar包,这个时候,就需要在MANIFEST.MF文件里配置classpath了。
Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
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
跳过测试失败
mvn clean test -Dmaven.test.failure.ignore=true
传递依赖的规则
依赖调节(Dependency mediation)
就近原则
如下图,甲间接依赖丁。根据就近原则会依赖最靠近自己的、层级最低的丁1.0版本。
<mirror>
<id>aliyunmaven</id>
<mirrorOf>!apache snapshots</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>apache snapshots</mirrorOf>
<name>阿里云阿帕奇仓库</name>
<url>https://maven.aliyun.com/repository/apache-snapshots</url>
</mirror>
https://developer.aliyun.com/mvn/guide?spm=a2c6h.13651104.0.0.435836a4HkFEEU
-P 在PowerShell有问题
某模块失败继续
--fail-at-end
Maven shaded 不能反序列化问题 maven shade deserialize error
Invalid lambda deserialization
https://issues.apache.org/jira/browse/MSHADE-260