Eclipse JEE

From campisano.org
Jump to navigation Jump to search

Noob tips to discover the Eclipse current working directory

  • Sometime the location of this directory is not the expected
System.out.println("Current working directory:");
System.out.println(System.getProperty("user.dir"));

Maven manual eclipse project

Intro: final folder structure

PROJECT_NAME root
    eclipse/
    src/
        main/
            java/
            resources/
                META-INF/
                    persistence.xml
                log4j.properties
            webapp/
                WEB-INF/
                    mvc-dispatcher-servlet.xml
                    web.xml
                index.jsp
        test/
            java/
    target/
    pom.xml

Preparation

  • Folder structure
mkdir PROJECT_NAME
cd PROJECT_NAME
mkdir -p src/main/java src/test/java eclipse target src/main/webapp/WEB-INF src/main/resources/META-INF/
touch pom.xml
touch src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml
touch src/main/webapp/WEB-INF/web.xml
touch src/main/resources/log4j.properties
touch src/main/resources/META-INF/persistence.xml
touch src/main/webapp/index.jsp


  • Initial src/main/webapp/index.jsp
<html>
    <body>
        <h2>Maven World!</h2>
    </body>
</html>


  • Initial src/main/webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
</web-app>


  • Final src/main/resources/log4j.properties
# Root logger option
log4j.rootLogger = INFO, console, file

# Direct log messages to console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold = WARN
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-5p %d{yyyy-MM-dd HH:mm:ss} [%t][%F:%L] - %m%n

# Direct log messages to a log file
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File = log/log4j.log
log4j.appender.file.MaxFileSize = 1MB
log4j.appender.file.MaxBackupIndex = 3
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %-5p %d{yyyy-MM-dd HH:mm:ss} [%t][%F:%L] - %m%n

Maven

  • This example use java 1.6, log4j, jpa/hsqldb, spring-webmvc, jackson

./pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.PROJECT_NAME</groupId>
    <artifactId>PROJECT_NAME</artifactId>
    <version>0.1</version>
    <packaging>war</packaging>

    <properties>

        <!-- Encoding -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Java version to compile -->
        <java.version>1.6</java.version>

        <!-- Maven compiler -->
        <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>

        <!-- Spring -->
        <spring-core.version>4.0.2.RELEASE</spring-core.version>
        <spring-web.version>4.0.2.RELEASE</spring-web.version>
        <spring-webmvc.version>4.0.2.RELEASE</spring-webmvc.version>
        <spring-orm.version>4.0.2.RELEASE</spring-orm.version>
        <spring-tx.version>4.0.2.RELEASE</spring-tx.version>
        <spring-data-jpa.version>1.5.1.RELEASE</spring-data-jpa.version>
        <spring-test.version>4.0.2.RELEASE</spring-test.version>

        <!-- Jackson -->
        <jackson-mapper-asl.version>1.9.13</jackson-mapper-asl.version>

        <!-- Logging -->
        <log4j.version>1.2.17</log4j.version>
        <slf4j-simple.version>1.7.6</slf4j-simple.version>

        <!-- Database -->
        <hibernate-jpa-2.0-api.version>1.0.1.Final</hibernate-jpa-2.0-api.version>
        <hibernate-entitymanager.version>4.2.8.Final</hibernate-entitymanager.version>
        <hsqldb.version>2.3.2</hsqldb.version>

        <!-- JUnit -->
        <junit.version>4.11</junit.version>

    </properties>



    <build>

        <finalName>PROJECT_NAME</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>

    </build>



    <dependencies>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-core.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring-web.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-webmvc.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring-orm.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-tx.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring-data-jpa.version}</version>
        </dependency>



        <!-- Jackson -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson-mapper-asl.version}</version>
        </dependency>



        <!-- Logging -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j-simple.version}</version>
        </dependency>



        <!-- Database -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>${hibernate-jpa-2.0-api.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-entitymanager.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>${hsqldb.version}</version>
        </dependency>



        <!-- Optional testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-test.version}</version>
        </dependency>

    </dependencies>

</project>

TODO

  • Discover how put pom.xml in a subdirectory like maven/pom.xml
    <!-- example -->
    <build>
        <sourceDirectory>${project.basedir}/../src/main/java</sourceDirectory>
        <testSourceDirectory>${project.basedir}/../src/test/java</testSourceDirectory>

        <plugins>
            <plugin>
                <configuration>
                    <webXml>${project.basedir}/../src/main/webapp/WEB-INF/web.xml</webXml>
                    <warSourceDirectory>${project.basedir}/../src/main/webapp</warSourceDirectory>

Usage

mvn clean install
mvn tomcat:run
firefox http://localhost:8080/PROJECT_NAME/

Eclipse

  • Open Eclipse J2EE (Eclipse Luna version tested here) and create a new Dynamic Web Project in this way:
# on menu, click on 'File' -> 'New' -> 'Other...'
# select 'Web' -> 'Dynamic Web Project' and click 'Next ->'
# 'Project name:' "PROJECT_NAME"
# 'Location:' "PROJECT_PATH/eclipse"
# click 'Finish'



# on left panel, right click on 'PROJECT_NAME/Java Resources/src -> 'Delete' -> 'Ok'
# on left panel, right click on 'PROJECT_NAME' and select 'New' -> 'Folder'
# 'Folder name:' "src"
# click on 'Advanced > >'
# 'Link to alternate location': "../src/main/java"
# click 'Finish'
# on left panel, right click on 'PROJECT_NAME/src' and select 'Build Path' -> 'Use as Source Folder'



# on left panel, right click on 'PROJECT_NAME' and select 'New' -> 'Folder'
# 'Folder name:' "test"
# click on 'Advanced > >'
# 'Link to alternate location': "../src/test/java"
# click 'Finish'
# on left panel, right click on 'PROJECT_NAME/test' and select 'Build Path' -> 'Use as Source Folder'



# on left panel, right click on 'PROJECT_NAME' and select 'New' -> 'Folder'
# 'Folder name:' "resources"
# click on 'Advanced > >'
# 'Link to alternate location': "../src/main/resources"
# click 'Finish'
# on left panel, right click on 'PROJECT_NAME/resources' and select 'Build Path' -> 'Use as Source Folder'



# on left panel, right click on 'PROJECT_NAME/WebContent -> 'Delete' -> 'Ok'
# on left panel, right click on 'PROJECT_NAME' and select 'New' -> 'Folder'
# 'Folder name:' "WebContent"
# click on 'Advanced > >'
# 'Link to alternate location': "../src/main/webapp"
# click 'Finish'



# on left panel, right click on 'PROJECT_NAME/WebContent/WEB-INF' and select 'New' -> 'Folder'
# 'Folder name:' "lib"
# click on 'Advanced > >'
# 'Link to alternate location': "../target/PROJECT_NAME/WEB-INF/lib"
# click 'Finish'

# if needed,
# on left panel, right click on 'PROJECT_NAME/Java Resources/Libraries/Web App Libraries' and select -> 'Build Path' -> 'Remove from Build Path'
# on left panel, select all files under 'PROJECT_NAME/WebContent/WEB-INF/lib' and right click, select 'Build Path' -> 'Add to Build Path'