fb
WSO2 Tutorial 6 min

WSO2: How to change Proxy Parameters at build time

joost-hofman.jpg
Joost Hofman
WSO2 Expert
Scroll

Many of us that are working often with the VFS transport have a common problem: The static VFS parameters.

When deploying to an environment that doesn’t have the same folder structure you have to manually change the proxy parameters.

This can easily be fixed with Maven at build time.

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

First We create for each environment a properties file that contains the translation from a key to the actual value:

I work on a windows machine so my properties file would look like:

Src/main/properties/Local.properties

1
2
3
file.location.in=file://d:/yenlo/in
file.location.fail=file://d:/yenlo/fail
file.location.done=file://d:/yenlo/done

But my production environment is running on linux:

Src/main/properties/Production.properties

1
2
3
file.location.in=file:///yenlo/in
file.location.fout=file:/// yenlo/fail
file.location.klaar=file:/// yenlo/done

After creating the environment properties files we can change the pom.xml from the WSO2 ESB Configuration project.

There are many ways to do this and you can make it as fancy as you want.

In this example I keep it simple.

In the pom.xml we create 2 new profiles:

1
2
3
4
5
6
7
8
9
10
11
12
13
<profiles>
  <profile>
    <id>production</id>
<plugins> ... <plugins>
</profile>
  <profile>
    <id>local</id>
    <activation>
<activeByDefault>true</activeByDefault>
    </activation>
<plugins> ... <plugins>
</profile>
  </profiles>

When we don’t specify a profile (-P) Maven will automatically take the local profile because of the activation element.

Now we can add the plugin we use to do the actual replacement.

Because WSO2 does a double Maven build (first from source and then another build from target) we will replace the keys at compile time in the target folder. I use the replacer plugin from “com.google.code.maven-replacer-plugin”, because it’s simple to configure and has the options that we need for our case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<plugins>
     <plugin>
            <groupId>com.google.code.maven-replacer
-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <basedir>${project.build.directory}
</basedir>
                               <includes>
                                                
<include>**/*.xml</include>
                                </includes>
                               <excludes>

<exclude>src/**/*.*</exclude>
                                </excludes>
                <tokenValueMap>${basedir}/src
/main/properties/local.properties</tokenValueMap>
            </configuration>
        </plugin>
    </plugins>

As you can see in the above configuration we set the execution time on compile.

As the baseDir we set the target folder.

We include every xml file that’s in there.

Just in case I excluded the src folder, this should not be necessary but better save then sorry.

At the tokenValueMap we add the path to our properties file. This will be different for profile.

In the end the profiles should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<profiles>
  <profile>
    <id>production</id>
                <build>
     <plugins>
                               <plugin>
            <groupId>com.google.code.maven-replacer
-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>

<basedir>${project.build.directory}</basedir>

<includes>
                    <include>**/*.xml</include>
                </includes>

<excludes>
                    <exclude>src/**/*.*</exclude>
                </excludes>
                <tokenValueMap>src/main/properties
/production.properties</tokenValueMap>
            </configuration>
        </plugin>
    </plugins>
  </build>
  </profile>
  <profile>
    <id>local</id>
    <activation>

<activeByDefault>true</activeByDefault>
                </activation>
                <build>
     <plugins>
                               <plugin>

<groupId>com.google.code.maven-replacer-
plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>1.5.3</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>

<basedir>${project.build.directory}</basedir>

<includes>
                    <include>**/*.xml</include>
                </includes>

<excludes>
                    <exclude>src/**/*.*</exclude>
                </excludes>
                <tokenValueMap>${basedir}/src/main
/properties/local.properties</tokenValueMap>
            </configuration>
        </plugin>
    </plugins>
  </build>
  </profile>
  </profiles>

Our changes to the POM.xml file are done.

We can now make our changes to the proxy service.

1
2
3
<parameter name="transport.vfs.MoveAfterProcess&quot
;>file.
location.done</parameter>
<parameter name="transport.vfs.FileURI">file.
location.in&lt
;/parameter>
<parameter name="transport.vfs.MoveAfterFailure">file.
location.fail</parameter>

We add the key values from our property file to the proxy service.

With the command “mvn clean install” Maven will build the local files

With the command “mvn clean install –Production” Maven will build the production files.

Of course there are many other ways to solve this case. But I like this solution because it’s easy and fast to implement.

Note: After these changes you can no longer deploy directly from the Developer Studio into the WSO2 server.

Full API lifecycle Management Selection Guide

WHITEPAPER

smartmockups l0qqucke