Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've noticed strange behaviour when building web services via the Oracle WebLogic Maven Plugin. In some cases the compiled Java sources would be created for the wrong target version of the JDK than what was configured for the Maven Compiler plugin.

I tracked down the conditions that cause this behaviour to this - the JDK that was being used to run the Maven build was of a different (higher) language level than what the target level was configured for the Maven Compiler plugin. For example - using JDK 8 but setting the target language level as 1.7.

When running the wsgen goal, all of the generated classes will use the JDK native language level, ignoring the Maven Compiler plugin settings. All other classes (that are not generated) will be compiled to the correct target level.

This is how the Maven Compiler Plugin was configured in my case...
 Maven
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>


After deploying a service to WebLogic that was being run with JDK 7, a whole bunch of java.lang.UnsupportedClassVersionError exceptions with Unsupported major.minor version 52.0 were being thrown. Whoops!

So how did I fix it?

I knew that I had to force the correct target language level across all class files. The wsgen goal documentation talked about the args parameter but was very scarce on information...
Specifies optional command-line options. Multiple elements can be specified, and each token must be placed in its own list.


That wasn't very helpful so I started to look for wsgen documentation. The most common relevant documentation I found was this:
-J pass this option to javac




Also not super helpful but it was a step in the right direction. I tried setting the target level but no matter what I tried, I either got errors or the values I passed in were ignored. Eventually I came across this page that finally had something that I could use...
-J Pass this option to Javac compiler. Note: use '=' instead of space as a delimiter between option name and its value.


This meant I had to use this string as the args parameter - "-J-source=1.7 -J-target=1.7". The configuration for the wsgen goal now became something like this...
 Maven
<execution>
<id>wsgen</id>
<phase>process-classes</phase>
<goals>
<goal>wsgen</goal>
</goals>
<configuration>
...
<args>
<arg>-J-source=1.7</arg>
<arg>-J-target=1.7</arg>
</args>
<verbose>true</verbose>
</configuration>
</execution>


After making this change, all of the compiled class files were for the correct target language level and no more exceptions were showing in WebLogic!

Of course I would not recommend hard coding the language level as I have in the examples above. In my actual pom.xml files this was all defined via variables.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.