Empaquetamiento de Proyectos Spring Boot en IntelliJ IDEA

Empaquetamiento de Proyectos Spring Boot en IntelliJ IDEA

Un: Paquete JAR Ejecutable

Puntos importantes a considerar:

  • El tipo de package de Maven debe ser jar
  • Debe configurarse el plugin spring-boot-maven-plugin

1.1 Archivo pom.xml


<?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>com.ejemplos.spring</groupId>
    <artifactId>demo-springboot</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-springboot</name>
    <description>Proyecto de ejemplo para Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Simplemente ejecute el ciclo de vida ---> package en Maven para generar el archivo JAR ejecutable.

Dos: Generación de Paquete WAR

Puntos importantes a considerar:

  • Excluir el Tomcat incorporado en el web-starter
  • Clase ServletInitializer generada automáticamente por IntelliJ IDEA
  • El tipo de package de Maven debe ser war

2.1 Archivo pom.xml


<?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>com.ejemplos.spring</groupId>
    <artifactId>demo-springboot</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo-springboot</name>
    <description>Proyecto de ejemplo para Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </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>

2.2 Clase ServletInitializer


package com.ejemplos.spring.configuracion;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class InicializadorServlet extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configurar(SpringApplicationBuilder aplicacion) {
        return aplicacion.fuentes(DemoSpringbootApplication.class);
    }

}

Simpleemnte ejecute el ciclo de vida ---> package en Maven para generar el archivo WAR.

Es importante mencionar que algunos tutoriales disponibles en internet pueden contener información incorrecta. Es recomendable verificar y probar cada paso personalmente para asegurar los resultados deseados.

Etiquetas: IntelliJ IDEA Spring Boot empaquetamiento maven JAR

Publicado el 6-1 10:56