说明
maven是一个非常好用的东西,在我们日常开发中依赖jar就不在这里说了,我们主要说的是使用maven打包的问题。我们可能会碰到不一样的打包方式,,这里我们主要使用的三个Maven打包插件maven-dependency-plugin(依赖包打包) maven-jar-plugin(项目打包)maven-resources-plugin(配置文件打包)
maven 的内置变量
1. ${basedir} 项目根目录
2. ${project.build.directory} 构建目录,缺省为target
3. ${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes
4. ${project.build.finalName} 产出物名称,缺省为${project.artifactId}-${project.version}
5. ${project.packaging} 打包类型,缺省为jar
6. ${project.xxx} 当前pom文件的任意节点的内容
maven-dependency-plugin (依赖包打包)
直接上代码吧
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!--可以配置多个execution,只要id不重即可-->
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<!--goals有多个方式,copy,unpack,copy-dependencies等 具体可以去https://maven.apache.org/plugins/maven-dependency-plugin/usage.html这里看-->
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!--指定打的依赖包,只需要ArtifactIds-->
<includeArtifactIds>junit,slf4j-log4j12</includeArtifactIds>
<!--指定输出目录,${project.build.directory} 可以在pom.xml中配置自己的默认路径-->
<outputDirectory>${project.build.directory}/compile/lib</outputDirectory>
<!-- 如果需要打包所有的依赖包 请使用以下代码 替换<includeArtifactIds>junit,slf4j-log4j12</includeArtifactIds>这一行即可
<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
这里表示只打包scope为runtime 的包
<includeScope>runtime</includeScope>
-->
</configuration>
</execution>
</executions>
</plugin>
官网地址:https://maven.apache.org/plugins/maven-dependency-plugin/index.html
maven-jar-plugin(项目打包)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 指定打包的jar包输出路径-->
<outputDirectory>
${project.build.directory}/compile/lib
</outputDirectory>
<!--不打入jar包的文件类型或者路径-->
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<!--表明项目主方法-->
<archive>
<manifest>
<mainClass>com.xing.MapReduce.WordCount.WordCountDriver</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
官网地址:http://maven.apache.org/plugins/maven-jar-plugin/
maven-resources-plugin(配置文件打包)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<!--复制到的路径-目的路径-->
<outputDirectory>
${project.build.directory}/compile/conf
</outputDirectory>
<resources>
<resource>
<!--配置文件所在的路径-->
<directory>${basedir}/conf</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
整个pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--${project.build.directory}-->
<groupId>com.xing</groupId>
<artifactId>MapReduce</artifactId>
<version>1.0-SNAPSHOT</version>
<!--指定编码格式-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>
D:\Program Files\Java\jdk1.8.0_162\bin\javac.exe
</executable>
</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>
<includeArtifactIds>junit,slf4j-log4j12</includeArtifactIds>
<!--${project.build.directory} class的输出目录不做设置的话默认代表项目根目录的target目录;也可以使用“自定义文件夹/自定义文件夹 例如:a/b”--> <outputDirectory>${project.build.directory}/lib</outputDirectory>
<outputDirectory>${project.build.directory}/compile/lib</outputDirectory>
<!--<excludeTransitive>false</excludeTransitive>
<stripVersion>false</stripVersion>
<includeScope>runtime</includeScope>-->
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 指定打包的jar包输出路径-->
<outputDirectory>
${project.build.directory}/compile/lib
</outputDirectory>
<!--不打入jar包的文件类型或者路径-->
<!--<includes>
<include>log4j.properties</include>
</includes>-->
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<mainClass>com.xing.MapReduce.WordCount.WordCountDriver</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<!--打成jar包后复制到的路径-->
<outputDirectory>
${project.build.directory}/compile/conf
</outputDirectory>
<resources>
<resource>
<!--项目中的路径-->
<directory>${basedir}/conf</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
项目截图: