Java EE sample

Last Thursday, March 27th, 2014, I offered a talk called “Introduction to Java EE”, as part of our local developers group’s event, Betabeers. It was an amazing experience for me.

20140327BetabeersI started off with a short theoretical introduction. Later on, I briefly showed some of the API of Java EE through a small CRUD sample application for registering ship data. I talked about JPA, EJB, Bean Validation, JSF, Managed Beans, JAX-RS and JAAS.

Here you have the PowerPoint that supported my presentation:


How to assemble an standalone application using Maven

I’m a newbie at Maven, but I’m becoming a fan. Today I’d like to talk about a simple task, which Netbeans performs automatically in Ant based standalone project but doesn’t in Maven ones, assembling the library dependencies in a lib folder along with the jar file:

JMSBasicClientScreenshot

First of all, I had to add the Maven Dependency plugin to my pom.xml file, this action created a folder called dependency in my Maven target directory when a built the project:


<plugin>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <phase>process-sources</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <outputDirectory>${targetdirectory}</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Later on, I also put the Maven JAR plugin with a configuration in order to add the CLASSPATH to the manifest file, using the same prefix (lib) I wanted to use in the assembly:


<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>                            
        <classpathPrefix>lib/</classpathPrefix>                            
        <mainClass>com.wordpress.fcosfc.JMSBasicClient</mainClass>
      </manifest>
     </archive>
  </configuration>
</plugin>

The following task was writing a custom assembly descriptor for the Maven Assembly plugin and putting in the folder src/main/assembly:


...
<id>bin</id>
<formats>
  <format>zip</format>
</formats>
<fileSets>
  <fileSet>
    <directory>${project.build.directory}</directory>
    <outputDirectory>/</outputDirectory>
    <includes>
      <include>*.jar</include>
    </includes>
  </fileSet>
  <fileSet>
    <directory>${project.build.directory}/dependency</directory>
    <outputDirectory>lib</outputDirectory>
  </fileSet>
</fileSets>
...

Finally, I had to add the Maven Assembly plugin to my pom.xml file:


<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/customAssembly.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> 
      <phase>package</phase> 
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

References