From b7f4c3416c207a8ac2bd10578ac09b92031a86ac Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Tue, 14 Jul 2020 22:12:21 +0200 Subject: [PATCH] Inject maven properties in asciidoctor mojo (#459) * inject Maven properties as attributes in Mojo same as site module * updates README * adds unit test * updates CHANGELOG --- CHANGELOG.adoc | 4 ++ README.adoc | 33 ++++++-------- .../invoker.properties | 1 + .../inject-attributes-from-maven-pom/pom.xml | 45 +++++++++++++++++++ .../src/docs/asciidoc/attributes-example.adoc | 15 +++++++ .../validate.groovy | 19 ++++++++ .../asciidoctor/maven/AsciidoctorHelper.java | 16 +++++++ .../asciidoctor/maven/AsciidoctorMojo.java | 3 +- .../SiteConversionConfigurationParser.java | 6 +-- .../maven/test/AsciidoctorMojoTest.groovy | 32 ++++++++++++- .../test/plexus/MockPlexusContainer.groovy | 14 ++++-- .../src/asciidoctor/attributes-example.adoc | 15 +++++++ 12 files changed, 171 insertions(+), 32 deletions(-) create mode 100644 src/it/inject-attributes-from-maven-pom/invoker.properties create mode 100644 src/it/inject-attributes-from-maven-pom/pom.xml create mode 100644 src/it/inject-attributes-from-maven-pom/src/docs/asciidoc/attributes-example.adoc create mode 100644 src/it/inject-attributes-from-maven-pom/validate.groovy create mode 100644 src/test/resources/src/asciidoctor/attributes-example.adoc diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 3fefa134..0b538cba 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ == Unreleased +Improvements:: + + * Inject Maven properties as attributes in `process-asciidoc` mojo (#459) + Bug Fixes:: * Remove Maven components from plugin descriptor (#450) diff --git a/README.adoc b/README.adoc index 839c9757..493f436c 100644 --- a/README.adoc +++ b/README.adoc @@ -216,6 +216,18 @@ Refer to the http://asciidoctor.org/docs/user-manual/#attribute-catalog[catalog coderay ---- +In addition to those attributes found in this section, any Maven property is also passed as attribute (replacing . by -). ++ +[source,xml] +---- + + 2.3.0 <.> + +---- +<.> Will be passed as `my-site-version` to the converter. ++ +Note that when defining a build with multiple executions, shared attributes can be set in the global `` section of the plugin. + embedAssets:: embeds the CSS file and images into the output, defaults to `false` gemPaths:: enables to specify the location to one or more gem installation directories (same as GEM_PATH environment var), `empty` by default requires:: a `List` to specify additional Ruby libraries not packaged in AsciidoctorJ, `empty` by default @@ -306,25 +318,6 @@ Since version 1.5.8 of AsciidoctorJ set `enableVerbose` to `true` option to vali See https://github.com/asciidoctor/asciidoctor/issues/2722[#2722] if your are interested in the details. ==== -==== Passing POM properties - -It is possible to pass properties defined in the POM to the Asciidoctor processor. -This is handy for example to include in the generated document the POM artifact version number. - -This is done by creating a custom AsciiDoc property in the `attributes` section of the `configuration`. -The AsciiDoc property value is defined in the usual Maven way: `${property.name}`. - -[source,xml] ----- - - ${project.version} - ----- - -The custom AsciiDoc property can then be used in the document like this: - - The latest version of the project is {project-version}. - ==== Setting boolean values Boolean attributes in asciidoctor, such as `sectnums`, `linkcss` or `copycss` can be set with a value of `true` and unset with a value of `false`. @@ -558,7 +551,7 @@ Specifies additional Ruby libraries not packaged in AsciidoctorJ, `empty` by def attributes:: Similar to the plugin's `attributes`. + Allows defining a set of Asciidoctor attributes to be passed to the conversion. + -In addition to those attributes found in this section, any maven property is also passed as attribute (replacing . by -). +In addition to those attributes found in this section, any Maven property is also passed as attribute (replacing . by -). + [source,xml] ---- diff --git a/src/it/inject-attributes-from-maven-pom/invoker.properties b/src/it/inject-attributes-from-maven-pom/invoker.properties new file mode 100644 index 00000000..44cc1910 --- /dev/null +++ b/src/it/inject-attributes-from-maven-pom/invoker.properties @@ -0,0 +1 @@ +invoker.goals=clean generate-resources \ No newline at end of file diff --git a/src/it/inject-attributes-from-maven-pom/pom.xml b/src/it/inject-attributes-from-maven-pom/pom.xml new file mode 100644 index 00000000..a0049e71 --- /dev/null +++ b/src/it/inject-attributes-from-maven-pom/pom.xml @@ -0,0 +1,45 @@ + + 4.0.0 + + org.asciidoctor + test + 1.0-SNAPSHOT + + Converts Asciidoctor Article to Html + Processes attributes from different sections in the Maven pom + + + UTF-8 + project property configuration + + + + + + org.asciidoctor + asciidoctor-maven-plugin + @project.version@ + + + plugin configuration + + + + + asciidoc-to-html + generate-resources + + process-asciidoc + + + + execution configuration + + + + + + + + \ No newline at end of file diff --git a/src/it/inject-attributes-from-maven-pom/src/docs/asciidoc/attributes-example.adoc b/src/it/inject-attributes-from-maven-pom/src/docs/asciidoc/attributes-example.adoc new file mode 100644 index 00000000..c53738dd --- /dev/null +++ b/src/it/inject-attributes-from-maven-pom/src/docs/asciidoc/attributes-example.adoc @@ -0,0 +1,15 @@ +Document Title +============== +Doc Writer +:execution.attribute: cosa +:idprefix: id_ + +Preamble paragraph. + +NOTE: This is test, only a test. + +== Section A + +* This attribute is set in the plugin configuration: {plugin-configuration-attribute} +* This attribute is set in the execution configuration: {execution-attribute} +* This attribute is set in the project's properties: {project-property-attribute} diff --git a/src/it/inject-attributes-from-maven-pom/validate.groovy b/src/it/inject-attributes-from-maven-pom/validate.groovy new file mode 100644 index 00000000..4c24817a --- /dev/null +++ b/src/it/inject-attributes-from-maven-pom/validate.groovy @@ -0,0 +1,19 @@ +final File outputDir = new File(basedir, "target/generated-docs"); +final File expectedFile = new File(outputDir, 'attributes-example.html') + +if (!expectedFile.exists()) { + throw new Exception("Missing file " + expectedFile) +} + +expectedFile.text.with { outputContent -> + assertContains(outputContent, 'This attribute is set in the plugin configuration: plugin configuration') + assertContains(outputContent, 'This attribute is set in the execution configuration: execution configuration') + assertContains(outputContent, 'This attribute is set in the project’s properties: project property configuration') +} + +void assertContains(String text, String expectedValueToContain) { + if (!text.contains(expectedValueToContain)) + throw new Exception("Expected value '$expectedValueToContain' not found") +} + +return true \ No newline at end of file diff --git a/src/main/java/org/asciidoctor/maven/AsciidoctorHelper.java b/src/main/java/org/asciidoctor/maven/AsciidoctorHelper.java index ac641bf4..a06190a5 100644 --- a/src/main/java/org/asciidoctor/maven/AsciidoctorHelper.java +++ b/src/main/java/org/asciidoctor/maven/AsciidoctorHelper.java @@ -12,6 +12,7 @@ package org.asciidoctor.maven; +import org.apache.maven.project.MavenProject; import org.asciidoctor.Attributes; import org.asciidoctor.AttributesBuilder; @@ -36,6 +37,21 @@ public static void addAttributes(final Map attributes, Attribute } } + /** + * Adds properties from the {@link MavenProject} into a {@link AttributesBuilder} taking care of Maven's XML parsing special + * cases like toggles, nulls, etc. + * + * @param project Maven project + * @param attributesBuilder AsciidoctorJ AttributesBuilder + */ + public static void addMavenProperties(MavenProject project, AttributesBuilder attributesBuilder) { + if (project.getProperties() != null) { + for (Map.Entry entry : project.getProperties().entrySet()) { + attributesBuilder.attribute(((String) entry.getKey()).replaceAll("\\.", "-"), entry.getValue()); + } + } + } + /** * Adds an attribute into a {@link AttributesBuilder} taking care of Maven's XML parsing special cases like * toggles toggles, nulls, etc. diff --git a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java index 03f5145b..dfe60b86 100644 --- a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java +++ b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java @@ -517,12 +517,13 @@ protected void setOptionsOnBuilder(OptionsBuilder optionsBuilder) { optionsBuilder.templateDirs(templateDirs.toArray(new File[]{})); } - protected void setAttributesOnBuilder(AttributesBuilder attributesBuilder) throws MojoExecutionException { + protected void setAttributesOnBuilder(AttributesBuilder attributesBuilder) { if (embedAssets) { attributesBuilder.linkCss(false); attributesBuilder.dataUri(true); } + AsciidoctorHelper.addMavenProperties(project, attributesBuilder); AsciidoctorHelper.addAttributes(attributes, attributesBuilder); if (!attributesChain.isEmpty()) { diff --git a/src/main/java/org/asciidoctor/maven/site/SiteConversionConfigurationParser.java b/src/main/java/org/asciidoctor/maven/site/SiteConversionConfigurationParser.java index 0e50cd16..7a088a77 100644 --- a/src/main/java/org/asciidoctor/maven/site/SiteConversionConfigurationParser.java +++ b/src/main/java/org/asciidoctor/maven/site/SiteConversionConfigurationParser.java @@ -37,11 +37,7 @@ SiteConversionConfiguration processAsciiDocConfig(Xpp3Dom siteConfig, return new SiteConversionConfiguration(options.get(), Collections.emptyList()); } - if (project.getProperties() != null) { - for (Map.Entry entry : project.getProperties().entrySet()) { - presetAttributes.attribute(((String) entry.getKey()).replaceAll("\\.", "-"), entry.getValue()); - } - } + AsciidoctorHelper.addMavenProperties(project, presetAttributes); final List gemsToRequire = new ArrayList<>(); for (Xpp3Dom asciidocOpt : asciidocConfig.getChildren()) { diff --git a/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy b/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy index 1486e158..aa16ddd9 100644 --- a/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy +++ b/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy @@ -439,7 +439,7 @@ class AsciidoctorMojoTest extends Specification { } // Test for Issue 62 - def 'setting_boolean_values'() { + def 'setting boolean attributes values'() { given: File srcDir = new File(DEFAULT_SOURCE_DIRECTORY) File outputDir = new File('target/asciidoctor-output-issue-62') @@ -460,11 +460,39 @@ class AsciidoctorMojoTest extends Specification { File sampleOutput = new File(outputDir, 'sample.html') String text = sampleOutput.getText() text.contains('class="toc2"') + } + def 'should inject attributes from maven property, plugin and execution'() { + given: + MockPlexusContainer.initializeMockContext(AsciidoctorMojo, + ['project.property.attribute': 'project property configuration']) + File srcDir = new File(DEFAULT_SOURCE_DIRECTORY) + File outputDir = new File('target/unit-tests/attributes') + + if (!outputDir.exists()) + outputDir.mkdir() + when: + AsciidoctorMojo mojo = new AsciidoctorMojo() + mojo.outputDirectory = outputDir + mojo.sourceDirectory = srcDir + mojo.sourceDocumentName = 'attributes-example.adoc' + mojo.backend = 'html5' + mojo.attributes = [ + 'plugin-configuration-attribute': 'plugin configuration', + 'execution-attribute': 'execution configuration' + ] + mojo.resources = [new Resource(directory: '.', excludes: ['**/**'])] + mojo.execute() + then: + File sampleOutput = new File(outputDir, 'attributes-example.html') + String text = sampleOutput.getText() + text.contains('This attribute is set in the project’s properties: project property configuration') + text.contains('This attribute is set in the plugin configuration: plugin configuration') + text.contains('This attribute is set in the execution configuration: execution configuration') } // Test for Issue 62 (unset) - def 'unsetting_boolean_values'() { + def 'unsetting boolean attributes values'() { given: File srcDir = new File(DEFAULT_SOURCE_DIRECTORY) File outputDir = new File('target/asciidoctor-output-issue-62-unset') diff --git a/src/test/groovy/org/asciidoctor/maven/test/plexus/MockPlexusContainer.groovy b/src/test/groovy/org/asciidoctor/maven/test/plexus/MockPlexusContainer.groovy index 64a6e267..57b279c1 100644 --- a/src/test/groovy/org/asciidoctor/maven/test/plexus/MockPlexusContainer.groovy +++ b/src/test/groovy/org/asciidoctor/maven/test/plexus/MockPlexusContainer.groovy @@ -23,12 +23,14 @@ class MockPlexusContainer { Log logger = new SystemStreamLog() } - private void initializeMojoContext(AsciidoctorMojo mojo) { + private void initializeMojoContext(AsciidoctorMojo mojo, Map properties) { mojo.@project = [ - getBasedir: { + getBasedir : { return new File('.') - }] as MavenProject + }, + getProperties: properties as Properties + ] as MavenProject mojo.@buildContext = new DefaultBuildContext() @@ -51,12 +53,16 @@ class MockPlexusContainer { * Intercept Asciidoctor mojo constructor to mock and inject required plexus objects. */ static MockPlexusContainer initializeMockContext(Class clazz) { + initializeMockContext(clazz, Collections.emptyMap()) + } + + static MockPlexusContainer initializeMockContext(Class clazz, Map mavenProperties) { final MockPlexusContainer mockPlexusContainer = new MockPlexusContainer() def oldConstructor = clazz.constructors[0] clazz.metaClass.constructor = { def mojo = oldConstructor.newInstance() - mockPlexusContainer.initializeMojoContext(mojo) + mockPlexusContainer.initializeMojoContext(mojo, mavenProperties) return mojo } mockPlexusContainer diff --git a/src/test/resources/src/asciidoctor/attributes-example.adoc b/src/test/resources/src/asciidoctor/attributes-example.adoc new file mode 100644 index 00000000..c53738dd --- /dev/null +++ b/src/test/resources/src/asciidoctor/attributes-example.adoc @@ -0,0 +1,15 @@ +Document Title +============== +Doc Writer +:execution.attribute: cosa +:idprefix: id_ + +Preamble paragraph. + +NOTE: This is test, only a test. + +== Section A + +* This attribute is set in the plugin configuration: {plugin-configuration-attribute} +* This attribute is set in the execution configuration: {execution-attribute} +* This attribute is set in the project's properties: {project-property-attribute}