giovedì 24 marzo 2016

How to move a web project from Glassfish to Tomcat?

Today I stumbled upon a web project that was configured for Glassfish instead of Tomcat. Since I am very fond of Tomcat, which integrates perfectly with my Eclipse IDE, I did not want to change my forma mentis and tools just to comply with the former configuration.

Unfortunately, when I tried to launch the project, it was not reading properly static resources (e.g., CSS files), since Glassfish has an XML configuration file (which is a substitute of the plain old web.xml file) in which you specify the context root of your project, i.e.:

http://localhost:8080/context-root/the-page-you-want

Tomcat is configured so as to use as context root the name of the project. So, how to fix this?
A solution can be found here, and is applied as follows:

  1. Project -> Properties -> Web Project Settings
  2. Modify the context root with the name you want and confirm
  3. Stop Tomcat
  4. Clean the Tomcat server by right-clicking on the server itself and selecting Clean
  5. Restart the server
  6. Confirm that you want to change the context root
...and you are done!

venerdì 11 marzo 2016

Use JPA with Maven projects in Eclipse

Want to build your own metamodel in Eclipse?

Prerequisite: use EclipseLink
Add the following to your pom.xml file:


        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.1</version>
            <scope>provided</scope>
        </dependency>

First step: Modify your pom.xml file
The pom.xml file has to be modified to include the following plugin:

            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.2.4</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                            </processors>
                            <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    
                    <dependency>
                        <groupId>org.eclipse.persistence</groupId>
                        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
                        <version>2.6.1</version>
                    </dependency>
                </dependencies>
            </plugin>

Second step: add the Generated-sources folder
Then, open the project properties and move to Java Build Path. In the "Source" tab, select "Add folder" and then add "target/generated-sources".
Build the project.

Third step: compile
Finally, again in project properties, select the JPA menu, select EclipseLink as a platform, allow Discover annotated classes automatically, and then press Apply. From this point on, the generated-sources folder should contain your compiled metamodel.