CREAR PROYECTO MULTIMODULO CON SPRING Y MAVEN

Crearemos un proyecto multimodulo en spring, con los siguientes modulos:
siserp-pos-app: Es el proyecto contenedor de los modulos, tipo POM.
siserp-pos-frontend: Es el modulo que contendrá el código HTML de la aplicación, tipo JAR.
siserp-pos-backend: Es el modulo que contendrá las API REST, tipo WAR.
siserp-pos-: Es el modulo que encapsulara el modulo “siserp-pos-frontend” en un WAR desplegable, tipo WAR.
 
A)       COMPONENTE SISERP-POS-APP
Hacer clic en: File > New > Other …





Contenido del archivo pom.xml
<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.besoft.siserp.pos</groupId>
        <artifactId>siserp-pos-app</artifactId>
        <version>TRUNK</version>
        <packaging>pom</packaging>
        <name>siserp-pos-app</name>
        <description>Siserp pos - Aplicacion</description>
</project>

 B)       COMPONENTE SISER-POS-FRONTEND
 Hacer clic en: File > New > Other …




Contenido del archivo POM.xml
<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>pe.gob.essalud.sigps.cam</groupId>
        <artifactId>siserp-pos-frontend</artifactId>
        <packaging>jar</packaging>
        <name>siserp-pos-frontend</name>
        <description>Siserp pos - frontend</description>
        <version>1.0</version>
        <parent>
                <groupId>com.besoft.siserp.pos</groupId>
                <artifactId>siserp-pos-arq</artifactId>
                
                <relativePath>../siserp-pos-arq</relativePath>
        </parent>
 
        <profiles>
                <profile>
                        <id>Windows</id>
                        <activation>
                               <os>
                                       <family>Windows</family>
                               </os>
                        </activation>
                        <properties>
                               <script.extension>.bat</script.extension>
                        </properties>
                </profile>
                <profile>
                        <id>unix</id>
                        <activation>
                               <os>
                                       <family>unix</family>
                               </os>
                        </activation>
                        <properties>
                               <script.extension>.sh</script.extension>
                        </properties>
                </profile>
        </profiles>
 
        <build>
                <plugins>
                        <plugin>
                               <groupId>org.codehaus.mojo</groupId>
                               <artifactId>exec-maven-plugin</artifactId>
                               <version>1.6.0</version>
                               <executions>
                                       <execution>
                                               <id>npm-build</id>
                                               <phase>generate-sources</phase>
                                               <goals>
                                                       <goal>exec</goal>
                                               </goals>
                                               <configuration>
                                                       <workingDirectory>src/main/siserp-pos-frontend</workingDirectory>
                                                       <executable>${basedir}/src/main/siserp-pos-frontend/npm-build${script.extension}</executable>
                                               </configuration>
                                       </execution>
                               </executions>
                        </plugin>
                </plugins>
                <resources>
                        <resource>
                               <directory>target/siserp-pos-frontend</directory>
                               <targetPath>static</targetPath>
                        </resource>
                </resources>
        </build>
</project>

Agregar un archivo npm-build.bat en la carpeta src/main/siserp-pos-frontend/ con el siguiente código:

* Este archivo es un ejecutable que nos permitirá compilar el código frontend en Angular.
npm install && npm run build


Crear un proyecto angular en el ruta: src/main/
$ ng new siserp-pos-frontend

En el archivo src/main/siserp-pos-frontend/angular.json modificar el outputPath, para imprimir todo lo compilado en la carpeta “target” del proyecto spring

