0

I'm using asciidoctor-maven-plugin and asciidoctor-diagram to generate PlantUML diagrams (more specifically C4-PlantUML diagrams). When generating those diagrams, output is polluted by these kind of messages

[Log] "dynamic undefined legend colors" requires PlantUML version >= 1.2021.6, therefore only static assigned colors are used

I tracked them down to asciidoctor-diagram embedding - in its source, lo less, a reasonably old version of PlantUML (see their Github source). Fortunatly, there is in asciidoctor-diagram plantuml converter.rb class some code to use PlantUML provided externally through an environment variable named DIAGRAM_PLANTUML_CLASSPATH.

But as I use asciidoctor-diagram through asciidoctor-maven-plugin, how can I set that environment variable from my Maven POM without setting it in the maven launch ? Indeed, I want to use a dependency from my pom and maven-dependency-plugin properties goal to have it correct on any machine without configuration.

I've already tried using asciidoctor-maven-plugin configuration attributes, but it didn't seem to work.

Is there any other way to inject environment variables into asciidoctor-maven-plugin ?

EDIT 1 I've also tried using the properties-maven-plugin to add the environment variable to my maven build this way

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <id>Set DIAGRAM_PLANTUML_CLASSPATH from PlantUML maven dependency PATH</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>DIAGRAM_PLANTUML_CLASSPATH</name>
                                    <value>${net.sourceforge.plantuml:plantuml:jar}</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

But it changed nothing

EDIT 2

So, after some reflection, I had to resolve to use the age-long solution : debugging the culprit code (in this case, asciidoctor-maven-plugin). In AsciidoctorMojo#execute(), in a long chain of calls, we initialize a JRuby interpreter (in JRubyAsciidoctor#createRubyRuntime). When creating this interpreter, we pass the system environment. This system environment DO NOT include my DIAGRAM_PLANTUML_CLASSPATH variable, so I guess the properties-maven-plugin does not really set it as environment variable for the whole maven build - or there has been some changes in recent maven versions.

However, this JRubyAsciidoctor#createRubyRuntime method accepts as input a map of environment variables. Can we manipulate it ? Absolutely not as it is a private method.

And guess what ? DIAGRAM_PLANTUML_CLASSPATH doesn't appear in the list because it's not an environment variable, but a system property, which is quite different.

So the question is more "how can I create an environment variable ? Or inject an environment variable in my ruby env prior to running my build ?"

Riduidel
  • 22,052
  • 14
  • 85
  • 185
  • in my case i was able to pass environment variable as configuration attribute like `${ES_CONFLUENCE_USER}`. So, i used the same semantic, as you mentioned in your snippet. Maybe try to pass environment variable in uppercase – iChrome Sep 17 '21 at 08:06

1 Answers1

0

So, I've established in the question that what was not working was the injection of the environment variable into JRuby : As default, Java doesn't allow setting environment variable, and on the other hand asciidoctor only allow injection through environment variable. So I had to create that environment variable. Hopefully, Stackoverflow already answers that question : How do I set environment variables from Java?. My only point was doing that in a maven build. Enter GMaven Plus. Using this plugin the following way

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>properties</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <executions>
                    <execution>
                        <id>Put PlantUML path in that damn DIAGRAM_PLANTUML_CLASSPATH environment variable</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>${project.basedir}/src/build/groovyScripts/set_PlantUML_path_in_system_environment.groovy</script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Notice the preliminary call to dependency-maven-plugin to obtain all dependencies paths as properties.

THen I created a very small Groovy script


log.debug "PlantUML dependency path is ${project.properties['net.sourceforge.plantuml:plantuml:jar']}"

setEnvironmentVars(['DIAGRAM_PLANTUML_CLASSPATH':project.properties['net.sourceforge.plantuml:plantuml:jar']])

And it was enough to have asciidoctor-maven-plugin working.

CAVEAT: I'm quite sure this won't work with recent Java releases like Java 14 and Java 17. Anyway, in Java 11, it works!

Riduidel
  • 22,052
  • 14
  • 85
  • 185