From 5c52ff3ed9290b71064c10640ff74ce64abcf9c7 Mon Sep 17 00:00:00 2001 From: Cornelius Wicke Date: Mon, 15 Jun 2020 14:50:55 +0200 Subject: [PATCH] Initial content --- .gitignore | 5 + README.md | 13 +++ pom.xml | 85 +++++++++++++++ .../ispackage/mojo/IsPackageDefinition.java | 41 +++++++ .../ispackage/mojo/IsPackageMojo.java | 103 ++++++++++++++++++ 5 files changed, 247 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/farsight/ispackage/mojo/IsPackageDefinition.java create mode 100644 src/main/java/farsight/ispackage/mojo/IsPackageMojo.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3204498 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.metadata +.settings +target +.project +.classpath diff --git a/README.md b/README.md new file mode 100644 index 0000000..3259206 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# farsight-wm-parent +[![License: MIT](https://img.shields.io/badge/License-MIT-silver.svg)](https://opensource.org/licenses/MIT) + +A small maven-plugin assisting in building IS-packages that depend on jar files. + +## Features + - copy jar files from compile or runtime dependencies into the code/jars (/static) directory of the is package. + +## Nice-to-Have Features + - compile java services + - create java service nodes from annotations in the source code + - create importable zip archive + - create/update entries in mainfest.v3 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b21ad7a --- /dev/null +++ b/pom.xml @@ -0,0 +1,85 @@ + + 4.0.0 + + farsight-wm + is-package-maven-plugin + + maven-plugin + + 1.0.0 + farsight-wm:is-package-maven-plugin + https://github.com/farsight-wm/is-package-maven-plugin + + + https://github.com/farsight-wm/parent/is-package-maven-plugin + GitHub.com + + + + https://github.com/farsight-wm/is-package-maven-plugin + scm:git:https://github.com/farsight-wm/is-package-maven-plugin.git + scm:git:https://github.com/farsight-wm/is-package-maven-plugin.git + HEAD + + + + + The MIT License (MIT) + https://opensource.org/licenses/MIT + manual + + + + + + Cornelius Wicke + https://www.xing.com/profile/Cornelius_Wicke + + + + + UTF-8 + 1.8 + ${java.version} + ${java.version} + + + + + org.apache.maven + maven-plugin-api + 2.0 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.6.0 + provided + + + org.apache.maven + maven-project + 2.2.1 + + + + org.apache.commons + commons-lang3 + 3.7 + + + org.codehaus.plexus + plexus-utils + 3.3.0 + + + + junit + junit + 3.8.1 + test + + + diff --git a/src/main/java/farsight/ispackage/mojo/IsPackageDefinition.java b/src/main/java/farsight/ispackage/mojo/IsPackageDefinition.java new file mode 100644 index 0000000..0ab43a3 --- /dev/null +++ b/src/main/java/farsight/ispackage/mojo/IsPackageDefinition.java @@ -0,0 +1,41 @@ +package farsight.ispackage.mojo; + +import java.util.List; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.maven.plugins.annotations.Parameter; + +public class IsPackageDefinition { + + public static class IsJarDependency { + @Parameter + public boolean staticJar = false; + + @Parameter(required = true) + public String id; + + @Parameter(required = false) + public String filename; + + public String toString() { + return new ToStringBuilder(this) + .append("id", id) + .build(); + } + } + + + @Parameter(required = true) + public String name; + + @Parameter(required = false) + public List jars; + + public String toString() { + return new ToStringBuilder(this) + .append("name", name) + .append("jars", jars) + .build(); + } + +} diff --git a/src/main/java/farsight/ispackage/mojo/IsPackageMojo.java b/src/main/java/farsight/ispackage/mojo/IsPackageMojo.java new file mode 100644 index 0000000..82bc72b --- /dev/null +++ b/src/main/java/farsight/ispackage/mojo/IsPackageMojo.java @@ -0,0 +1,103 @@ +package farsight.ispackage.mojo; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +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.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.util.FileUtils; + +import farsight.ispackage.mojo.IsPackageDefinition.IsJarDependency; + +/** + * Mojo that copies required jar file into an is package + */ +@Mojo(name = "is-package", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME) +public class IsPackageMojo extends AbstractMojo { + + @Parameter(defaultValue = "src/is") + public String source; + + @Parameter(defaultValue = "target/is") + public String target; + + @Parameter(required = true) + public List packages; + + @Parameter(defaultValue = "${project}", readonly = true, required = true) + private MavenProject project; + + private File sourceBase; + private File targetBase; + + public void execute() throws MojoExecutionException { + sourceBase = new File(project.getBasedir(), source); + targetBase = new File(project.getBasedir(), target); + + for (IsPackageDefinition isPackage : packages) { + buildIsPackage(isPackage); + } + } + + private void buildIsPackage(IsPackageDefinition isPackage) throws MojoExecutionException { + getLog().info("Building IS-Package: " + isPackage.name); + + File pkgSource = new File(sourceBase, isPackage.name); + File pkgTarget = new File(targetBase, isPackage.name); + + if (!pkgSource.isDirectory()) { + throw new MojoExecutionException("IS-Package source not found at: " + pkgSource); + } + + pkgTarget.mkdirs(); + + try { + FileUtils.copyDirectoryStructureIfModified(pkgSource, pkgTarget); + } catch (IOException e) { + throw new MojoExecutionException("Error copying source files", e); + } + + if (isPackage.jars != null) { + // append jars + + File jars = new File(pkgTarget, "code/jars"); + File staticJars = new File(jars, "static"); + + for (IsJarDependency jar : isPackage.jars) { + Artifact artifact = getArtifact(jar); + if (artifact == null) + throw new MojoExecutionException("Artifact is not a dependency: " + jar.id); + + getLog().info("Adding jar: " + artifact); + File outDirectory = jar.staticJar ? staticJars : jars; + outDirectory.mkdirs(); + + File jarFile = artifact.getFile(); + if (jarFile == null || !jarFile.canRead()) + throw new MojoExecutionException("Cannot read artifact file: " + jarFile); + + File output = new File(outDirectory, jar.filename == null ? jarFile.getName() : jar.filename); + + try { + FileUtils.copyFile(jarFile, output); + } catch (IOException e) { + throw new MojoExecutionException("Cannot copy artifact file: " + jarFile, e); + } + } + + } + + } + + private Artifact getArtifact(IsJarDependency jar) { + return jar.id.equals(project.getGroupId() + ":" + project.getArtifactId()) ? project.getArtifact() + : (Artifact) project.getArtifactMap().get(jar.id); + } +}