项目打包,满足以下要求:
assembly翻译过来就是组装、装配的意思
Maven对项目打包常用的打包插件有三种,分别是:
插件 | 功能 |
---|---|
maven-jar-plugin | maven 默认打包插件,用来创建 project jar |
maven-shade-plugin | 打可执行包,executable(fat) jar |
maven-assembly-plugin | 支持自定义打包方式 |
这里使用maven-jar-plugin和maven-assembly-plugin
项目目录:
每次找jar包之前先clean一下,不然的话IDEA会认为你的项目没有修改而不重新加载
另:配置文件的注释已经很详细了,这里就不另外再说明了
主要配置如下:
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>2.4version>
<configuration>
<archive>
<addMavenDescriptor>falseaddMavenDescriptor>
<manifest>
<addClasspath>trueaddClasspath>
<classpathPrefix>lib/classpathPrefix>
manifest>
archive>
<excludes>
<exclude>**/assembly/exclude>
excludes>
configuration>
plugin>
完整配置见底部
pom.xml下的主要配置如下:
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>2.4version>
<configuration>
<descriptors>
<descriptor>src/main/resources/assembly/package.xmldescriptor>
descriptors>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
assembly插件的配置文件package.xml见底部
双击执行mvn:package会生成两个包:可执行jar包和项目压缩包,因为assembly的装配配置的是绑定到这上面来的
双击执行assembly:single只生成项目压缩包
解压缩生成的项目包
TestString的源码:
public class TestString {
public static void main(String[] args) {
String[] arr = new String[]{"aaa", "bbb", "ccc", "DDD", "EEE", "FFF"};
System.out.println(StringUtils.join(arr, "---"));
}
}
TestNumber的源码:
public class TestNumber {
public static void main(String[] args) {
Integer[] arr = new Integer[]{11, 22, 33, 44, 55, 66};
System.out.println(StringUtils.join(arr, "---"));
}
}
命令行运行生成的jar
java -classpath dong.jar com.dong.bigdata.TestString
java -classpath dong.jar com.dong.bigdata.TestNumber
包含两个文件:
pom.xml整体的配置
package.xml包含在pom.xml中,用于指定assembly装配时的配置
pom.xml文件:
<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.0modelVersion>
<groupId>com.donggroupId>
<artifactId>bigdataartifactId>
<packaging>jarpackaging>
<version>1.0-SNAPSHOTversion>
<name>bigdataname>
<url>http://http://www.dong.com/.comurl>
<properties>
<project.script.execute.directory>src/main/scripts/executeproject.script.execute.directory>
<project.document.directory>documentproject.document.directory>
<project.config.directory>src/main/resourcesproject.config.directory>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<repositories>
<repository>
<id>aliyunid>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
repository>
<repository>
<id>repo2id>
<url>http://repo2.maven.org/maven2/url>
repository>
<repository>
<id>clouderaid>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/url>
repository>
<repository>
<id>scala-tools.orgid>
<name>Scala-Tools Maven2 Repositoryname>
<url>http://scala-tools.org/repo-releasesurl>
repository>
repositories>
<dependencies>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.4version>
dependency>
dependencies>
<build>
<finalName>dongfinalName>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>2.4version>
<configuration>
<archive>
<addMavenDescriptor>falseaddMavenDescriptor>
<manifest>
<addClasspath>trueaddClasspath>
<classpathPrefix>lib/classpathPrefix>
manifest>
archive>
<excludes>
<exclude>**/assembly/exclude>
excludes>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>2.4version>
<configuration>
<descriptors>
<descriptor>src/main/resources/assembly/package.xmldescriptor>
descriptors>
configuration>
<executions>
<execution>
<id>make-assemblyid>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
<assembly>
<id>fullid>
<formats>
<format>zipformat>
formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>falseuseProjectArtifact>
<outputDirectory>liboutputDirectory>
dependencySet>
dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.directory}directory>
<outputDirectory>outputDirectory>
<includes>
<include>*.jarinclude>
includes>
fileSet>
<fileSet>
<directoryl>${projet.document.directory}directoryl>
<outputDirectory>outputDirectory>
<includes>
<include>readme.*include>
includes>
fileSet>
<fileSet>
<directory>${project.document.directory}directory>
<outputDirectory>documentoutputDirectory>
<excludes>
<exclude>readme.*exclude>
excludes>
fileSet>
<fileSet>
<directory>${project.script.execute.directory}directory>
<outputDirectory>outputDirectory>
<includes>
<include>*include>
includes>
fileSet>
fileSets>
assembly>