Tuesday, July 25, 2017

Building a Real Java EE + Oracle Coherence Application with Maven

There is an amazing Building a Real Application with Maven story published within a new book, Fusion Middleware Developing Applications Using Continuous Integration. The story tells of building a web-application which uses Oracle Coherence as a data store and a servlet as a data accessor. Apache Maven is used for compilation and building, the appropriate 'pom.xml' files are presented.

If we look deeply into the web-module's 'pom.xml' file, the following dependency (the gar-module) may be found:


A very interesting fact is there isn't any type declaration, so the gar-module is used as a usual 'jar' dependency. If a real application contains more gar-modules and dependencies, the best-practice for Maven users is to define the dependencies in the 'dependencyManagement' section inside the parent 'pom.xml' file. And the <type>gar</type> stanza must exist in this section since the dependency has to be used as the gar-type inside the ear-module's 'pom.xml' file.


Ok, we can say, let's define the dependencies inside the parent 'pom.xml' file and add the references on these dependencies into the 'pom.xml' files of the war- and ear-modules. Note! The 'groupId' and 'artifactId' attributes are modified.



Well, the 'pom.xml' files now look very clear and neatly. And everything might have been fine if there hadn't been a small problem: no gar-modules are included into the compilation classpath. The compilation of the war-module will forever fail, because the classes defined inside the gar-module are missing in the classpath.

The com.oracle.coherence:gar-maven-plugin contains no META-INF/plexus/components.xml file that defines an ArtifactHandler with the <addedToClasspath>true</addedToClasspath> note for .gar files. We can solve the problem just create our own Maven plugin and include the ArtifactHandler there. The appropriate META-INF/plexus/components.xml file is accessible on GitHub. The plugin has to be used in the plugin section of the war-module's 'pom.xml' file:


The sources of the application as well as gar-to-classpath-plugin plugin can be found on GitHub. You comments are more than welcome, as usual.

Would you like to give a 'Like'? Please follow me on Twitter!