Maven Configuration

Commmand

1
2
3
4
5
mvn '-Dtest=com.anda.engine.disruptor.*Test' test

mvn clean install -DskipTests

mvn compile

Dependencies

Exclude

1
2
3
4
5
6
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>

Scope

  • compile:默认选项,编译测试运行都有效。项目在编译,测试,运行阶段都需要这个artifact对应的Jar。
  • provided:在编译和测试时有效。在编译测试阶段,我们需要这个artifact对应的jar包在classpath中,而在运行阶段,假定已经提供了这个jar包,所以无需这个artifact对应的Jar。
  • runtime:在测试和运行时有效。
  • test:仅在测试时有效。
  • system:在编译和测试时有效,与本机系统关联,可移植性差。

Unit Test

Plugin

1
2
3
4
5
6
7
8
9
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
</plugin>

Build

Package Kotlin Project With Jar File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<!-- package jar file -->
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<mainClass>com.anda.engine.ApplicationKt</mainClass>
</manifest>
</archive>
<!-- <descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs> -->
</configuration>
</execution>
</executions>
</plugin>

在pom.xml同级目录下,新建assembly.xml,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>

Including Library for Spring Boot

1
2
3
4
5
6
7
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>

Build Library

  1. 导出的jar没有包含依赖的denpendency,导致其他项目引入的时候报错。

解决方案:添加maven-assembly-plugin插件进行打包。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

此外,需要注意的是,maven-assembly打包出来的是一个可运行的jar,即包含了Kotlin标准库的代码,而实际上需要的仅仅是一个Library。

针对标准库添加provided关键字

1
2
3
4
5
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<scope>provided</scope>
</dependency>

Repository

MacOS

1
2
# 查看maven安装目录
mvn -v

添加Aliyun Repository,sudo vim /usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml

1
2
3
4
5
6
7
8
<mirrors>  
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>