maven 使用

[toc]

命令

1
2
3
4
5
6
7
8
9
10
11
12
13
# 清理
$ mvn clean

# 打包
$ mvn package

# 依赖树
$ mvn dependency:tree

# 指定 pom
$ mvn --settings YourOwnSettings.xml package
# OR
$ mvn -s YourOwnSettings.xml package

创建项目

1
$ mvn archetype:generate

打包

1
$ mvn package

指定模块

1
$ mvn clean package -pl <group-id>:<artifact-id> -am

指定 pom 文件

1
$ mvn package -f /path/to/pom.xml

可执行jar

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
29
30
31
32
33
34
35
<build>
<resources>
<resource>
<directory>
${project.basedir}/src/main/resources
</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>top.szhkai.mitest.TestZKFacade</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

搜索依赖包

1
$ mvn dependency:tree | grep recommend-service-common

指定编译版本

1
2
3
4
5
6
7
8
9
10
11
12
<plugins>  
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
...
</plugins>

配置多源文件

resources

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
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>

<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>

<resource>
<directory>src/main/generated</directory>
<includes>
<include>**/*.java</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>

sourceDirectory

1
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>

maven-compiler-plugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
<sourceDirectory>.</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>src/main/java/**/*.java</include>
<include>src/main/scala/**/*.scala</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

build-helper-maven-plugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

添加本地依赖

如果有需要添加一个 jar 包到工程,且没有可用的如 nexus 的包管理服务,可以参考这里

首先在工程创建一个目录 libs

1
$ mkdir libs

然后,安装 jar 包到 libs

1
2
3
4
5
6
7
$ mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=<path-to-jar> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=jar \
-DlocalRepositoryPath=libs

然后,修改 pom。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<repositories>
<repository>
<id>project-repo</id>
<url>file://${project.basedir}/libs</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-kudu_${scala.version}</artifactId>
<version>1.2-SNAPSHORT</version>
</dependency>
</dependencies>