Ein eigenes Maven-Plugin in 15 Minuten inklusive Dokumentation erstellen

Ein eigenes Maven-Plugin zu erstellen ist nicht schwer und ist schnell gemacht. Wir wollen ein Plugin welches einen Verzeichnis Inhalt mit ls -la in das Log schreibt erstellen. Es können aber auch andere Linux Befehle übergeben werden. Hier nun die 6 Schritte mit den Vorlagen die mind. nötig sind.
Freut euch immerAlways rejoice, mit freundlicher Genehmigung meines Lieblingskünstlers

1. Wir legen in Eclipse ein neue Maven Projekt mit Java 1.8 an. mit Artifact Id und Group Id de.wenzlaff.command.maven.plugin

Ein eigenes Maven-Plugin in 15 Minuten inklusive Dokumentation erstellen

2. In der pom.xml fügen wir die folgenden Abhängikeiten hinzu:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>de.wenzlaff.command.maven.plugin</groupId>
	<artifactId>de.wenzlaff.command.maven.plugin</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>maven-plugin</packaging>

	<name>Command Maven Plugin</name>
	<description>Command Maven Plugin</description>

	<organization>
		<name>Thomas Wenzlaff</name>
		<url>https://www.wenzlaff.info</url>
	</organization>

	<prerequisites>
		<maven>3.5.0</maven>
	</prerequisites>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	</properties>

	<reporting>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-plugin-plugin</artifactId>
				<reportSets>
					<reportSet>
						<reports>
							<report>report</report>
						</reports>
					</reportSet>
				</reportSets>
			</plugin>
		</plugins>
	</reporting>

	<dependencies>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>3.6.3</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-core</artifactId>
			<version>3.6.0</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>3.5</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-plugin-plugin</artifactId>
					<version>3.6.0</version>
				</plugin>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-site-plugin</artifactId>
					<version>3.8.2</version>
				</plugin>
				<!--This plugin's configuration is used to store Eclipse m2e settings 
					only. It has no influence on the Maven build itself. -->
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.apache.maven.plugins</groupId>
										<artifactId>maven-plugin-plugin</artifactId>
										<versionRange>[3.6.0,)</versionRange>
										<goals>
											<goal>descriptor</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore></ignore>
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

3. Dann brauchen wir noch ein Mojo. Mit einem Parameter, der default das ls -la ausführt.

Dazu die folgende Klasse im Package de.wenzlaff.command.maven.plugin anlegen:

package de.wenzlaff.command.maven.plugin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
 * Ein Kommando-Mojo.
 * 
 * @author Thomas Wenzlaff
 *
 */
@Mojo(name = "info", defaultPhase = LifecyclePhase.INITIALIZE)
public class CommandMojo extends AbstractMojo {

	/**
	 * Der command Parameter. Wenn er nicht angegeben wird, wird default: ls -la
	 * verwendet.
	 * 
	 * Es kann aber auch noch ein Verzeichnis übergeben werden, das überwacht werden
	 * soll. Z.B.
	 * 
	 * mvn de.wenzlaff.dir.maven.plugin:de.wenzlaff.command.maven.plugin:info
	 * -Dcommand="ls -la /Users/thomaswenzlaff"
	 * 
	 */
	@Parameter(property = "command", defaultValue = "ls -la")
	private String command;

	@Parameter(property = "project", readonly = true)
	private MavenProject project;

	public void execute() throws MojoExecutionException, MojoFailureException {

		getLog().info("Thomas sein Command-Plugin in Version: " + project.getVersion() + ", ArtifactId: " + project.getArtifactId());
		getLog().info("Führe das Kommando aus: " + command);

		String commandoErgebnis = getCommand(command);

		getLog().info(commandoErgebnis);
	}

	public String getCommand(String command) throws MojoExecutionException {

		String s = null;
		StringBuilder buffer = new StringBuilder("\n");

		try {
			Process p = Runtime.getRuntime().exec(command);

			try (BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()))) {

				while ((s = stdInput.readLine()) != null) {
					buffer.append(s);
					buffer.append("\n");
				}
			}

			try (BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {

				while ((s = stdError.readLine()) != null) {
					buffer.append("Error: " + s);
				}
			}
		} catch (IOException e) {
			throw new MojoExecutionException("Fehler beim ausführen des Kommandos: '" + command, e);
		}

		return buffer.toString();

	}
}

Die Klasse CommandMojo wird von dem AbstractMojo abgeleitet. Es muss nur eine Methode (execute) implementiert werden. Das Projekt sieht nun so aus:

Ein eigenes Maven-Plugin in 15 Minuten inklusive Dokumentation erstellen

4. Jetzt können wir das Plugin schon bauen mit: mvn clean install

5. Das Plugin testen wir auf der Kommandozeile, indem wir in das Projektverzeichnis wechseln (da wo die pom.xml liegt) und das info Goal aufrufen:

mvn de.wenzlaff.command.maven.plugin:de.wenzlaff.command.maven.plugin:info

Es wird nun das Verzeichnis augegeben und im log geschrieben:

Ein eigenes Maven-Plugin in 15 Minuten inklusive Dokumentation erstellen

Wenn man einen anderen als den default Parameter haben will, kann der mit -D angegeben werden z.B.

mvn de.wenzlaff.command.maven.plugin:de.wenzlaff.command.maven.plugin:info -Dcommand=“ps“

um alle Prozesse auszugeben.

6. Mit mvn site kann auch leicht der gewohnt Report erzeugt werden:

Ein eigenes Maven-Plugin in 15 Minuten inklusive Dokumentation erstellen

Ähnliche Artikel:

  1. Wie können Artifacte nach Maven Central (Nexus) mit einem Mac hochgeladen werden?
  2. Wie können auf einem Mac OS X dmg (Apple disk image) Archive mit Maven und hdiutil erstellt werden?
  3. Wie kann die aktuellste Maven Version 3.6.2 auf dem Raspberry Pi installiert werden?

wallpaper-1019588
altraverse stellt Shojo-Titel für Herbst 2024 vor
wallpaper-1019588
Ninja to Koroshiya no Futarigurashi: Manga erhält eine Anime-Adaption
wallpaper-1019588
[Manga] H.P. Lovecrafts Der leuchtende Trapezoeder
wallpaper-1019588
Gemüsebeet in Mai: Diese 10 Gemüse kannst du jetzt pflanzen