For an ESB development, we use sub-projects of type carbon archive, esb and registry. We can also add custom mediators, custom java projects, as well as test (sub)projects.
Pom file of Maven parent project
xml version="1.0"Â encoding="UTF-8"?>
|
Let’s dig in to version management.
By default, versions in eclipse consist of 3 parts, major, minor and bugfix version components, i.e. 1.0.0. It is possible your projects to have 4 parts in a version. In the following examples, I will use a 4-part version number, such as 1.0.0.0. With the maven versions plugin we are able to change the artifact version of all pom.xml files involved in this project. The following command is used to set the new version from any version to 1.2.3.4:
mvn versions:set -DnewVersion=1.2.3.4Â Â
After execution of the command, the version of the parent project and all sub-projects is set to the new value.
The project version in all pom files are changed.
Main pom snippet
<?xml version="1.0"Â encoding="UTF-8"?> |
Esb project pom snippet
<?xml version="1.0"Â encoding="UTF-8"?> |
Artifact.xml and versions
In WSO2 esb and registry project there is a file named artifact.xml which contains the artifacts within the project (sequence, template, registry resource). All artifacts have their own version.
Artifact.xml in esb project
<?xml version=“1.0” encoding=“UTF-8”?> <artifacts> <artifact name=“accounts” groupId=“com.example.esb-samples.endpoint” version=“1.0.0.0” type=“synapse/endpoint” serverRole= “EnterpriseServiceBus”> <file>src/main/synapse-config/endpoints/accounts.xml</file> </artifact> <artifact name=“mail_receiver” groupId=“com.example.esb-samples.proxy-service” version=“1.0.0.0” type=“synapse/proxy-service” serverRole=“EnterpriseServiceBus”> <file>src/main/synapse-config/proxy-services/mail_receiver.xml</file> </artifact> </artifacts> |
Artifact.xml in the registry project
<?xml version=“1.0” encoding=“UTF-8”?> <artifacts>    <artifact name=“xslt” groupId=“com.example.registry-samples.resource” version=“1.0.0.0” type=“registry/resource” serverRole= “GovernanceRegistry”>     <collection>       <directory>xslt</directory>       <path>/_system/governance/config/xslt</path>     </collection>   </artifact>   <artifact name=“endpoints” groupId=“com.example.registry-samples.resource” version=“1.0.0.0” type=“registry/resource” serverRole= “GovernanceRegistry”>     <collection>       <directory>endpoints</directory>       <path>/_system/governance/config/endpoints</path>     </collection>   </artifact> </artifacts> |
In case you do not see the artifact.xml in Eclipse Project Explorer
In the default WSO2 Developer Studio for Eclipse, artifact.xml files are not visible. You can see them in file system, or in Navigator perspective, and since you need to be aware of those files, you can customize the view in Eclipse as follows:
- Right click on the menu in project Explorer
- Select Customize View
- Uncheck Hide Artifact.xml files box
When you create a new artifact using WSO2 Developer Studio, the version of that artifact is taken from the project pom at the moment of the artifact creation. If we increase version of the project through Maven, the versions in artifact.xml file are not changed automatically. We want to have the matching version numbers between the maven project and the artifacts like a sequence and any other artifact when we build a project.
We will achieve update of the versions in the artifacts with these 2 steps:
- Change version of the dependency artifacts in the car pom
- Change versions of the artifacts in the artifact.xml
Change version of the dependency artifacts in the car pom
It is easy to use the variable ${project.version}
in the version element of the dependency.
   <dependency> |
