Archive

Posts Tagged ‘eclipse’
  • Why Eclipse WTP doesn't publish libraries when using m2e

    Lately, I noticed my libraries weren’t published to Tomcat when I used a Maven project in Eclipse, even though it was standard war packaging. Since I mostly use Vaadin, I didn’t care much, I published the single vaadin-x.y.z.jar to the deployed WEB-INF/lib manually and I was done with it.

    Then, I realized it happened on two different instances of Eclipse and for the writing of Develop Vaadin apps with Scala, I used 3 different libraries, so I wanted to correct the problem. At first, I blamed it on the JRebel plugin I recently installed, then I began investigating the case further and I found out that I needed a special Maven connector that was present in neither of my Eclipse instances: the WTP connector. I had installed this kind of connector back when Maven Integration was done by m2eclipse by Sonatype, but had forgotten that a while ago. Now the integration is called m2e, but I have to do the whole nine yards again…

    The diagnostics that can be hard but the solution is very simple.

    Go to the Windows -> Preferences menu and from this point on go to Maven -> Discovery.

    Click on Open Catalog.

    Search for “wtp”, select Maven Integration for WTP and click on Finish. Restart and be pleased. Notice that most Maven plugins integration in Eclipse can be resolved the same way.

    For a sum up on my thoughts about the current state of m2e, please see here.

    Categories: JavaEE Tags: eclipsemavenwtp
  • Migrating from m2eclipse to m2e

    Since Indigo, the Maven Ecliple plugin formerly known as m2eclipse became part of Eclipse release (at least in the pure Java release). The name of the plugin also changed from m2eclipse to m2e. This was not the sole change, however:

    • The number of tabs on the POM has shrinked drastically, and the features as well. This will probably be the subject of a later post since I feel quite cheated by the upgrade.
    • The POM configuration has been more integrated with Eclipse build (which can cause unwanted side-effects as I described in my last article).

    More importantly, projects that began with m2eclipse can be built in Indigo but no contextual Maven menu is accessible on the project itself (though a contextual menu is available on the POM).

    In order to migrate flawlessly and have our contextual menu back, some actions are necessary. They are gruesome because it involves updating by hand Eclipse configuration file.

    Warning: at this point, you have the choice to stop reading. If you decide to continue and use the process described below, it’s at your own risk!

    The Maven plugin recognizes a project as a Maven one based the .project Eclipse proprietary configuration file. To display it, go to the Project Explorer view, click on the scrolling menu at the top right and choose Customize View. You have to uncheck *.resources: along the .project file , you should see a .classpath file as well as a .settings folder.

    1. In the .project:
      • Replace org.maven.ide.eclipse.maven2Builder by org.eclipse.m2e.core.maven2Builder in the buildSpec section
      • Replace org.maven.ide.eclipse.maven2Nature by org.eclipse.m2e.core.maven2Nature in the natures section
    2. In the .classpath, replace org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER by org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER
    3. Finally, in the .settings folder, rename the org.maven.ide.eclipse.prefs file to org.eclipse.m2e.core.prefs. Contents should be left unchanged.

    Now, the contextual menu should appear and work accordingly.

    Remember, this is a big hack you should only use with the right parachute (at least a source control management system) since it will hurt you plenty if it fails. For me, it has always worked… yet.

    Categories: Java Tags: eclipsem2eclipsemaven
  • My first Scala servlet (with Eclipse)

    In this article, I will show you how to tweak Eclipse so that you will be able to code “classical” webapps in Scala.

    Note: I know about Lift, I just want to see how Scala can integrate in my existing infrastructure step-by-step.

    Setting up your Eclipse project

    Eclipse has a plugin(found at the Scala IDE site) that let you help develop with Scala. The plugin is by no mean perfect (autocompletion does not works in Ganymede), but it gets the job done, letting you create Scala projects that automatically compile Scala code with scalac.

    The Web Tools Platform, another Eclipse plugin, let you create web application projects that can be deployed to application servers.

    Most projects created with one or another Eclipse plugin are compatible with one another. For example, you can create a new Dynamic Web Project, then add a JPA extension and presto, you can now use the Java Persistence API in you project. Likewise, you can add a Vaadin extension to your Web Project and you can use Vaadin in it. These extensions are called “Facets” in Eclipse. Each project can have many facets: Java, JPA, Vaadin, etc. depending on the plugins you installed.

    Unfortunately, the Scala plugin does not use the Facet mechanism. Thus, Dynamic Web Projects cannot be enhanced with the Scala facet. Creating a web project that compiles your Scala code requires a little tweaking. First, you should create a simple Dynamic Web Project. Then, open the .project file. If you do not see the file, go to Customize View in the view and uncheck .*resources in the Filter tab (the first one). It should look something like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <projectDescription>
        <name>scala-servlet-example</name>
        <comment></comment>
        <projects>
        </projects>
        <buildSpec>
            <buildCommand>
                <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
                <arguments>
                </arguments>
            </buildCommand>
            <buildCommand>
                <name>org.eclipse.jdt.core.javabuilder</name>
                <arguments>
                </arguments>
            </buildCommand>
            <buildCommand>
                <name>org.eclipse.wst.common.project.facet.core.builder</name>
                <arguments>
                </arguments>
            </buildCommand>
            <buildCommand>
                <name>org.eclipse.wst.validation.validationbuilder</name>
                <arguments>
                </arguments>
            </buildCommand>
        </buildSpec>
        <natures>
            <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
            <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
            <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
            <nature>org.eclipse.jdt.core.javanature</nature>
            <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
        </natures>
    </projectDescription>
    

    Replace the org.eclipse.jdt.core.javabuilder with org.scala-ide.sdt.core.scalabuilder. Note: you can remove the Java builder with the IDE but you cannot add another one so hacking the configuration file is a necessary evil. Now add the Scala nature to your project:

    <nature>org.scala-ide.sdt.core.scalanature</nature>
    

    The .project file should now look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <projectDescription>
        <name>scala-servlet-example</name>
        <comment></comment>
        <projects>
        </projects>
        <buildSpec>
            <buildCommand>
                <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
                <arguments>
                </arguments>
            </buildCommand>
            <buildCommand>
                <name>org.scala-ide.sdt.core.scalabuilder</name>
                <arguments>
                </arguments>
            </buildCommand>
            <buildCommand>
                <name>org.eclipse.wst.common.project.facet.core.builder</name>
                <arguments>
                </arguments>
            </buildCommand>
            <buildCommand>
                <name>org.eclipse.wst.validation.validationbuilder</name>
                <arguments>
                </arguments>
            </buildCommand>
        </buildSpec>
        <natures>
            <nature>org.scala-ide.sdt.core.scalanature</nature>
            <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
            <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
            <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
            <nature>org.eclipse.jdt.core.javanature</nature>
            <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
        </natures>
    </projectDescription>
    

    If you made the right change, you should see a S instead of a J on your project’s icon (and it should keep the globe!).

    The last thing to do should be to add the Scala library to your path. Depending on your application server configuration, either add the Scala library to your build path (Build Path -> Configure Build Path -> Add Library -> Scala Library) or manually add the needed library to your WEB-INF/lib folder. You’re all set to create your first Scala servlet!

    Creating your servlet

    In order to create your servlet “the easy way”, just do New -> Other -> Scala Class. Choose a package (it is a good practice to respect the Java guidelines regarding the class location according to its package name). Choose the HttpServlet as the superclass. Name it however you please and click OK.

    It seems to compile but you and I know there will be some problem later since you don’t override the servlet’s doGet() method. Do it:

    override def doGet(request:HttpServletRequest, response:HttpServletResponse) {
    
    }
    

    Don’t forget the imports. Let’s use the Scala way:

    import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse}
    

    Last but not least, let’s code some things for your servlet to do. Since I’m feeling very innovative, I will print the good old “Hello world!”:

    override def doGet(request: HttpServletRequest, response: HttpServletResponse) {
      response setContentType ("text/html")
      val out = response getWriter
      out println """<html>
      <head>
          <title>Scala Servlet
      <body>
          <p>Hello world!"""
      }
    }
    

    Don’t forget to add the servlet to your web.xml and presto, you should see some familiar example, in a familiar IDE, but with some unorthodox language.

    Conclusion

    Once Eclipse correctly set up, it’s pretty straightforward to develop your webapp in Scala. I do hope the next version of the Scala IDE plugin will definitely improve Scala’s integration into Eclipse and WTP further.

    Here are the sources of the example, in Eclipse format.

    To go further:

    Categories: JavaEE Tags: eclipsescala
  • Top Eclipse plugins I wouldn't go without

    Using an IDE to develop today is necessary but any IDE worth his salt can be enhanced with additional features. NetBeans, IntelliJ IDEA and Eclipse have this kind of mechanism. In this article, I will mention the plugins I couldn’t develop without in Eclipse and for each one advocate for it.

    m2eclipse

    Maven is my build tool of choice since about 2 years. It adds some very nice features comparing to Ant, mainly the dependencies management, inheritance and variable filtering. Configuring the POM is kind of hard once you’ve reached a fairly high number of lines. The Sonatype m2eclipse plugin (formerly hosted by Codehaus) gives you a tabs-oriented view of every aspect of the POM:

    • An Overview tab neatly grouped into : Artifact, Parent, Properties, Modules, Project, Organization, SCM, Issue Management and Continuous Integration,

      m2eclipse Overview tab

    • A Dependencies tab for managing (guess what) dependencies and dependencies management. For each of the former, you can even exclude dependent artifacts. This tab is mostly initialized at the start of the project, since its informations shouldn’t change during the lifecycle,
    • A Repositories tab to deal with repositories, plugin repositories, distribution, site and relocation (an often underused feature that enable you to change an artifact location without breaking builds a.k.a a level of indirection),
    • A Build tab for customizing Maven default folders (a usually very bad idea),
    • A Plugins tab to configure and/or execute Maven plugins. This is one of the most important tab since it’s here you will configure maven-compiler-plugin to use Java 6, or such,
    • A Report tab to manage the ` part,
    • A Profiles tab to cope with profiles,
    • A Team tab to fill out team-oriented data such as developers and contributors information,
    • The most useful and important tab (according to me) graphically displays the dependency tree. Even better, each scope is represented in a different way and you can filter out unwanted scope.

      m2eclipse Dependencies tab

    • Last but not least, the last tab enables you to directly edit the underlying XML.

    Moreover, m2eclipse adds a new Maven build Run configuration that is equivalent for the command line:

    m2eclipse Run Configuration

    With this, you can easily configure the -X option (Debug Output) or the -Dmaven.test.skip option (Skip Tests).

    More importantly, you can set up the plugin to resolve dependencies from within the workspace during Eclipse builds; that is, instead of using your repository classpath, Eclipse will use the project’s classpath (provided it is in the desired version). It prevents the need to build an artifact each time it is modified because another won’t compile after the change. It merely duplicates the legacy Eclipse dependency management.

    I advise not to use the Resolve Workspace artifacts in the previous Run configuration because it will use this default behaviour. In Maven build, I want to distance myself from the IDE, using only the tool’s features.

    TestNG plugin

    For those not knowing TestNG, it is very similar to JUnit4. It was the first to bring Java 5 annotations (even before JUnit) so I adopted the tool. Now as to why I keep it even though JUnit4 uses annotations: it has one important feature JUnit has not. You can make a test method dependent on another, so as to develop test scenarios. I know this is not pure unit testing anymore; still I like using some scenarios in my testing packages in order to test build breaks as early as possible.

    FYI, Maven knows about TestNG and runs TestNG tests as readily as JUnit ones.

    The TestNG plugin for Eclipse does as the integrated JUnit plugin does, whether regarding configuration or run or even test results.

    TestNG Plugin Run configuration

    Emma

    When developing, and if one uses tests, one should know about one’s test coverage over code. I used to use Cobertura Maven plugin: I configured in the POM and, every once in a while, I ran a simple mvn cobertura:cobertura. Unfortunately, it is not very convenient to do so. I searched for an Eclipse plugin to have the same feature; alas, there’s none.  However, I found the EclEmma Eclipse plugin that brings the same functionality. It uses Emma (an OpenSource code coverage tool) under the hood and, though I searched thoroughly, Emma has no Maven 2 plugin. Since I value equally IDE code coverage during development and Maven code coverage during nightly builds (on a Continuous Integration infrastrcuture), so you’re basically stuck with 2 different products. So?

    EclEmma line highlighting

    ElcEmma provides a 4th run button (in addition to Run, Debug and External Tools) that launches the desired run configuration (JUnit, TestNG or what have you) in enhanced mode, the latter providing the code coverage feature. In the above screenshot, you can see line 20 was not run during the test.

    Even better, the plugin provides a aggregation view for code coverage percentage. This view can be decomposed on the project, source path, package and class levels.

    EclEmma statistics

    Spring IDE

    Spring does not need to be introduced. Whether it will be outed by JEE 5 dependency injection annotations remains to be seen. Plenty of projects still use Spring and that’s a fact. Still, XML configuration is very ankward in Spring in a number of cases:

    • referencing a qualified class name. It is not neither easy nor productive to type it; the same is true for properties
    • understanding complex or big configurations files
    • referencing a Spring bean in a hundred or more lines of file
    • refactoring a class name or a property name without breaking the configurations files
    • being informed of whether a class is a Spring bean in a project and if so, where it is used

    Luckily, Spring IDE provides features that make such tasks easy as a breeze:

    • auto-completion on XML configuration files
    • graphic view of such files

      Spring IDE Graph View

    • an internal index so that refactoring is takes into account the XML files (though I suspect there is some bugs hidden somewhere for I regularly have errors)
    • a enhanced Project Explorer view to display where a bean is used

      Spring Project Explorer View

    This entire package guarantees a much better productivity when using XML configuration in Spring than plain old XML editor. Of course, you can still use Annotation configuration, though I’m reluctant to do so (more in a latter post).

    I conclusion, these 4 integration plugins mean I feel really more comfortable using their underlying tools. If I were in an environment where I couldn’t update my Eclipse however I choose, I would definitely think about using these tools at all (except Maven), or use Annotations configuration for Spring. You can have exceptional products or frameworks, they have to integrate seamlessly into your IDE(s) to really add value: think about the fate of EJB v2!