In my Java Programming class at Penn State – Great Valley, we are learning all about Enterprise Java; session beans, JPA, JMS, and all kinds of other Java goodness.  In the process, I am teaching them how to build their projects with Eclipse and Maven using the M2Eclipse plugin.  The plugin has improved a lot in the last few months, but we  still had to work out the kinks for a couple of issues.

The Default Maven Install Run Configuration Fails

This issue can be seen when running a project that has multiple modules in it. In my case we have an ejb module and then an ear module that is dependent on the ejb module.  Running Default Maven InstallWhen we try to run the default maven install task for the project (see image to the right), we get an error:

The following mojo encountered an error while executing:

Group-Id: org.apache.maven.plugins
Artifact-Id: maven-ear-plugin
Version: 2.3.1
Mojo: ear
brought in via: packaging: ear

While building project:

Group-Id: edu.psu.gv

Artifact-Id: classtwo-ear
Version: 0.0.1-SNAPSHOT
From file: /Users/rdimarco/Documents/workspace/classtwo/ear/pom.xml
Reason: Cannot copy a directory: /Users/rdimarco/Documents/workspace/classtwo/ejb/target/classes; Did you package/install active project artifact:
artifact = edu.psu.gv:classtwo-ejb:ejb:0.0.1-SNAPSHOT:compile;
project: MavenProject: edu.psu.gv:classtwo-ejb:0.0.1-SNAPSHOT @ /Users/rdimarco/Documents/workspace/classtwo/ejb/pom.xml?

The problem is that there is a bug when the “Resolve Workspace Artifacts” button is checked.  To resolve the problem, we need to create a custom run configuration, set the goals to install, and make sure the “Resolve Workspace Artifacts” is unchecked.

The build will now proceed correctly

Unable to Locate the Javac Compiler

Many of my students saw the following error: 


Reactor Summary:

</font></pre> > > <pre>

[INFO] ------------------------------<wbr></wbr>------------------------------<wbr></wbr>------------

[INFO] Class Two ..............................<wbr></wbr>............... SUCCESS [1.483s]

</font></pre> > > <pre>

[INFO] ClassTwo EJBs ..............................<wbr></wbr>........... FAILED [2.388s]

</font></pre> > > <pre>

[INFO] ClassTwo EAR ..............................<wbr></wbr>............ NOT BUILT

</font></pre> > > <pre>

[INFO] ------------------------------<wbr></wbr>------------------------------<wbr></wbr>------------

</font></pre> > > <pre>

[ERROR]

</font></pre> > > <pre>

 
Mojo: org.apache.maven.plugins:<wbr></wbr>maven-compiler-plugin:2.0.2:<wbr></wbr>compile
 
FAILED for project: edu.psu.gv:classtwo-ejb:ejb:0.<wbr></wbr>0.1-SNAPSHOT

</font></pre> > > <pre>

Reason:Unable to locate the Javac Compiler in:
  C:\Program
Files\Java\jre6\..\lib\tools.
<wbr></wbr>jar
Please ensure you are using JDK 1.4 or above
and not a JRE (the com.sun.tools.javac.Main class is required).
In most cases you can change the location of your Java
installation by setting the JAVA_HOME environment variable.

</font></pre>

As the error message suggests, this issue occurs when you are trying to run Maven and your the runtime JRE used to run the Maven executable is from a JRE and not a JDK.  To fix this, you need to do the following things:

  1. Install a valid JDK on to your computer.  You will need to download the JDK from Sun and then install it on your computer.
  2. Tell Eclipse about the JDK.  To do this, you will
    1. Go to the Preferences. 
      • On Windows, choose the top menu “Window” and then select sub-menu “Preferences”
      • On a Mac, choose the top menu “Eclipse” and then select the sub-menu “Preferences
    2. Click on >Java and then on > Installed JREs
    3. You will see a box listing one or more JREs.  Click on the button to the right of this box “Add
    4. You will be given a list of types of JREs to use.  Choose Standard VM
    5. You will then get a screen asking for the JDK directory.  Click on the Directory button and navigate to the newly installed JDK directory
      • On Windows, this will be something like C:\Program Files\Java\jdk1.6_xx
      • On Mac, this will be something like /System/Library/Frameworks/JavaVM.framework/Versions/1.6.xx/Home
    6. After choosing your directory, Eclipse will think for a bit and then fill in some additional fields on the form.  You do not need to make any changes, just hit Finish.
  3. Now we will need to tell Maven to use this JDK when it runs
    1. Right click on your project and choose Run As -> Run Configurations (as you normally would to build)
    2. In the Run Configuration dialog, click on the tab JRE.
    3. Click on the radio button for Alternate JRE
    4. From the drop down list, choose your JDK

You will now be able to run Maven without the error.