SHO酱的Blog

SHO酱的Blog

Blade项目打包

2018-06-12

简述

Blade的项目打包是使用Maven的package来打包的。
使用【maven-jar-plugin】将项目中的Java文件编译,并打包到jar文件中。
之后,再使用【maven-assembly-plugin】将项目的jar包和依赖包统一打包的dist文件夹中。

源文件

先上一段源文件看看。

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>

  <groupId>net.xxxx.report</groupId>
  <artifactId>xxxx</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>datawall</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
	.
	.
	.
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <appendAssemblyId>false</appendAssemblyId>
          <descriptors>
            <descriptor>package.xml</descriptor>
          </descriptors>
          <outputDirectory>${project.build.directory}/dist/</outputDirectory>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>net.xxxx.App</mainClass>
              <classpathPrefix>lib/</classpathPrefix>
              <addClasspath>true</addClasspath>
            </manifest>
            <manifestEntries>
              <!-- 在Class-Path下添加配置文件的路径 -->
              <Class-Path>resources/</Class-Path>
            </manifestEntries>
          </archive>
          <excludes>
            <!-- 注意这玩意从编译结果目录开始算目录结构 -->
            <exclude>/app.properties</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
	<pluginManagement>
		<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
		<plugins>
			<plugin>
				<artifactId>maven-clean-plugin</artifactId>
				<version>3.0.0</version>
				</plugin>
				<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<version>3.0.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.20.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<version>3.0.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-install-plugin</artifactId>
				<version>2.5.2</version>
			</plugin>
			<plugin>
				<artifactId>maven-deploy-plugin</artifactId>
				<version>2.8.2</version>
			</plugin>
		</plugins>
    </pluginManagement>
  </build>
</project>


package.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>customAssembly</id>
    <formats>
        <format>dir</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/resources</directory>
            <outputDirectory>/resources</outputDirectory>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <outputDirectory>/lib</outputDirectory>
            <scope>runtime</scope>
            <excludes>
                <exclude>${project.groupId}:${project.artifactId}</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <includes>
                <include>${project.groupId}:${project.artifactId}</include>
            </includes>
        </dependencySet>
    </dependencySets>

</assembly>

注意

其中有几处值得注意的地方,下面一一说明。

排除导入Jar的文件

以下源码中,excludes中就是定义的不导入jar文件中的文件。

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>3.0.2</version>
	<configuration>
		<archive>
			<manifest>
				<mainClass>net.xxxx.App</mainClass>
				<classpathPrefix>lib/</classpathPrefix>
				<addClasspath>true</addClasspath>
			</manifest>
			<manifestEntries>
				<!-- 在Class-Path下添加配置文件的路径 -->
				<Class-Path>resources/</Class-Path>
			</manifestEntries>
		</archive>
		<excludes>
			<!-- 注意这玩意从编译结果目录开始算目录结构 -->
			<exclude>/app.properties</exclude>
		</excludes>
	</configuration>
</plugin>

插件Assembly配置部分

主要配置信息都定义在了“package.xml”文件中,具体看package.xml源文件。

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<configuration>
		<appendAssemblyId>false</appendAssemblyId>
		<descriptors>
			<descriptor>package.xml</descriptor>
		</descriptors>
		<outputDirectory>${project.build.directory}/dist/</outputDirectory>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

执行Maven的导致命令

项目使用的 IDE 是 IDEA ,在 IDEA 中点击【Maven Projects】按照如下图操作和填写,之后点击【Execute】,之后就是等待打包完成啦。