spring boot 两种创建方式和两种导包方式

描述

  • spring boot是javaWeb框架中最流行的企业级框架,它以spring为主体maven构建项目,简化项目构建过程,去除繁重的xml配置,内嵌tomcat等容器,支持jar包直接启动。

构建项目

  • 创建maven项目,创建过程不再赘述,pom.xml设置springboot相关配置。方式有两种parent和dependency,一个maven项目只能有一个parent,被其他占用时,使用dependency。

parent方式

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>  <!-- 包含自动配置、日志和YAML -->
  <version>2.3.1.RELEASE</version>
</parent>

dependency方式

  • 与parent同级设置。
<dependencyManagement>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.3.1.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
      </dependency>
  </dependencies>
</dependencyManagement>
  • 其他功能jar设置
<dependencies>
  	<!-- 构建web项目模块 包括了Tomcat和spring-webmvc -->
	<!-- spring-boot-starter-web 默认依赖了tomcat的starter 所以使得项目可以直接运行而不需要部署到tomcat中-->
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	
  	<!-- 测试模块,包含JUnit、Hamcrest、Mockito -->
  	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-test</artifactId>
	    <scope>test</scope>
	</dependency>
	
	<!-- thymeleaf 模板引擎 -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	
	<!-- freemarker 模板引擎
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-freemarker</artifactId>
	</dependency>
	 -->
	
	<!--热部署模块 -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-devtools</artifactId>
	    <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
	</dependency>
	<!-- mysql连接 -->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	</dependency>
</dependencies>

导包

  • 导包有两种
    • 一是:自身代码和引用jar导入一个jar包,好处是启动方便,java -jar 包名启动即可。缺点每次更新都要上传一个几十M的大jar包。
    • 二是:自身代码和引用jar分开,好处代码更新只上传自身代码包,依赖包不必重复上传,缺点启动通过-cp引入所有包,相对复杂。
  • 基本设置,配置spring boot插件,设置打包跨过test目录代码。
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
</plugin>

<!-- 不打包test -->  		
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

导出独立包

  • 以上配置,直接运行run --> maven install会在项目根目录target目录下打包独立包。

不包含引用导出

  • 增加build导出编写的代码逻辑,不包含引用jar
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>***.***.Application</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <fork>true</fork>    <!-- hot deploy  -->
        <includes>   <!-- exclude third part jar files -->
            <include>
            <groupId>nothing</groupId>
            <artifactId>nothing</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
            <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • 导出独立包,但部署时需要的依赖包,打包时也一起导出到target下lib目录。
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
            <goal>copy-dependencies</goal>
            </goals>
            <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>false</overWriteSnapshots>
            <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

同时导出独立包和不包含引用包

  • 有时即需要导出独立包,又需要不包含引用包,可以在上面配置基础上增加如下配置,target目录下会生成一个包含dependencies关键字的独立包。
<!--同时打包包含引用jar的结果包  -->
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>-dependencies</descriptorRef> <!--独立包关键字配置  -->
        </descriptorRefs>
        <archive>
            <manifest>
            <mainClass></mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
            <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

猜你喜欢

转载自blog.csdn.net/qq_22973811/article/details/111355456