Maven Build of NodeJS Module#

Mit dem Plugin "frontend-maven-plugin" kann ein WAR-File gebaut werden:
<?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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.intersult</groupId>
	<artifactId>reactjs-test</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>
    <name>${project.artifactId}</name>
	<description>ReactJS Test</description>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	
	<build>
		<finalName>${project.artifactId}</finalName>
		<plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.12.1</version>
                <executions>
                    <execution>
                        <id>install-node-and-npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>npm-install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>npm-build</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run build</arguments>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <nodeVersion>v18.8.0</nodeVersion>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webResources>
                        <resource>
                            <directory>build</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
		</plugins>
	</build>
</project>

Jenkins Build mit NodeJS#

Jenkins kann auch native NodeJS Befehle ausführen. Das Plugin NodeJS unterstützt sowohl Standard-Builds als auch die Build-Pipeline.
  1. Zunächst unter Jenkins verwalten -> Plugins verwalten gehen

nodejs/jenkins verwalten.png

  1. Dort kann das NodeJS Plugin installiert werden

nodejs/nodejs plugin.png

  1. Dann zurück auf Jenkins verwalten -> Konfiguration der Hilfsprogramme. Dort findet man (relativ weit unten) NodeJS Installationen und wiederum einen Knopf NodeJS hinzufügen. Dieser Knopf klappt die Sektion zur Installation von NodeJS und NPM auf. Darin eine entsprechende Konfiguration vornehmen

nodejs/nodejs installationen.png

Hinweis: Nach dem Speichern der Konfiguration scheint erst einmal gar nichts passiert zu sein. Die Installation von NodeJS und NPM ist jedoch Bestandteil eines entsprechenden Build-Prozesses und findet später an dieser Stelle statt.

Jenkins Standard Build#

Nach der oben genannten Installation kann NodeJS und NPM nun im Build-Prozess verwendet werden. Es ist zu beanten, dass der Haken bei "Provide Node & npm bin/ folder to PATH" gesetzt wird.

nodejs/jenkins build standard.png

Jenkins Pipeline Build#

NodeJS und NPM kann auch bei Pipeline-Builds verwendet werden. Das Plugin stellt dort den zusätzlichen Befehl "nodejs" zur Verfügung

nodejs/jenkins build pipeline.png

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                script {
                    currentRevision = checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '',
                        excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false,
                        ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true,
                        credentialsId: 'dynarocks.com', depthOption: 'infinity', ignoreExternalsOption: true, local: '.',
                        remote: 'https://dynarocks.com/svn/com.intersult/trunk/com.intersult/internal/work/reactts-test']],
                        quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
                }
            }
        }
        stage('Install') {
            steps {
                nodejs(nodeJSInstallationName: 'NodeJS') {
                    sh 'npm install'
                }
            }
        }
        stage('Generate Sources') {
            steps {
                nodejs(nodeJSInstallationName: 'NodeJS') {
                    sh 'npm run generate'
                }
            }
        }
        stage('Build') {
            steps {
                nodejs(nodeJSInstallationName: 'NodeJS') {
                    sh 'npm run build'
                }
            }
        }
    }
}