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

Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...