Ignite a Java EE 6 project for Eclipse Juno with Maven 3

Supposed that Maven and Eclipse are correctly installed, we can execute in the workspaces directory…

E:devworkspaces>mvn archetype:generate

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> br.gov.frameworkdemoiselle.archetypes:demoiselle-jsf-jpa (Archetype for web applications (JSF + JPA) using Demoiselle Framework)
2: remote -> br.gov.frameworkdemoiselle.archetypes:demoiselle-minimal (Basic archetype for generic applications using Demoiselle Framework)
3: remote -> co.ntier:spring-mvc-archetype (An extremely simple Spring MVC archetype, configured with NO XML.)
:
:
:
413: remote -> org.codehaus.mojo.archetypes:webapp-j2ee13 (-)
414: remote -> org.codehaus.mojo.archetypes:webapp-j2ee14 (-)
415: remote -> org.codehaus.mojo.archetypes:webapp-javaee6 (-)
416: remote -> org.codehaus.mojo.archetypes:webapp-jee5 (-)
417: remote -> org.codehaus.mojo.groovy:groovy-maven-archetype (An archetype for creating Maven modules/projects using the Groovy language.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 231:

Find the “webapp-javaee6” archetype ID – in this case 415. This number is changing all the time, btw. Enter 415 at the Maven prompt.

You get asked for the archetype plugin version to use. Let’s chose the latest.

Choose org.codehaus.mojo.archetypes:webapp-javaee6 version:
1: 1.0
2: 1.0.1
3: 1.0.2
4: 1.1
5: 1.2
6: 1.3
7: 1.4
8: 1.5
Choose a number: 8:

Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee6/1.5/webapp-javaee6-1.5.jar
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee6/1.5/webapp-javaee6-1.5.jar (2 KB at 12.1 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee6/1.5/webapp-javaee6-1.5.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/mojo/archetypes/webapp-javaee6/1.5/webapp-javaee6-1.5.pom (918 B at 12.0 KB/sec)

Define value for property 'groupId': : eu.lucubratory
Define value for property 'artifactId': : cck
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  eu.lucubratory: :
Confirm properties configuration:
groupId: eu.lucubratory
artifactId: cck
version: 1.0-SNAPSHOT
package: eu.lucubratory
Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: webapp-javaee6:1.5
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: eu.lucubratory
[INFO] Parameter: artifactId, Value: cck
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: eu.lucubratory
[INFO] Parameter: packageInPathFormat, Value: eu/lucubratory
[INFO] Parameter: package, Value: eu.lucubratory
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: eu.lucubratory
[INFO] Parameter: artifactId, Value: cck
[INFO] project created from Archetype in dir: E:devworkspaceseclipsecck
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3:31.560s
[INFO] Finished at: Thu Dec 06 21:51:48 CET 2012
[INFO] Final Memory: 12M/245M
[INFO] ------------------------------------------------------------------------
E:devworkspaces>

Done! Now, we can enter the directory and build the (empty) project (for fun and download some stuff :).

E:devworkspaceseclipse>cd cck

E:devworkspaceseclipsecck>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building cck 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/javax/javaee-web-api/6.0/javaee-web-api-6.0.pom
Downloaded: http://repo.maven.apache.org/maven2/javax/javaee-web-api/6.0/javaee-web-api-6.0.pom (4 KB at 16.1 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/javax/javaee-web-api/6.0/javaee-web-api-6.0.jar
Downloaded: http://repo.maven.apache.org/maven2/javax/javaee-web-api/6.0/javaee-web-api-6.0.jar (909 KB at 670.6 KB/sec)
[INFO]
[INFO] --- maven-dependency-plugin:2.1:copy (default) @ cck ---
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.1/plexus-utils-1.5.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.1/plexus-utils-1.5.1.pom (3 KB at 23.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/doxia/doxia-sink-api/1.0-alpha-10/doxia-sink-api-1.0-alpha-10.pom
:
:
[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ cck ---
[INFO] Packaging webapp
[INFO] Assembling webapp [cck] in [E:devworkspaceseclipseccktargetcck-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [E:devworkspaceseclipseccksrcmainwebapp]
[INFO] Webapp assembled in [24 msecs]
[INFO] Building war: E:devworkspaceseclipseccktargetcck-1.0-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.800s
[INFO] Finished at: Thu Dec 06 21:55:08 CET 2012
[INFO] Final Memory: 12M/243M
[INFO] ------------------------------------------------------------------------
E:devworkspaceseclipsecck>

The fresh Maven file should look similar to this one:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>eu.lucubratory</groupId>
    <artifactId>cck</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>cck</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Let’s update to Java 1.7, add some useful dependencies (for later :) and attach the Eclipse plugin as well:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>eu.lucubratory</groupId>
    <artifactId>cck</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>cck</name>
    <url>http://lucubratory.eu</url>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jersey.version>1.11</jersey.version>
        <jackson.version>1.9.2</jackson.version>
    </properties>

    <!-- Order of dependencies is of importance! -->
    <dependencies>

        <!-- Test dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <!-- The javaee JARs contains an "empty" implementetion, thats why they HAVE to be
        put at the end of the classpath. So potentially added "real" implementations get
        loaded first. -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <!-- Full EE profile: <artifactId>javaee-api</artifactId> -->
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

 <build>
   <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <compilerArguments>
                <endorseddirs>${endorsed.dir}</endorseddirs>
            </compilerArguments>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1.1</version>
        <configuration>
           <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>

      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.1</version>
         <executions>
           <execution>
              <phase>validate</phase>
              <goals>
                <goal>copy</goal>
              </goals>
              <configuration>
                <outputDirectory>${endorsed.dir}</outputDirectory>
                <silent>true</silent>
                <artifactItems>
                  <artifactItem>
                    <groupId>javax</groupId>
                      <artifactId>javaee-endorsed-api</artifactId>
                      <version>6.0</version>
                      <type>jar</type>
                  </artifactItem>
                </artifactItems>
              </configuration>
           </execution>
         </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
            <wtpversion>2.0</wtpversion>
            <downloadSources>true</downloadSources>
            <downloadJavadocs>true</downloadJavadocs>

            <additionalProjectFacets>
               <!-- For front-end/web modules -->
               <jst.jaxrs>1.1</jst.jaxrs>
               <jst.jstl>1.2</jst.jstl>
               <jst.web>3.0</jst.web>
               <!-- For back-end modules, replace the above by
               <jst.ejb>3.1</jst.ejb> -->
            </additionalProjectFacets>

            <!-- Set Eclipse project to UTF-8 -->
            <additionalConfig>
              <file>
                 <name>.settings/org.eclipse.core.resources.prefs</name>
                 <content>
                   <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=${project.build.sourceEncoding}${line.separator}]]>
                 </content>
              </file>
            </additionalConfig>

        </configuration>
     </plugin>

   </plugins>
 </build>

</project>

The Eclipse plugin allows generating Eclipse project metafiles by using the command: mvn eclipse:clean eclipse:eclipse (remember: mvn plugin:command). The ‘clean’ parameter is only required when regenerating the Eclipse project files after updates made outside of Eclipse, like adding dependencies in the pom.xml.

Eclipse needs to know the path to the local Maven repository. Therefore the classpath variable M2_REPO has to be set in Eclipse. This can be done via maven: mvn -Declipse.workspace= eclipse:add-maven-repo or manually in Eclipse via the “Window > Preferences, Java > Build Path > Classpath Variables” page, where M2_REPO has to be added.

More info can be found here: http://maven.apache.org/plugins/maven-eclipse-plugin/.

After executing the eclipse plugin, we can open Eclipse and “File, Import…” the project.

Glassfish 3.1.2 and Eclipse Juno

Normally, you would install the Oracle Glassfish plugin for Eclipse via the Eclipse Market, but if you are using Eclipse Juno and Glassfish 3.1.2, the plugin will not be able to handle your Glassfish installation. Juno will not recognize the Glassfish server path. (I don’t understand why the official plugin is always 100 years behind the current Glassfish release?!).

Instead, you have to install another plugin via “Help, Install new Software”. Use this URL to find the operational one:
http://dlc.sun.com.edgesuite.net/glassfish/eclipse/juno/. (There is also http://dlc.sun.com.edgesuite.net/glassfish/eclipse/indigo if you stay with Indigo).

After restarting Eclipse, you will find “GlassFish 3.1.2.2” in the server view and you can create an instance by selecting the “xxx/glassfish3/glassfish” directory.

Et zou!

1 thought on “Ignite a Java EE 6 project for Eclipse Juno with Maven 3

Leave a Reply to Imifos Cancel reply

Your email address will not be published. Required fields are marked *