Â
Car project pom file
<?xml version="1.0"Â encoding="UTF-8"?> |
Change versions of the artifacts in the artifact.xml
A simple replacement of the version inside the artifact.xml file will not work because if we add the ${project.version}
in the place of the version, the build will take this exact string and attach it as it is a version number. The variable will not be changed in the moment when the artifact.xml is used to build the artifact files. Moreover, in m2 folder you will find the string “${project.version}”
as a suffix to the artifact files, instead of the real version.
In order to solve updating of the versions inside the artifact files, we can use maven-antrun-plugin in the pom file of the project that contains the artifact file.
Execution phase is important
Maven-antrun-plugin should run on the phase validate, because after this phase, the artifact.xml file is used by the wso2 maven plugins to complete the build.
The ant task should have a regular expression for finding version-attributes with a version number. The substitution expression should be the replacement of that attribute value with the project’s version number.
Plugin execution specification
<plugins> Â Â Â <plugin> Â Â Â Â <artifactId>maven-antrun-plugin</artifactId> Â Â Â Â <version>1.8</version> Â Â Â Â <executions> Â Â Â Â Â <execution> Â Â Â Â Â Â <id>artifacts_version</id> Â Â Â Â Â Â <phase>validate</phase> Â Â Â Â Â Â <goals> Â Â Â Â Â Â Â <goal>run</goal> Â Â Â Â Â Â </goals> Â Â Â Â Â Â <configuration> Â Â Â Â Â Â Â <tasks> Â Â Â Â Â Â Â Â <replaceregexp byline="true"> Â Â Â Â Â Â Â Â Â <regexp pattern="(?<!xml) version="(?:(d+).)?(?:(d+).)?(*|d+)?(?:(d+).)?(*|d+)(-SNAPSHOT)?""Â /> Â Â Â Â Â Â Â Â Â <substitution expression=" version="${project.version}""Â /> Â Â Â Â Â Â Â Â Â <fileset dir="."> Â Â Â Â Â Â Â Â Â Â <include name="artifact.xml"Â /> Â Â Â Â Â Â Â Â Â </fileset> Â Â Â Â Â Â Â Â </replaceregexp> Â Â Â Â Â Â Â </tasks> Â Â Â Â Â Â </configuration> Â Â Â Â Â </execution> Â Â Â Â </executions> Â Â Â </plugin> Â Â Â <plugin> |
Executing the command mvn clean install
after the version increase (mvn versions:set _DnewVersion=1.2.3.4)
will change all artifact versions to the new version which confirms compliance of our code with the latest version number from the main pom file.
The regular expression
The regular expression for version number with four numeric groups and optional –SNAPSHOT is
<regexp pattern="(?<!xml) version="(?:(d+).)?(?:(d+).)?(*|d+)?(?:(d+).)?(*|d+)(-SNAPSHOT)?"" />
Versions matching the above regex patterns are:
111.222.333.444
111.222.333.444-SNAPSHOT
With this specific part of the regex: (?<!xml)
, we make sure to exclude the first line in the xml: <?xml version="1.0" encoding="UTF-8"?>.
The substitution pattern
The substitution pattern is
  <substitution expression=" version="${project.version}"" />
Notice the empty space at the beginning, the escaped sign for quote "
and the parameter ${project.version}
which is the version of the main project.
Table 1 Artifact file from ESB project
<?xml version=“1.0” encoding=“UTF-8”?><artifacts>    <artifact name=“accounts” groupId=“com.example.esb-samples.endpoint” version=“1.2.3.4” type=“synapse/endpoint” serverRole= “EnterpriseServiceBus”>     <file>src/main/synapse-config/endpoints/accounts.xml</file>   </artifact>    <artifact name=“mail_receiver” groupId=“com.example.esb-samples.proxy-service” version=“1.2.3.4” type=“synapse/proxy-service” serverRole=“EnterpriseServiceBus”>     <file>src/main/synapse-config/proxy-services/mail_receiver.xml</file>   </artifact> </artifacts> |
Table 2 Artifact file from Registry project
<?xml version="1.0"Â encoding="UTF-8"?> <artifacts> Â Â Â <artifact name="xslt"Â groupId="com.example.registry-samples.resource"Â version="1.2.3.4"Â type="registry/resource" Â Â Â Â <collection> Â Â Â Â Â Â <directory>xslt</directory> Â Â Â Â Â Â <path>/_system/governance/config/xslt</path> Â Â Â Â </collection> Â Â </artifact> Â Â <artifact name="endpoints"Â groupId="com.example.registry-samples.resource"Â version="1.2.3.4"Â type="registry/resource" Â Â Â Â <collection> Â Â Â Â Â Â <directory>endpoints</directory> Â Â Â Â Â Â <path>/_system/governance/config/endpoints</path> Â Â Â Â </collection> Â Â </artifact> </artifacts> |
References
[1] https://maven.apache.org/index.html[2] https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
[3]Â https://svn.wso2.org/repos/wso2/trunk/tools/ide/eclipse/docs/src/site/xdoc/devs_maven_support.xml