maven 随笔
一、项目构建
使用maven构建项目时,优秀的做法是创建一个parent工程,用parent的pom.xml将所有子模块聚合到一起
使用parent聚合子模块的好处:
1、parent工程可以统一管理所有依赖的版本,子模块只需要继承parent,有效避免各模块之间版本各异。
2、对parent打包即可同时将所有子模块自动打包,不需要为每个子模块单独打包
项目示例:
app-parent/pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.oldpub.app</groupId> 6 <artifactId>app-parent</artifactId> 7 <version>${revision}</version> 8 <packaging>pom</packaging> 9 <name>app-parent</name> 10 11 <modules> 12 <module>../app-starter</module> 13 <module>../app-core</module> 14 </modules> 15 16 <properties> 17 <revision>1.0</revision> 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <maven.compiler.source.version>1.8</maven.compiler.source.version> 20 <maven.compiler.target.version>1.8</maven.compiler.target.version> 21 <junit.version>4.11</junit.version> 22 </properties> 23 24 <dependencyManagement> 25 <dependencies> 26 <dependency> 27 <groupId>junit</groupId> 28 <artifactId>junit</artifactId> 29 <version>${junit.version}</version> 30 </dependency> 31 </dependencies> 32 </dependencyManagement> 33 34 <build> 35 <pluginManagement> 36 <plugins> 37 <plugin> 38 <artifactId>maven-compiler-plugin</artifactId> 39 <version>3.8.0</version> 40 <configuration> 41 <encoding>${project.build.sourceEncoding}</encoding> 42 <source>${maven.compiler.source.version}</source> 43 <target>${maven.compiler.target.version}</target> 44 </configuration> 45 </plugin> 46 <plugin> 47 <artifactId>maven-install-plugin</artifactId> 48 <version>2.5.2</version> 49 </plugin> 50 <plugin> 51 <artifactId>maven-jar-plugin</artifactId> 52 <version>3.0.2</version> 53 </plugin> 54 <plugin> 55 <artifactId>maven-dependency-plugin</artifactId> 56 <version>2.10</version> 57 </plugin> 58 <plugin> 59 <artifactId>maven-antrun-plugin</artifactId> 60 <version>3.0.0</version> 61 </plugin> 62 </plugins> 63 </pluginManagement> 64 </build> 65 </project>
app-starter/pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <parent> 5 <artifactId>app-parent</artifactId> 6 <groupId>com.oldpub.app</groupId> 7 <version>${revision}</version> 8 <relativePath>../app-parent/pom.xml</relativePath> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>app-starter</artifactId> 12 <name>app-starter</name> 13 14 <dependencies> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <scope>test</scope> 19 </dependency> 20 </dependencies> 21 22 <build> 23 <plugins> 24 <plugin> 25 <artifactId>maven-compiler-plugin</artifactId> 26 </plugin> 27 <plugin> 28 <artifactId>maven-install-plugin</artifactId> 29 </plugin> 30 <plugin> 31 <artifactId>maven-jar-plugin</artifactId> 32 <configuration> 33 <excludes> 34 <exclude>*.properties</exclude> 35 </excludes> 36 </configuration> 37 </plugin> 38 <plugin> 39 <artifactId>maven-dependency-plugin</artifactId> 40 <executions> 41 <execution> 42 <id>copy</id> 43 <phase>package</phase> 44 <goals> 45 <goal>copy-dependencies</goal> 46 </goals> 47 <configuration> 48 <outputDirectory> 49 ${project.build.directory}/lib 50 </outputDirectory> 51 <excludeArtifactIds>junit,hamcrest-core</excludeArtifactIds> 52 </configuration> 53 </execution> 54 </executions> 55 </plugin> 56 <plugin> 57 <!--把jar包复制到指定的位置--> 58 <artifactId>maven-antrun-plugin</artifactId> 59 <executions> 60 <execution> 61 <id>artifact-copy</id> 62 <phase>package</phase> 63 <goals> 64 <goal>run</goal> 65 </goals> 66 <configuration> 67 <target> 68 <!-- todir 指定要复制到哪个位置--> 69 <copy todir="${project.build.directory}/lib"> 70 <fileset dir="${project.build.directory}"> 71 <include name="${project.artifactId}-${project.version}.jar"/> 72 </fileset> 73 </copy> 74 </target> 75 </configuration> 76 </execution> 77 </executions> 78 </plugin> 79 </plugins> 80 </build> 81 </project>
app-core/pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <parent> 5 <artifactId>app-parent</artifactId> 6 <groupId>com.oldpub.app</groupId> 7 <version>${revision}</version> 8 <relativePath>../app-parent/pom.xml</relativePath> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 <artifactId>app-core</artifactId> 12 <name>app-core</name> 13 14 <dependencies> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <scope>test</scope> 19 </dependency> 20 </dependencies> 21 22 <build> 23 <plugins> 24 <plugin> 25 <artifactId>maven-compiler-plugin</artifactId> 26 </plugin> 27 <plugin> 28 <artifactId>maven-install-plugin</artifactId> 29 </plugin> 30 <plugin> 31 <artifactId>maven-jar-plugin</artifactId> 32 </plugin> 33 </plugins> 34 </build> 35 </project>
打包出源码,添加maven的source插件
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
如果打包成war(<packaging>war</packaging>),在 pom.xml 中可配置如下构建方式,指定webapp名称与war包名称
1 <build> 2 <!--指定webapp名称,不带版本号后缀--> 3 <finalName>${project.artifactId}</finalName> 4 <plugins> 5 <plugin> 6 <artifactId>maven-compiler-plugin</artifactId> 7 </plugin> 8 <plugin> 9 <artifactId>maven-install-plugin</artifactId> 10 </plugin> 11 <plugin> 12 <artifactId>maven-war-plugin</artifactId> 13 <configuration> 14 <!--指定war包名称,不带版本号后缀--> 15 <warName>${project.artifactId}</warName> 16 </configuration> 17 </plugin> 18 </plugins> 19 </build>
tips:windows操作系统下打包,可在app-parent目录下编写一个 .bat 脚本,双击可以快捷打包
mvn-package.bat:
1 ::author@oldpub 2 ::ANSI file encoding is required under windows OS 3 ::cd %~dp0 表示回到脚本文件所在目录 4 @title MyAPP Package 5 @echo off 6 :begin 7 echo. 8 echo ===================== MyAPP 打包 ===================== 9 echo :pack 开始打包命令 10 echo :end 退出窗口命令 11 echo ===================================================== 12 echo. 13 14 set/p cmd=输入上方命令: 15 if "%cmd%"=="end" goto :end 16 if "%cmd%"=="pack" goto :pack 17 echo 命令错误,请重新输入 18 goto :begin 19 20 ::开始打包 21 :pack 22 cd %~dp0 23 call mvn clean install -D skipTests=true -U -e 24 echo. 25 echo ===================================================== 26 echo 即将打开 target 目录...... 27 echo ===================================================== 28 echo. 29 call start ..\app-starter\target 30 pause 31 goto :begin 32 33 :end 34 exit
mvn Options:
-e,--errors Produce execution error messages
-U,--update-snapshots Forces a check for missing releases and updated snapshots on remote repositories
-X,--debug Produce execution debug output
二、依赖树与冲突解决
IDEA插件 Maven Helper,可帮助查看依赖冲突(插件市场:https://plugins.jetbrains.com/)