Maven
Install Maven
Ubuntu
sudo apt install maven
Manually
- 下载Binary tar.gz archive文件
- 安装
mkdir ~/.java
cd ~/.java
tar zxvf apache-maven-3.5.4-bin.tar.gz
vi ~/.bash_profile
export MAVEN_HOME=/Users/yizhenchen/.java/apache-maven-3.5.4
export M2_HOME=$MAVEN_HOME
export PATH=$PATH:$MAVEN_HOME/bin
source ~/.bash_profile
- 检查是否安装成功
mvn -v
echo $M2_HOME
首次运行完mvn后会在"~“目录下创建”.m2"文件夹,作为本地仓库
配置Maven
修改配置文件
- $MAVEN_HOME/conf/settings.xml
- ~/.m2/settings.xml
使用Maven
在项目中添加pom.xml文件
- groupId: 一般为package名
- artifactId: 一般为项目名
- version: 版本
目录结构:
- Project
- pom.xml
- src
- main
- java
- resources
- test
- java
- resources
- main
- target
<groupId>here.there</groupId>
<artifactId>MavenTest</artifactId>
<version>1.0</version>
命令
- clean:清空target目录
- complie:编译工程
- package:执行打包
- install:发布到仓库
运行
mvn exec:java -Dexec.mainClass="package.Main"
# 避免 `IllegalThreadStateException`
mvn exec:java -Dexec.mainClass="package.Main" -Dexec.cleanupDaemonThreads=false
依赖关系
- compile: 编译时需要用到该jar包(默认)
- test: 编译Test时需要用到该jar包
- runtime: 编译时不需要,但运行时需要用到
- provided: 编译时需要用到,但运行时由JDK或某个服务器提供
SNAPSHOT
SNAPSHOT结尾的版本号会被Maven视为开发版本,开发版本每次都会重复下载。
镜像仓库
添加镜像仓库
<!-- 阿里云仓库 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<!-- 中央仓库1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央仓库2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
搜索第三方组件
Jar 打包
主要有三个plugins可以提供打包jar的功能,jar-plugin
/shade-plugin
/assembly-plugin
,具体区别见以下链接:
Difference between maven plugins ( assembly-plugins , jar-plugins , shaded-plugins)
maven-jar-plugin
使用 maven-jar-plugin
插件打包。不会复制dependency到目标文件夹,因此需要在manifest中指定classpath路径
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 在manifest中添加classpath -->
<addClasspath>true</addClasspath>
<!-- 指定classpath路径 -->
<classpathPrefix>/Users/yizhenchen/.m2/repository/</classpathPrefix>
<!-- 使用Repository-Style的路径 -->
<classpathLayoutType>repository</classpathLayoutType>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
maven-dependency-plugin
使用maven-dependency-plugin
插件将依赖包复制到 ./target/lib/
中
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
maven-assembly-plugin
适用于较少denpendencies的项目,因为同名文件会有覆盖的问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
maven-shade-plugin
使用 maven-shade-plugin
打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
常用 Dependencies
hive udf dependency
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>0.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
Jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
问题
- Warning:
File encoding has not been set, using platform encoding UTF-8
在pom.xml中加上
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
- Maven中引用了Dependencies,但是在Idea运行时报错找不到依赖
重启IntelliJ Idea,会在External Libraries中下载好包,再运行即可