views:

294

answers:

4

Most of our projects are built with Maven, and some of them use classes in the com.sun package. That's a bad practice, but there's not much I can do about it.

For a while this didn't seem to be a big problem. Then, at some point, builds started failing for me with errors such as the following:

java.lang.Error: Unresolved compilation problems:
        Access restriction: The type TextSerializer is not accessible due to res
triction on required library C:\Program Files\Java\jre1.5.0_15\lib\rt.jar

That's fair enough, but I couldn't for the life of me figure out how to turn off that check. The builds were failing both from the command line and from within Eclipse, so I assumed it must be some Maven setting.

Eventually I figured out that the problem was somehow with Eclipse. I went to Preferences > Java > Compiler > Errors/Warnings > Deprecated and restricted API and changed 'Forbidden Reference' to 'Warning'. Thereafter, my builds started working. This is completely incomprehensible to me, because I was getting the errors when building from the command line.

Can someone please explain to me how an Eclipse setting can somehow affect the behaviour of a build from the command line?!

+3  A: 
Pascal Thivent
Thanks for your help, but I grepped the whole project for "compilerId" and it showed up nothing, so that isn't it.
Daniel Cassidy
Is maven really using the same JRE as Eclipse? What's the value of JAVA_HOME?
Pascal Thivent
JAVA_HOME=C:\Program Files\Java\jdk1.5.0_15PATH includes %JAVA_HOME%\binEclipse is set to use the same version.The choice of Java 1.5.0_15 is deliberate due to a project here that 'requires' it, but I tried with 1.6.0_16 and got the same problem.
Daniel Cassidy
Ok, so maven is actually using a JDK... Another question: can you reproduce the problem? If yes, what does happen if you run `mvn clean` before to compile?
Pascal Thivent
I have reliably reproduced the problem on two different machines -- otherwise I would have concluded that I was going mad. I had thought I’d tried mvn clean, but apparently not. My colleague beat you to the suggestion by a few minutes, and trying it solved the problem and led me to the explanation -- see my answer above. Thanks for your help!
Daniel Cassidy
You're welcome. You could have rewarded me though :)
Pascal Thivent
I have now that you've changed the answer :). I will change the Eclipse target folder as you suggest, which should damn well be the default behaviour.
Daniel Cassidy
Thank you very much, that's very nice from you (notice I used a smiley). Regarding the output folders, I agree with you and dislike when eclipse and maven use the same output folders. I find there are more disadvantages than advantages to use collocated folders.
Pascal Thivent
A: 

My colleague helpfully pointed out that mvn clean install fixes the problem. Eventually we worked out that (M2)Eclipse is leaving some partially compiled classes in the target directory, and of course vanilla Maven doesn’t know that it should recompile those classes. I’m a little bit annoyed about this. It should be obvious to anyone that leaving broken output in the target directory is bound to subtly pollute later builds.

Daniel Cassidy
See my answer for a solution to the problem you mention.
Pascal Thivent
FWIW, you should not have different headless and IDE compiler settings. Your question shows exactly why.
Robert Munteanu
The setting in question doesn't even *exist* in the vanilla compiler. So, while your suggestion is reasonable, it presupposes that one is aware of every obscure feature in the Eclipse compiler and how to turn it off.
Daniel Cassidy
A: 

yeah the "clean" is the key Note however that it's not just that it has failure builds around--somehow or other m2eclipse just leaves junk behind, even if it compiles right.

This can also cause things like

java.lang.Error: Unresolved compilation problem:

from the command line

rogerdpack
A: 

I am seeing similar issues using Netbeans (6.9.1) with a Maven project. mvn clean resolves the problem.

kldavis4