Use the eclipse compiler in a maven component
Asked Answered
R

4

12

I am working on a fairly big Maven project, and develop in Java with Eclipse.

To save compilation time, I would like Maven and Eclipse to share the same target, which I managed to do. However when I compile with Maven, Eclipse lacks some stuff that it puts in the bytecode, so it recompiles everything (from what I understand). I am talking about the "build automatically" feature here, so it is not Eclipse delegating the build to Maven.

To solve this, I thought I would ask Maven to use the same compiler as Eclipse. After some search on the web, I found out I could add this in the top pom:

<build>
...
<plugins>
...
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
           <compilerId>eclipse</compilerId>
           <source>1.5</source>
           <target>1.5</target>
           <optimize>true</optimize>
   </configuration>
   <dependencies>
           <dependency>
                   <groupId>org.codehaus.plexus</groupId>
                   <artifactId>plexus-compiler-eclipse</artifactId>
                   <version>1.8.1</version>
           </dependency>
   </dependencies>
</plugin>
</plugins>
</build>

This seems to work, but the build fails fairly quickly with lots of errors, while it succeeds with javac. I'm not sure why, but it seems that there is some conflicts linked to the fact the failing Java files are generated files.

So I thought I could try to use the Eclipse compiler only for the component I am working on (which does not have that kind of generated files). I added the above snippet in the pom of my component, but when the build reaches my component, the following error is raised:

No such compiler 'eclipse'

I also tried to add the plexus-compiler-eclipse dependency in the dependencies listed in the top pom, but same error.

Do you know if what I am trying to do is possible? Any hint of how I can do it?

Ryter answered 27/6, 2011 at 16:29 Comment(0)
O
2

This is an old open issue related to maven multimodule projects: http://jira.codehaus.org/browse/MCOMPILER-165

Ocean answered 17/3, 2015 at 12:37 Comment(1)
Thanks, yes it seems to explain why I couldn't build just one module with the Eclipse compiler. However, unlike in the reported issue, I don't need to have different compiler for different modules, so I should have been able to compile the whole project. Unfortunately it's been a while I left this project and can't investigate anymore.Ryter
O
1

I would guess that your issues arise from the eclipse project and maven pom not being in synch. I would suggest that you use the m2eclipse plugin to keep maven and eclipse in synch. This will configure your eclipse project by using the POM as the 'master' configuration.

I don't think you need to specifically configure what compiler to use, but you should configure the maven-compiler-plugin as you are already doing.

Overfeed answered 27/6, 2011 at 16:36 Comment(1)
I am actually using m2eclipse already. The eclipse projects are generated by eclipse from pom files. Maybe something I should mention: when I say "eclise rebuilds everything", I am talking the "build automatically" feature of eclipse. I need it to do hot swap.Ryter
E
0

Make sure you use the latest version of the maven-compiler-plugin (2.3.2 right now, see this page - it's "Full name" or the little version info in the top right corner).

Apart from this, the configuration should work. You can try with the latest 1.8.4 version of the plexus-compiler-eclipse, too.

Apart from that, you need to understand that Maven has several types of dependencies. In your case, two are interesting: Build-time and plugin dependencies.

The former are collected in the dependencies element while the latter must be in a pluginManagement element. Adding a plugin to the build time classpath will not have the effect you want.

Easeful answered 21/2, 2012 at 9:10 Comment(0)
R
-2

I'm not 100% sure what you are trying to achieve here but it sounds like a bad idea. Firstly, you should have Eclipse configured to being use a SDK on your system and Maven should be using the same thing. So I don't understand why you think Eclipse is adding something to the byte code which Maven would not be given they are both using the same compiler anyway.

Secondly I would not be trying to use any Eclipse components in a maven build. They are design to work within the Eclipse IDE so getting them working outside of that is usually fraught with problems. I've seen it done, but it was a hack and didn't produce very good results.

Thridly if you are having performance problems with a Maven build (not uncommon), there are a few things you can do. Firstly break down the build and work out where the time is going. My experience is that usually it's not the compilation that is the problem. Here's a list of things that I've seen slow down builds:

  • Poor quality tests. Usually slow to start up integration tests compounded with tests that are actually unit tests being run as integrations.

  • Cobertura. Great tool but because of the way Maven works, the Cobertura plugin has to run all the phases from resources to compilation again. I once shaved 40% off the during of a long Maven build by writing a plugin to stop tests being run until they where being run by the cobertura phase.

  • Over building. ie. including submodules that basically never change. Better choice is to create a profile that includes these sub mobules only when necessary or break then out to a completely seperate build.

  • Long integration testing. If the integration tests take a long time, consider putting them into a different profile and a different build on the CI server. Then your main build can go faster allowing developers to get back to work sooner, whilst the integration tests can run less often.

  • Finally - admit that Maven is a performance hog and move to something faster. Ant is probably still the fastest but a pain to setup because you have to manually configure everything. I'd suggest trialing Grandle as it fixes a lot of the issues that both Ant and Maven have. Although to be honest, I have not personally done any performance testing on it. But it does give you a level of control that Maven does not.

Rhodolite answered 28/6, 2011 at 0:1 Comment(3)
Thanks for the advices Derek. To be honest I don't know exactly what the differences are, but for sure eclipse and maven do not generate the same class files, I just did a comparison. Also note that I am not directly using a eclipse component in a maven build, this is a plexus artifact made to be used that way. I am actually quite OK with maven performances, my problem is that when I build with maven then eclipse re-builds the workspace. That is why I would like than maven build and eclipse incremental build share the same compiler.Ryter
Kool, I've seen that problem. It's usually caused by having eclipse build path settings using target/classes and target/test-classes. because maven clean and maven compile manipulate these directories, Eclipse sees the changes and assumes it has to compile. An effective fix is to not use the same directories. In eclipse you can also tell eclipse to ignore/hide certain directories so you don't see mavens target directories.Rhodolite
Having a separate target for eclipse and maven is what I had at first. But then it means that I have to compile twice any modification of the code. That is why I merged eclipse target and maven target in the first place. Hence my question.Ryter

© 2022 - 2024 — McMap. All rights reserved.