javamavenopenapi-generatorexec-maven-plugin

Passing value between diffirent maven plugin


I'm having trouble passing credentials that are Base64-encoded between different Maven plugins. Here's my approach so far:

  1. Using the exec-maven-plugin to Compute Base64 Credentials:

I use the exec-maven-plugin to encode the credentials (stored in environment variables) into Base64 format. I verified that the environment variables exist before proceeding.

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>compute-base64</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>sh</executable>
                            <arguments>
                                <argument>-c</argument>
                                <argument>
                                    echo -n '${ar.user}:${ar.password}' | base64
                                </argument>
                            </arguments>
                            <outputFile>${project.build.directory}/base64-output.txt</outputFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

  1. Using the properties-maven-plugin to Read the Base64 Output:

After generating the Base64-encoded credentials, I use the properties-maven-plugin to read the contents of the output file.


<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${project.build.directory}/base64-output.txt</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
  1. Passing the Base64 Credentials to the OpenAPI Generator:

Finally, I pass the Base64-encoded credentials (retrieved via the property ${base64-output}) to the OpenAPI Generator plugin for authentication.

<plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.2.0</version>
                <configuration>
                    <!-- https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/jaxrs-spec.md -->
                    <generatorName>jaxrs-spec</generatorName>
                    <generateApiTests>false</generateApiTests>
                    <inputSpec>${project.basedir}/reference/project.yaml</inputSpec>
                    <modelPackage>project.api.dto</modelPackage>
                    <auth>Authorization: Basic ${base64-output}</auth>
                    <configOptions>
                        <sourceFolder>src</sourceFolder>
                        <apiPackage>project.api</apiPackage>
                        <useTags>true</useTags>
                        <dateLibrary>java8-localdatetime</dateLibrary>
                        <interfaceOnly>true</interfaceOnly>
                        <returnResponse>true</returnResponse>
                        <generateBuilders>true</generateBuilders>
                        <serializableModel>true</serializableModel>
                        <useJakartaEe>true</useJakartaEe>
                    </configOptions>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-api</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

With this setup, I should be able to pass the Base64-encoded credentials to the OpenAPI generator for authentication, but it isn't working as expected.


Solution

  • I've already found the solution:

    1. Check the execution order of the plugin.
    2. Create a properties file with the necessary settings for the Maven environment.

    here the solution

    Step 1: Create Properties File To start, I created a properties file containing the necessary environment settings. This is done by using the exec-maven-plugin to execute a shell command and write the output to a properties file.

    
    <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>3.1.0</version>
                    <executions>
                        <execution>
                            <id>compute-base64</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <configuration>
                                <executable>sh</executable>
                                <arguments>
                                    <argument>-c</argument>
                                    <argument>
                                        echo  "ar.token=tokenencryted" 
                                    </argument>
                                </arguments>
                                <outputFile>${project.build.directory}/base64-output.properties</outputFile>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    This configuration runs the shell command and generates a properties file (base64-output.properties) in the target directory.

    Step 2: Read the Properties File Next, use the properties-maven-plugin to read the properties from the file and make them available in the Maven environment.

    
    <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>properties-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <executions>
                        <execution>
                            <phase>initialize</phase>
                            <goals>
                                <goal>read-project-properties</goal>
                            </goals>
                            <configuration>
                                <files>
                                    <file>${project.build.directory}/base64-output.properties</file>
                                </files>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    This ensures that the properties are loaded into the Maven environment.

    Step 3: Verify the Properties I verified that the properties are being read correctly by using the maven-antrun-plugin to print the property value to the console during the build.

    
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <echo message="Base64 Output: ${ar.token}" />
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    This will print the value of ar.token to the console to verify it's correctly passed through the Maven environment.

    Step 4: Use the Properties in Another Plugin Finally, ensure the properties are used by other steps in the build lifecycle. For example, the openapi-generator-maven-plugin can use the base64-output property in its configuration to generate the API with the necessary authorization header.

    
    <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>7.2.0</version>
                    <configuration>
                        <!-- https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/jaxrs-spec.md -->
                        <generatorName>jaxrs-spec</generatorName>
                        <generateApiTests>false</generateApiTests>
                        <inputSpec>${project.basedir}/reference/project.yaml</inputSpec>
                        <modelPackage>project.api.dto</modelPackage>
                        <auth>Authorization: Basic ${base64-output}</auth>
                        <configOptions>
                            <sourceFolder>src</sourceFolder>
                            <apiPackage>project.api</apiPackage>
                            <useTags>true</useTags>
                            <dateLibrary>java8-localdatetime</dateLibrary>
                            <interfaceOnly>true</interfaceOnly>
                            <returnResponse>true</returnResponse>
                            <generateBuilders>true</generateBuilders>
                            <serializableModel>true</serializableModel>
                            <useJakartaEe>true</useJakartaEe>
                        </configOptions>
                    </configuration>
                    <executions>
                        <execution>
                            <id>generate-api</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    in this step, the Authorization header is set dynamically using the base64-output property, which was generated earlier.