Search code examples
javaspring-bootsoapwsdl

Inject value from application.yaml to wsdl file


I have .wsdl file which I want to Environmentalize. For each environment it will be same, only different will be in address location property. Exist any way how to set this value dynamically from application.yaml file?

<wsdl:service name="PaymentService">
    <wsdl:port name="PaymentPortV1" binding="ns:PaymentServiceSoap">
        <wsdlsoap:address location="${gpwebpay.location}"/>
    </wsdl:port>
</wsdl:service>

Solution

  • A possible solution is to use Maven Profiles in tandem with Maven Resources Plugin.

    Define your Profiles in Your pom.xml

    <profiles>
        <profile>
            <id>profile1</id>
            <properties>
                <gpwebpay.location>https://default.location.com/</gpwebpay.location>
            </properties>
        </profile>
        <profile>
            <id>profile2</id>
            <properties>
                <gpwebpay.location>https://other.location.com/</gpwebpay.location>
            </properties>
        </profile>
    </profiles>
    

    Configure Maven Resources Plugin

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <execution>
                            <id>filter-resources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>resources</goal>
                            </goals>
                            <configuration>
                                <resources>
                                    <resource>
                                        <directory>src/main/resources/wsdl</directory>
                                        <filtering>true</filtering>
                                        <includes>
                                            <include>**/*.wsdl</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
        </plugins>
    <build>
    

    Invoke Maven With Profile

    Depending on your environment:

    mvn -Pprofile1 clean install
    

    or:

    mvn -Profile2 clean install
    

    Will end up with your desired WSDL file in target/classes/sample.wsdl:

    <wsdl:service name="PaymentService">
        <wsdl:port name="PaymentPortV1" binding="ns:PaymentServiceSoap">
            <wsdlsoap:address location="https://other.location.com/"/>
        </wsdl:port>
    </wsdl:service>
    

    In this case I specified -Pprofile2.

    IMPORTANT: With Spring Framework / Spring Boot, you should use @notation@ for your variables:

    <wsdl:service name="PaymentService">
        <wsdl:port name="PaymentPortV1" binding="ns:PaymentServiceSoap">
            <wsdlsoap:address location="@gpwebpay.location@"/>
        </wsdl:port>
    </wsdl:service>
    

    Use With Spring Profiles

    If you're already using Spring Profiles in your project, I would suggest to create a profile like this:

    <profile>
        <id>test</id>
        <properties>
            <spring.profiles.active>test</spring.profiles.active>
            <filter.filename>src/main/resources/application-test.properties</filter.filename>
        </properties>
    </profile>
    

    Next, use the defined filter.filename property in the plugin:

    <configuration>
        <filters>
            <filter>${filter.filename}</filter>
        </filters>
    </configuration>
    

    Invoke Maven:

    mvn -Ptest clean install