Project结构
maven-parent/pom.xml
<groupId>com.archon.example</groupId>
<artifactId>maven-parent</artifactId>
<packaging>pom</packaging>
<version>0.1</version>
<modules>
<module>maven-core</module>
<module>maven-plugin</module>
</modules>
maven-parent/maven-core/pom.xml
<parent>
<artifactId>maven-parent</artifactId>
<groupId>com.archon.example</groupId>
<version>0.1</version>
</parent>
<artifactId>maven-core</artifactId>
<packaging>jar</packaging>
maven-parent/maven-plugin/pom.xml
<parent>
<artifactId>maven-parent</artifactId>
<groupId>com.archon.example</groupId>
<version>0.1</version>
</parent>
<artifactId>maven-plugin</artifactId>
<packaging>jar</packaging>
artifact版本管理规范
一般artifact(依赖包)的版本信息全部在maven-parent中声明
PS: 被依赖的子模块也需要一并引入(一般全部子模块都引入即可maven-core maven-plugin)
maven-parent/pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<artifactId>maven-core</artifactId>
<groupId>com.archon.example</groupId>
<version>${maven.core.version}</version>
</dependency>
<dependency>
<artifactId>maven-plugin</artifactId>
<groupId>com.archon.example</groupId>
<version>${maven.plugin.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.11</version>
</dependency>
</dependencies>
</dependencyManagement>
同级引用
在maven-plugin中引入maven-core以及logback(用于测试打入依赖包)
maven-parent/maven-plugin/pom.xml
<dependencies>
<dependency>
<artifactId>maven-core</artifactId>
<groupId>com.archon.example</groupId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
引入之后可以本地run,但mvn compile需要maven本地仓库有
maven-parent的pom
maven-corepom和jar
此时需要在maven-parent中运行mvn install(并非在maven-core中运行,这样会导致缺少maven-parent的pom)
打包
maven默认插件
在<packaging>jar</packaging>的情况下,jdk版本默认为1.5(可能是IDEA的设置,没深究)
打包,不包含artifact(依赖包)
由于maven默认就引入了以上插件,所以可以直接打包
mvn package
为确保jdk版本,最好手动引入compile
maven-parent/pom.xml(直接覆盖整个项目)
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
打包,包含artifact(依赖包)
使用maven的assembly,默认不引入,需要手动引入并注意版本
maven-parent/maven-plugin/pom.xml
<properties>
<!-- 3.x版本没有assembly方法,只有single -->
<maven-assembly-plugin.version>2.6</maven-assembly-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<archive>
<manifest>
<mainClass>plugin.PluginMain</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
运行打包
mvn assembly:assembly
其他打包
打包,artifact(依赖包)分离
少用
maven-shade-plugin
略
Spring Boot
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>