SpringBoot 应用打包

本贴最后更新于 1778 天前,其中的信息可能已经时移世易

单个 Jar 包

全部依赖集成到一个 jar 包中,且 jar 包可以直接执行。

方式一:parent 依赖

<?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>geektime.demo</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWorld</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <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>
    <plugins>
      <plugin>
	<groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

方式二:dependencyManagement

<?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>geektime.demo</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>HelloWorld</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
	<version>2.1.1.RELEASE</version>
	<type>pom</type>
	<scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <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>
    <plugins>
      <plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<version>2.1.1.RELEASE</version>
	<executions>
	  <execution>
	    <goals>
	      <goal>repackage</goal>
	    </goals>
	  </execution>
	</executions>
      </plugin>
    </plugins>
  </build>

</project>

分目录部署包

jar 包,依赖包,配置文件分目录独立部署,修改配置文件无需重新打包。

appassembler-maven-plugin

pom 中配置

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>appassembler-maven-plugin</artifactId>
    <version>1.10</version>
    <configuration>
        <configurationDirectory>conf</configurationDirectory>  
        <configurationSourceDirectory>src/main/resources</configurationSourceDirectory>  
        <copyConfigurationDirectory>true</copyConfigurationDirectory>  
        <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>  
        <assembleDirectory>${project.build.directory}/app</assembleDirectory>  
        <extraJvmArguments>-Xms128m</extraJvmArguments>  
        <binFileExtensions>  
            <unix>.sh</unix>  
        </binFileExtensions>  
        <platforms>
            <platform>windows</platform>
            <platform>unix</platform>
        </platforms>
        <repositoryName>lib</repositoryName>
        <programs>
            <program>
                <id>app</id>
                <mainClass>com.app.Main</mainClass>
                <jvmSettings>
                    <extraArguments>
                        <extraArgument>-server </extraArgument>
                        <extraArgument>-Xmx2G</extraArgument>
                        <extraArgument>-Xms2G</extraArgument>
                    </extraArguments>
                </jvmSettings>
            </program>
        </programs>
    </configuration>
</plugin>

maven 命令

jsw 生成
<plugin>  
        <groupId>org.codehaus.mojo</groupId>  
        <artifactId>appassembler-maven-plugin</artifactId>  
        <version>1.2.1</version>  
        <configuration>  
            <repositoryLayout>flat</repositoryLayout>  
            <repositoryName>lib</repositoryName>  
            <includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>  
            <copyConfigurationDirectory>src/main/resources</copyConfigurationDirectory>  
            <target>${project.build.directory}</target>  
            <daemons>  
                <daemon>  
                    <id>client</id>  
                    <mainClass>com.demo.client.App</mainClass>  
                    <commandLineArguments>  
                        <commandLineArgument>start</commandLineArgument>  
                    </commandLineArguments>  
                    <platforms>  
                        <platform>jsw</platform>  
                    </platforms>  
                    <generatorConfigurations>  
                        <generatorConfiguration>  
                            <generator>jsw</generator>  
                            <includes>  
                                <include>linux-x86-32</include>  
                                <include>linux-x86-64</include>  
                                <include>windows-x86-32</include>  
                                <include>windows-x86-64</include>  
                            </includes>  
                            <configuration>  
                                <property>  
                                    <name>configuration.directory.in.classpath.first</name>  
                                    <value>etc</value>  
                                </property>  
                                <property>  
                                    <name>set.default.REPO_DIR</name>  
                                    <value>lib</value>  
                                </property>  
                                <property>  
                                    <name>wrapper.logfile</name>  
                                    <value>../logs/wrapper.log</value>  
                                </property>  
                                <property>  
                                    <name>run.as.user.envvar</name>  
                                    <value>johndoe</value>  
                                </property>  
                            </configuration>  
                        </generatorConfiguration>  
                    </generatorConfigurations>  
                    <jvmSettings>  
                        <initialMemorySize>256M</initialMemorySize>  
                        <maxMemorySize>512M</maxMemorySize>  
                        <systemProperties>  
                            <systemProperty>java.security.policy=conf/policy.all</systemProperty>  
                            <systemProperty>com.sun.management.jmxremote</systemProperty>  
                            <systemProperty>com.sun.management.jmxremote.port=8999</systemProperty>  
                            <systemProperty>com.sun.management.jmxremote.authenticate=false</systemProperty>  
                            <systemProperty>com.sun.management.jmxremote.ssl=false</systemProperty>  
                        </systemProperties>  
                        <extraArguments>  
                            <extraArgument>-server</extraArgument>  
                        </extraArguments>  
                    </jvmSettings>  
                </daemon>  
            </daemons>  
        </configuration>  
        <executions>  
            <execution>  
                <id>generate-jsw-scripts</id>  
                <phase>package</phase>  
                <goals>  
                    <goal>generate-daemons</goal>  
                </goals>  
            </execution>  
        </executions>  
    </plugin> 
  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    943 引用 • 1460 回帖 • 3 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...