0%

maven

1. 安装

wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz

2. pom结构

  • groupId
  • artifactId
  • version
  • package
    jar | war | pom
  • dependencies
    • groupId
    • artifactId
    • version
    • scope
      • compile: 编译,测试,运行都有效, 默认范围
      • provided: 在编译和测试时有效
      • runtime: 在测试和运行时有效
      • test: 只在测试时有效
      • system: 在编译和测试时有效, 与本机系统关联, 可移植性差
    • optional
      true | false , 是否排除依赖.
      例如, 如果ProjectA中配置依赖ProjectB, 并且optional:true, 那当ProjectX依赖ProjectA时, 如果没有显示的引入ProjectB, 则ProjectX不会引入ProjectB, 即ProjectX可以自己选择是否依赖ProjectB.

3. 添加maven镜像

在settings.xml的mirrors节点下添加

1
2
3
4
5
6
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

或者在pom.xml的profiles/profile节点添加

1
2
3
4
5
6
7
8
9
10
11
12
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<layout>default</layout>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>

4. 上传jar文件到私服

1
mvn deploy:deploy-file -DgroupId=com.google.code.kaptcha -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar -Dfile=/home/raven/Workspace/TomatoDocs/kaptcha-2.3.jar -Durl=http://10.18.75.202:8081/nexus/content/repositories/snapshots/ -DrepositoryId=snapshots

– DgroupId和DartifactId构成了该jar包在pom.xml的坐标, 对应依赖的DgroupId和DartifactId
– Dfile表示需要上传的jar包的绝对路径
– Durl私服上仓库的url精确地址(打开nexus左侧repositories菜单, 可以看到该路径)
– DrepositoryId服务器的表示id, 在nexus的configuration可以看到

5. 常用插件

5.1. Project Build Plugins

GroupId ArtifactId Version
com.mycila license-maven-plugin 3.0
net.revelc.code.formatter formatter-maven-plugin 2.0.1
org.apache.maven.plugins maven-antrun-plugin 1.8
org.apache.maven.plugins maven-assembly-plugin 3.0.0
org.apache.maven.plugins maven-clean-plugin 3.0.0
org.apache.maven.plugins maven-compiler-plugin 3.6.1
org.apache.maven.plugins maven-dependency-plugin 3.0.1
org.apache.maven.plugins maven-deploy-plugin 2.8.2
org.apache.maven.plugins maven-install-plugin 2.5.2
org.apache.maven.plugins maven-javadoc-plugin 2.10.4
org.apache.maven.plugins maven-pdf-plugin 1.3
org.apache.maven.plugins maven-release-plugin 2.5.3
org.apache.maven.plugins maven-resources-plugin 3.0.2
org.apache.maven.plugins maven-shade-plugin 3.0.0
org.apache.maven.plugins maven-site-plugin 3.6
org.apache.maven.plugins maven-surefire-plugin 2.20
org.codehaus.mojo versions-maven-plugin 2.4
org.eluder.coveralls coveralls-maven-plugin 4.3.0
org.jacoco http://jacoco-maven-plugin 0.7.9

5.2. Project Report Plugins

GroupId ArtifactId Version
org.apache.maven.plugins maven-changes-plugin 2.12.1
org.apache.maven.plugins maven-checkstyle-plugin 2.17
org.apache.maven.plugins maven-javadoc-plugin 2.10.4
org.apache.maven.plugins maven-jxr-plugin 2.5
org.apache.maven.plugins maven-pmd-plugin 3.8
org.apache.maven.plugins maven-project-info-reports-plugin 2.9
org.apache.maven.plugins maven-surefire-report-plugin 2.20
org.codehaus.mojo clirr-maven-plugin 2.8
org.codehaus.mojo findbugs-maven-plugin 3.0.4
org.codehaus.mojo jdepend-maven-plugin 2.0
org.codehaus.mojo taglist-maven-plugin 2.4
org.codehaus.mojo versions-maven-plugin 2.4
org.jacoco http://jacoco-maven-plugin 0.7.9

6. 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# -f 指定 mvn 命令工作目录, 在该目录下必须要有 pom.xml 文件
mvn clean install -f <pom-dir>

# -pl 单独编译哪些模块, 可以用逗号隔开
# -am 同时编译 `-pl 列举的模块依赖的模块`
# -amd 同时编译`依赖 -pl 列举的模块`
# -N 不递归, 常用在只构建父 pom
mvn clean install -pl .,<sub-project> -am

# 查看依赖树
mvn dependency:tree

# 依赖分析, 删除无用的依赖
# http://maven.apache.org/components/plugins/maven-dependency-plugin/
mvn dependency:analyze

7. 常用配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>project.build.sourceEncoding</encoding>
</configuration>
</plugin>
</plugins>
</build>

8. 常见问题

8.1. 带 @SpringApplication 的模块打包后, 在别的项目中引入时, 无法找到模块中的类

在jar包中看到, 编译文件在 BOOT-INF 目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier> <!--加上这个就可以, 但是会变成无法执行-->
<mainClass>${mainClass}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>