From 211461f228fd8ec162bf415f904030472d7eb880 Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Wed, 28 Aug 2024 01:44:14 +0200 Subject: [PATCH] [fj-doc-maven-plugin] added init fugerit venus goal --- CHANGELOG.md | 1 + fj-doc-maven-plugin/README.md | 32 ++++++-- .../org/fugerit/java/doc/maven/MojoInit.java | 75 +++++++++++++++++++ .../java/doc/project/facade/TestInit.java | 57 ++++++++++++++ 4 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoInit.java create mode 100644 fj-doc-maven-plugin/src/test/java/test/org/fugerit/java/doc/project/facade/TestInit.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 52639087f..95cc9e32e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- [fj-doc-maven-plugin] added init fugerit venus goal - [fj-doc-maven-plugin] build order changed, now will build before fj-doc-sample - [fj-doc-maven-plugin] new param 'reportOutputFormat' (default 'html') of 'verify' plugin diff --git a/fj-doc-maven-plugin/README.md b/fj-doc-maven-plugin/README.md index 6dc770689..a9c9e988e 100644 --- a/fj-doc-maven-plugin/README.md +++ b/fj-doc-maven-plugin/README.md @@ -39,7 +39,7 @@ mvn org.fugerit.java:fj-doc-maven-plugin:add \ -Dextensions=base,freemarker,mod-fop ``` -*Parameters* +### Add Fugerit Venus, parameters | parameter | required | default | description | |-----------------|----------|-----------------|---------------------------------------------------------------------------------------------------| @@ -51,10 +51,7 @@ mvn org.fugerit.java:fj-doc-maven-plugin:add \ | excludeXmlApis | false | false | It will exclude dependency xml-apis:xml-apis | | addExclusions | false | | Add comma separated exclusion, for instance : xml-apis:xml-apis,${groupId}:${artificatId} | | addVerifyPlugin | true | true | If set to true, it will configure the 'verify' goal on the project | - - - -*Available extensions* +### Add Fugerit Venus, available extensions | short name | full name | type handler | description | |-----------------|------------------------|--------------|---------------------------------------------------------------------------------------------------------| @@ -69,6 +66,31 @@ mvn org.fugerit.java:fj-doc-maven-plugin:add \ | base-yaml | fj-doc-base-yaml | | add support to use yaml documents as format for document template | +## Goal : init + +Create a new project and add Venus Doc Configuration to it. + +*Quickstart* : + +```shell +mvn org.fugerit.java:fj-doc-maven-plugin:init \ +-DgroupId=org.example.doc \ +-DartifactId=fugerit-demo +``` + +Project folder will be `./${artifactId}/`. + +### Init Fugerit Venus, parameters + +| parameter | required | default | description | +|----------------|----------|----------------|----------------------| +| groupId | true | | new project group id | +| artifactId | true | | new project group id | +| projectVersion | true | 1.0.0-SNAPSHOT | new project version | +| javaRelease | true | 21 | java release version | + +NOTE: it is possible to set any property from 'add' goal, except 'projectFolder' which is set to `./${artifactId}`. + ## Goal : verify verify the templates in a FreeMarker configuration (folder), note: it can be used on any Apache FreeMarker configuration, not only Fugerit Venus Doc. diff --git a/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoInit.java b/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoInit.java new file mode 100644 index 000000000..77b43754a --- /dev/null +++ b/fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoInit.java @@ -0,0 +1,75 @@ +package org.fugerit.java.doc.maven; + +import org.apache.maven.model.Model; +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.fugerit.java.core.function.UnsafeVoid; +import org.maxxq.maven.dependency.ModelIO; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +@Mojo( name = "init", requiresProject = false, defaultPhase = LifecyclePhase.NONE ) +public class MojoInit extends MojoAdd { + + @Parameter(property = "groupId", required = true) + protected String groupId; + + @Parameter(property = "artifactId", required = true) + protected String artifactId; + + @Parameter(property = "projectVersion", defaultValue = "1.0.0-SNAPSHOT", required = true) + protected String projectVersion; + + @Parameter(property = "javaRelease", defaultValue = "21", required = true) + protected String javaRelease; + + protected String baseInitFolder; + + public MojoInit() { + this.baseInitFolder = "."; + } + + public void apply(UnsafeVoid fun) throws MojoFailureException { + try { + fun.apply(); + } catch (Exception e) { + throw new MojoFailureException( String.format( "Project init failed: %s", e.getMessage() ), e ); + } + } + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + File initFolder = new File( this.baseInitFolder, this.artifactId ); + if ( initFolder.exists() ) { + throw new MojoFailureException( String.format( "Folder %s already exists.", initFolder.getAbsolutePath() ) ); + } else { + this.getLog().info( String.format( "project folder %s -> %s", initFolder.getAbsolutePath(), initFolder.mkdir() ) ); + File pomFile = new File( initFolder, "pom.xml" ); + Model model = new Model(); + model.setGroupId( this.groupId ); + model.setArtifactId( this.artifactId ); + model.setVersion( this.projectVersion ); + model.setModelVersion( "4.0.0" ); + Properties props = new Properties(); + props.setProperty( "maven.compiler.release", this.javaRelease ); + props.setProperty( "project.build.sourceEncoding", StandardCharsets.UTF_8.name() ); + model.setProperties( props ); + this.apply( () -> { + this.projectFolder = initFolder.getCanonicalPath(); + ModelIO modelIO = new ModelIO(); + try (OutputStream pomStream = new FileOutputStream( pomFile ) ) { + modelIO.writeModelToStream( model, pomStream ); + } + } ); + } + super.execute(); + } + +} diff --git a/fj-doc-maven-plugin/src/test/java/test/org/fugerit/java/doc/project/facade/TestInit.java b/fj-doc-maven-plugin/src/test/java/test/org/fugerit/java/doc/project/facade/TestInit.java new file mode 100644 index 000000000..158aa7b44 --- /dev/null +++ b/fj-doc-maven-plugin/src/test/java/test/org/fugerit/java/doc/project/facade/TestInit.java @@ -0,0 +1,57 @@ +package test.org.fugerit.java.doc.project.facade; + +import lombok.extern.slf4j.Slf4j; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.fugerit.java.core.cfg.ConfigException; +import org.fugerit.java.doc.maven.MojoInit; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.util.UUID; + +@Slf4j +public class TestInit { + + private String getVersion() { + return "8.7.1"; + } + + private File initConfigWorker() { + File outputFolder = new File( "target", "init_"+UUID.randomUUID().toString() ); + outputFolder.mkdir(); + return outputFolder; + } + + @Test + public void testMojoInit() throws MojoExecutionException, MojoFailureException { + File projectDir = this.initConfigWorker( ); + MojoInit mojoInit = new MojoInit() { + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + this.baseInitFolder = projectDir.getAbsolutePath(); + this.projectVersion = "1.0.0-SNAPSHOT"; + this.groupId = "org.fugerit.java.test"; + this.artifactId = "fugerit-test1"; + this.javaRelease = "21"; + this.version = getVersion(); + this.extensions = "fj-doc-base,fj-doc-base-json,fj-doc-base-yaml,fj-doc-freemarker,fj-doc-mod-fop,fj-doc-mod-poi,fj-doc-mod-opencsv"; + this.addDocFacade = true; + this.force = true; + this.excludeXmlApis = true; + this.addVerifyPlugin = true; + super.execute(); + } + }; + mojoInit.execute(); + Assert.assertTrue( projectDir.exists() ); + Assert.assertThrows( MojoFailureException.class, () -> mojoInit.execute() ); + Assert.assertThrows( MojoFailureException.class, () -> mojoInit.apply( () -> { + if ( Boolean.TRUE ) { + throw new ConfigException( "Scenario excetion" ); + } + } ) ); + } + +}