详解springboot项目带Tomcat和不带Tomcat的两种打包方式

1,带Tomcat的打包方式

1.1, 在pom.xml文件添加以下配置(目的:自定main入口和跳过Junit代码)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<build>

  <plugins>

    <!--打包为jar时指定main入口-->

    <plugin>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-maven-plugin</artifactId>

      <!--这里写上main方法所在类的路径-->

      <configuration>

        <mainClass>com.sh.tool.YnToolApplication</mainClass>

      </configuration>

      <executions>

        <execution>

          <goals>

            <goal>repackage</goal>

          </goals>

        </execution>

      </executions>

    </plugin>

    <!--忽略Junit代码-->

    <plugin>

      <groupId>org.apache.maven.plugins</groupId>

      <artifactId>maven-surefire-plugin</artifactId>

      <version>2.4.2</version>

      <configuration>

        <skipTests>true</skipTests>

      </configuration>

    </plugin>

  </plugins>

</build>

1.2,在命令行窗口输入命令 :

1

mvn clean package

1.3,结果会在项目的target路径下生成一个.jar文件,将.jar文件复制到任意路径,打开命令窗口进入.jar所在路径,执行以下命令

1

java -jar <.jar文件名>

执行成功之后就可以访问controller接口了。

2,去除Tomcat的打包方式

2.1,配置pom.xml文件,加上如下配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<packaging>war</packaging><!--将packaging指定为war-->

 

<!--移除内嵌的Tomcat-->

<dependency>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-tomcat</artifactId>

  <scope>provided</scope>

</dependency>

 

<build>

  <plugins>

    <!--忽略Junit代码-->

    <plugin>

      <groupId>org.apache.maven.plugins</groupId>

      <artifactId>maven-surefire-plugin</artifactId>

      <version>2.4.2</version>

      <configuration>

        <skipTests>true</skipTests>

      </configuration>

    </plugin>

  </plugins>

</build>

PS:网上有些说要加以下配置,但是我测试不加也是可以的

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<build>

  <plugins>

      <!-- maven打包的时候告诉maven不需要web.xml,否刚会报找不到web.xml错误 -->

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-war-plugin</artifactId>

        <version>2.6</version>

        <configuration>

          <failOnMissingWebXml>false</failOnMissingWebXml>

        </configuration>

      </plugin>

      <!--指定jdk版本-->

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-compiler-plugin</artifactId>

        <version>3.1</version>

        <configuration>

          <source>1.7</source>

          <target>1.7</target>

        </configuration>

      </plugin>

  </plugins>

</build>

2.2,修改启动类为如下

1

2

3

4

5

6

7

8

9

10

11

12

@SpringBootApplication

public class YnToolApplication extends SpringBootServletInitializer {

 

  public static void main(String[] args) {

    SpringApplication.run(YnToolApplication.class, args);

  }

 

  @Override

  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

    return builder.sources(YnToolApplication.class);

  }

}

 2.3,执行以下命令打包,结果将在target目录下生成.war文件

1

mvn clean package

上一篇:Maven有哪些优点和缺点


下一篇:SpringCloud中Eureka注册服务的info信息无法用$动态显示artifactId和version