"architect": {
   "build": {
      "builder""@angular-devkit/build-angular:browser",
      "options": {
         "outputPath""../../../target/siserp-pos-frontend",
          //COMPILA EN CARPETA TARGET DE JAVA
         "index""src/index.html",
         "main""src/main.ts",
         "polyfills""src/polyfills.ts",
         "tsConfig""src/tsconfig.app.json",

 En el archivo package.json adicionar el siguiente código
{
  "name""siserp-pos",
  "version""0.0.0",
  "scripts": {
    "ng""ng",
    "start""ng serve -o --base-href /siserp-pos/",//AGREGAR
    "build""ng build",
  },
 
En el archivo index.html, dejar como sigue
<base href="/siserp-pos">

D)        COMPONENTE SISERP-POS-BACKEND
 Hacer clic en: File > New > Other …




Contenido del archivo POM.xml
<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>
        <artifactId>siserp-pos-backend</artifactId>
        <packaging>war</packaging>
        <name>siserp-pos-backend</name>
        <description>Siserp pos - backend</description>
 
        <parent>
                <groupId>com.besoft.siserp.pos</groupId>
                <artifactId>siserp-pos-arq</artifactId>
                <version>1.0</version>
                <relativePath>../siserp-pos-arq</relativePath>
        </parent>
 
        <properties>
                <start-class>com.besoft.siserp.pos.SiserpPosApplication</start-class>
        </properties>
 
        <dependencies>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-devtools</artifactId>
                        <scope>runtime</scope>
                        <optional>true</optional>
                </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-log4j</artifactId>
                        <version>1.3.8.RELEASE</version>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-test</artifactId>
                        <scope>test</scope>
                        <exclusions>
                               <exclusion>
                                       <groupId>org.junit.vintage</groupId>
                                       <artifactId>junit-vintage-engine</artifactId>
                               </exclusion>
                        </exclusions>
                </dependency>
 
                <!-- ADICIONAL DEPENDENCIES -->
                <dependency>
                        <groupId>org.apache.commons</groupId>
                        <artifactId>commons-lang3</artifactId>
                </dependency>
                <dependency>
                        <groupId>com.oracle</groupId>
                        <artifactId>ojdbc6</artifactId>
                        <version>11.2.0.3</version>
                </dependency>
        </dependencies>
 
        <build>
                <finalName>besoft.siserp-pos.ws.1.0</finalName>
                <resources>
                        <resource>
                               <directory>src/main/resources</directory>
                               <filtering>true</filtering>
                        </resource>
                </resources>
 
                <plugins>
                        <plugin>
                               <groupId>org.springframework.boot</groupId>
                               <artifactId>spring-boot-maven-plugin</artifactId>
                               <configuration>
                                       <addResources>true</addResources>
                               </configuration>
                        </plugin>
 
                        <plugin>
                               <groupId>org.apache.maven.plugins</groupId>
                               <artifactId>maven-surefire-plugin</artifactId>
                               <configuration>
                                       <excludes>
                                               <exclude>**/*IntegrationTest.java</exclude>
                                               <exclude>**/*LiveTest.java</exclude>
                                               <exclude>**/*MvcTest.java</exclude>
                                       </excludes>
                               </configuration>
                        </plugin>
 
                        <plugin>
                               <groupId>org.apache.maven.plugins</groupId>
                               <artifactId>maven-resources-plugin</artifactId>
                               <configuration>
                                       <nonFilteredFileExtensions>
                                               <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                                       </nonFilteredFileExtensions>
                               </configuration>
                        </plugin>
                </plugins>
        </build>
 
        <profiles>
                <profile>
                        <id>dev</id>
                        <properties>
                               <spring.profiles.active>dev</spring.profiles.active>
                               <logging.level>INFO</logging.level>
                        </properties>
                        <activation>
                               <activeByDefault>true</activeByDefault>
                        </activation>
                        <dependencies>
                               <dependency>
                                       <groupId>org.springframework.boot</groupId>
                                       <artifactId>spring-boot-starter-web</artifactId>
                               </dependency>
                        </dependencies>
                </profile>
                <profile>
                        <id>qa</id>
                        <properties>
                               <spring.profiles.active>qa</spring.profiles.active>
                               <logging.level>INFO</logging.level>
                        </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>
                        </dependencies>
                </profile>
        </profiles>
</project>

Agregar el archivo src/main/resources/application.properties con el siguiente contenido
spring.datasource.url = jdbc:oracle:thin:@192.168.43.15:1521:orcl
spring.datasource.username=pos
spring.datasource.password=1234
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
 
# HikariCP settings
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maximum-pool-size=5
 
Crear el paquete com.besoft.siserp.pos y crear la clase SiserpPosApplication.java
package com.besoft.siserp.pos;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SiserpPosApplication {
 
        public static void main(String[] args) {
                SpringApplication.run(SiserpPosApplication.class, args);
        }
}
 
Crear la clase ServletInitializer.java
 
package com.besoft.siserp.pos;
 
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 
public class ServletInitializer extends SpringBootServletInitializer {
 
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(SiserpPosApplication.class);
        }
 
}
 
Crear el sub paquete com.besoft.siserp.pos.controller y la clase HomeController.class
package com.besoft.siserp.pos.controller;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/home")
public class HomeController {
 
        @GetMapping("/hola")
        public String hola() {
                return "HOLA MUNDO";
        }
}
 
E)       COMPONENTE SISERP-POS
Hacer clic en: File > New > Other …





Contenido del archivo pom.xml
<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>
        <artifactId>siserp-pos</artifactId>
        <packaging>war</packaging>
        <name>siserp-pos</name>
        <description>Sierp pos - frontend desplegable</description>
 
        <parent>
                <groupId>com.besoft.siserp.pos</groupId>
                <artifactId>siserp-pos-arq</artifactId>
                <version>1.0</version>
                <relativePath>../siserp-pos-arq</relativePath>
        </parent>
 
        <properties>
                <start-class>com.besoft.besoft.pos.SiserpPosApplication</start-class>
        </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-log4j</artifactId>
                        <version>1.3.8.RELEASE</version>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-devtools</artifactId>
                        <scope>runtime</scope>
                        <optional>true</optional>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-test</artifactId>
                        <scope>test</scope>
                        <exclusions>
                               <exclusion>
                                       <groupId>org.junit.vintage</groupId>
                                       <artifactId>junit-vintage-engine</artifactId>
                               </exclusion>
                        </exclusions>
                </dependency>
                <!-- We need to include the javax.servlet API specs, the implementation
                        will be provided by Wildfly / JBoss -->
                <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>javax.servlet-api</artifactId>
                        <scope>provided</scope>
                </dependency>
                <!-- Dependencia de frontend con codigo HTML -->
                <dependency>
                        <groupId>com.besoft.siserp.pos</groupId>
                        <artifactId>siserp-pos-frontend</artifactId>
                        <version>1.0</version>
                        <scope>runtime</scope>
                </dependency>
        </dependencies>
 
        <build>
                <finalName>besoft.siserp-pos.web.1.0</finalName>
                <resources>
                        <resource>
                               <directory>src/main/resources</directory>
                               <filtering>true</filtering>
                        </resource>
                </resources>
                <plugins>
                        <plugin>
                               <groupId>org.springframework.boot</groupId>
                               <artifactId>spring-boot-maven-plugin</artifactId>
                               <configuration>
                                       <addResources>true</addResources>
                               </configuration>
                        </plugin>
                </plugins>
        </build>
</project>

Crear el paquete com.besoft.siserp.pos y crear la clase SiserpPosApplication.java
package com.besoft.siserp.pos;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SiserpPosApplication {
 
        public static void main(String[] args) {
                SpringApplication.run(SiserpPosApplication.class, args);
        }
}
 
Crear la clase ServletInitializer.java
package com.besoft.siserp.pos;
 
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 
public class ServletInitializer extends SpringBootServletInitializer {
 
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(SiserpPosApplication.class);
        }
}