Maven打jar包的几种情况

2018/11/12 Maven build

概述

Maven项目打包的时候会有需要需求,比如打包时引入资源文件。比如打成可执行文件。指定编译的版本等等

SpringBoot打包

SpringBoot 有专门的插件 一般使用这个就可以了

<!-- 注意 jar 也可以是 war-->
<packaging>jar</packaging>
<build>
		<plugins>
      <plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
		</plugins>
</build>

一般项目打包

<build>
        <plugins>
            <!-- 编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerVersion>1.7</compilerVersion>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
          <plugins>

          <!-- 资源文件打包时事负放进去 -->
          <resources>
           <resource>
                <!-- 文件夹 -->
               <directory>src/main/java</directory>
               <!-- 包含文件的类型 -->
               <includes>
                   <include>**/*.properties</include>
                   <include>**/*.xml</include>
                   <include>**/*.txt</include>
               </includes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**/*.properties</include>
                   <include>**/*.xml</include>
                   <include>**/*.txt</include>
               </includes>
           </resource>
       </resources>
<build>

打可执行包

  <build>
  <plugins>
    <plugin>
              <!-- 包含所有依赖文件放在一个包里 -->
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                  <descriptorRefs>
                      <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                  <archive>
                      <manifest>
                          <mainClass>xxx.xxx.mainClass</mainClass>
                      </manifest>
                  </archive>
              </configuration>
              <executions>
                  <execution>
                      <id>make-assembly</id>
                      <phase>package</phase>
                      <goals>
                          <goal>single</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
    </plugins>
  <build>

其他

# 导出所有以来的jar到 ./target/dependency_lib 文件夹下
dependency:copy-dependencies -DoutputDirectory=./target/dependency_lib

Search

    Table of Contents