maven-打包的几种方式笔记

  1. 指定打的jar包使用的jdk版本
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <plugin>
    <!-- MAVEN 编译使用的JDK版本 -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    <encoding>UTF-8</encoding>
    </configuration>
    </plugin>
  1. 将依赖打入到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
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.5</version>
    <configuration>
    <archive>
    <manifest>
    <mainClass>cn.mastercom.locspark.LocFill</mainClass>
    </manifest>
    </archive>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
  2. 将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
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
    <archive>
    <manifest>
    <addClasspath>true</addClasspath>
    <classpathPrefix>lib/</classpathPrefix>
    <mainClass>cn.mastercom.locspark.LocFill</mainClass>
    </manifest>
    </archive>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
    <execution>
    <id>copy</id>
    <phase>install</phase>
    <goals>
    <goal>copy-dependencies</goal>
    </goals>
    <configuration>
    <outputDirectory>${project.build.directory}/lib</outputDirectory>
    </configuration>
    </execution>
    </executions>
    </plugin>
  3. 部分依赖打入到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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.1</version>
    <executions>
    <execution>
    <phase>package</phase>
    <goals>
    <goal>shade</goal>
    </goals>
    <configuration>
    <!-- 自动将所有不使用的类全部排除掉 -->
    <minimizeJar>false</minimizeJar>
    <transformers>
    <transformer
    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <mainClass>cn.mastercom.locspark.LocFill</mainClass>
    </transformer>
    </transformers>
    <artifactSet>
    <includes>
    <!-- 包括哪些类,自定义 -->
    <include>xxx.xxx.xx*</include>
    </includes>
    </artifactSet>
    <filters>
    <filter>
    <artifact>*:*</artifact>
    <!-- 排除哪些类,自定义 -->
    <excludes>
    <!-- 排除naive.io -->
    <exclude>org/**</exclude>
    <!-- 排除不需要的配置 自定义 -->
    <exclude>remote/**</exclude>
    <exclude>local/**</exclude>
    <exclude>META-INF/*.SF</exclude>
    <exclude>META-INF/*.DSA</exclude>
    <exclude>META-INF/*.RSA</exclude>
    </excludes>
    </filter>
    </filters>
    </configuration>
    </execution>
    </executions>
    </plugin>