1. 程式人生 > >使用 Maven 外掛將 class(位元組碼檔案),resource(資原始檔),lib(依賴的jar包)分開打包

使用 Maven 外掛將 class(位元組碼檔案),resource(資原始檔),lib(依賴的jar包)分開打包

1. 在pom檔案中對各個外掛進行配置

<?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
> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent
> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>1.0</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</
java.version> <encoding>UTF-8</encoding> <maven-compiler-plugin-version>3.8.0</maven-compiler-plugin-version> <maven-jar-plugin-version>3.1.0</maven-jar-plugin-version> <maven-source-plugin-version>3.0.1</maven-source-plugin-version> <maven-assembly-plugin-version>3.1.0</maven-assembly-plugin-version> <maven-dependency-plugin-version>3.1.0</maven-dependency-plugin-version> <maven-resources-plugin-version>3.1.0</maven-resources-plugin-version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <!-- 生成的專案jar包的名字--> <finalName>demo-findName</finalName> <!--原始碼路徑--> <sourceDirectory>src/main/java</sourceDirectory> <!--maven-resources-plugin 外掛打包resource檔案時會參考此節點的配置--> <resources> <!--resource 子節點可通過配置將制定目錄的檔案在打包後拷貝到制定目錄--> <!-- 把src/main/resources目錄下所有的檔案拷貝到conf目錄中 --> <resource> <directory>src/main/resources</directory> <targetPath>${project.build.directory}/conf</targetPath> </resource> </resources> <plugins> <!--編譯外掛--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin-version}</version> <configuration> <encoding>${encoding}</encoding> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> <!--將專案的原始碼的class檔案打包到一個jar包--> <!--jar包預設在target目錄下--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>${maven-jar-plugin-version}</version> <configuration> <archive> <!-- 生成的jar中,不要包含pom.xml和pom.properties這兩個檔案 --> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <!-- 是否要把第三方jar放到manifest的classpath中 --> <addClasspath>true</addClasspath> <!-- 生成的manifest中classpath的字首,因為要把第三方jar放到lib目錄下,所以classpath的字首是lib/ --> <classpathPrefix>lib/</classpathPrefix> <!-- 應用的main class --> <mainClass>com.example.demo.DemoApplication</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 用於拷貝maven依賴的plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>${maven-dependency-plugin-version}</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <!-- 把依賴的所有maven jar包拷貝到lib目錄中 --> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- 用於拷貝resource的plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>${maven-resources-plugin-version}</version> <configuration> <encoding>${encoding}</encoding> <outputDirectory>${project.build.directory}/conf</outputDirectory> </configuration> </plugin> </plugins> </build> </project>

2. 打包後的 target 目錄結構

  conf:資原始檔目錄

  lib:依賴的jar包目錄

  demo-findName:將class檔案打包生成的jar包

3. 執行專案

  把上述三個目錄檔案放在同一級目錄下,

  執行 java -jar 生成的jar包名字

  即可啟動專案