JAXB Maven Example

There might be a couple of Jaxb Maven plugins around causing some confusion but we will settle to the maven-jaxb2-plugin because of this stack overflow answer.

The POM will look like this:

<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>com.dimitrisli.jaxb</groupId>
	<artifactId>JaxbShowcase</artifactId>
	<version>1.0</version>
	<build>
		<plugins>
			<plugin>
				<!-- jaxb plugin -->
				<groupId>org.jvnet.jaxb2.maven2</groupId>
				<artifactId>maven-jaxb2-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<!-- the package for the generated java classes -->
							<generatePackage>com.dimitrisli.jaxb.producedClasses</generatePackage>
							<!-- If the following not specified all xsd in resources are included -->
							<schemaIncludes>
								<include>sampleJaxb/CustomersOrders.xsd</include>
							</schemaIncludes>
							<!-- if you don't want old output -->
							<removeOldOutput>true</removeOldOutput>
							<!-- if you want verbosity -->
							<!-- verbose>true</verbose -->
						</configuration>
					</execution>
				</executions>
			</plugin>
			<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>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.2.6</version>
		</dependency>
		<dependency>
			<groupId>javax.xml</groupId>
			<artifactId>jaxb-impl</artifactId>
			<version>2.1</version>
		</dependency>
	</dependencies>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
</project>

Note how we explicitly specify the package directory structure of the produced java classes, the location of the XSD file, the level of verbosity. Also by default the outputted java classes are generated in /target/generated-sources/xjc.

The XSD file along with the sample XML data file were taken from this Microsoft tutorial and were placed in resources/sampleJaxb/ and resources/sampleXml/ respectively.

After placing the produced java classes from the target directory to the src/main/java location, the MainExample test class is showcasing the unmarshalling capabilities and the looping through the data coming from the xml now residing in the java classes:

package com.dimitrisli.jaxb;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.dimitrisli.jaxb.producedClasses.OrderType;
import com.dimitrisli.jaxb.producedClasses.Root;
import com.dimitrisli.jaxb.producedClasses.CustomerType;

public class MainExample {

	public static void main(String[] args) throws JAXBException,
			FileNotFoundException {
		JAXBContext context = JAXBContext
				.newInstance("com.dimitrisli.jaxb.producedClasses");
		Unmarshaller unmarshaller = context.createUnmarshaller();
		Root root = (Root) unmarshaller.unmarshal(new FileInputStream(
				"src/main/resources/sampleXml/CustomersOrders.xml"));
		
		System.out.println("first get customers:");
		System.out.println("====================");
		for (CustomerType currentCustomer : root.getCustomers().getCustomer()) {
			System.out.println(currentCustomer.getCompanyName());
			System.out.println(currentCustomer.getContactName());
			System.out.println(currentCustomer.getContactTitle());
			System.out.println(currentCustomer.getCustomerID());
			System.out.println(currentCustomer.getFullAddress());
			System.out.println(currentCustomer.getPhone());
			System.out.println("-----");
		}
		System.out.println("then get orders:");
		System.out.println("====================");
		for (OrderType currentOrder : root.getOrders().getOrder()) {
			System.out.println(currentOrder.getCustomerID());
			System.out.println(currentOrder.getEmployeeID());
			System.out.println(currentOrder.getOrderDate());
			System.out.println(currentOrder.getShipInfo().getShipAddress());
			System.out.println(currentOrder.getShipInfo().getShipName());
			System.out.println(currentOrder.getShipInfo().getShipName());
			System.out.println("-----");
		}
	}
}

The code can be found in this Github repository.

Spring, Quartz, Maven example

This is a quick head-start Maven project showcasing Spring and Quartz integration.

First the POM will look like:

<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>com.dimitrisli.springQuartz</groupId>
  <artifactId>SpringQuartz</artifactId>
  <version>1.0</version>
  <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>
  				<encoding>${project.source.sourceEncoding}</encoding>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring</artifactId>
      <version>2.5.6</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>org.quartz-scheduler</groupId>
    	<artifactId>quartz</artifactId>
    	<version>1.8.0</version>
    </dependency>
  </dependencies>
  <properties>
  	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
</project>

The main task we want to repeat is created inside of our bean:

package com.dimitrisli.quartz;

public class ProcessToExecute {

	public void output() {
		System.out.println("important stuff");
	}
}

When the job wakes up it will come to the executeInternal() method of ProcessToExecuteJob:

package com.dimitrisli.quartz;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class ProcessToExecuteJob extends QuartzJobBean{

	private ProcessToExecute processToExecute;
	
	public void setProcessToExecute(ProcessToExecute processToExecute){
		this.processToExecute = processToExecute;
	}
	
	@Override
	protected void executeInternal(JobExecutionContext jobContext)	throws JobExecutionException {
		processToExecute.output();
	}

}

Finally the bean config file is defined like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="processToExecute" class="com.dimitrisli.quartz.ProcessToExecute" />

<bean name="processToExecuteJob" class="org.springframework.scheduling.quartz.JobDetailBean">
	<property name="jobClass" value="com.dimitrisli.quartz.ProcessToExecuteJob" />
	<property name="jobDataAsMap">
		<map>
			<entry key="processToExecute" value-ref="processToExecute" />
		</map>
	</property>
</bean>

	<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail" ref="processToExecuteJob" />
		<property name="repeatInterval" value="5000" />
		<property name="startDelay" value="2000" />
	</bean>

	<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="processToExecuteJob" />
		<property name="cronExpression" value="0/5 * * * * ?" />
	</bean>

	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="jobDetails">
			<list>
				<ref bean="processToExecuteJob" />
			</list>
		</property>
		<property name="triggers">
			<list>
				<ref bean="simpleTrigger" />
			</list>
		</property>
	</bean>

</beans>

The source code can be found in this Github repository.

Maven Resources Filtering

There are times when we want to use some parameters that can be only specified during compile time. A fine way of implementing this is via Maven’s resources tag and the place is in the POM:

<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>com.dimitrisli.maven</groupId>
	<artifactId>MavenResourcesFiltering</artifactId>
	<version>1.0</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<build>

		<finalName>MavenResourcesFiltering</finalName>

		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>

		<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>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>
		</plugins>

	</build>
</project>

notice in particular the resources tag:

                 <resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>

The default filtering value for this directory is false so we need to override it switching filtering to true. This basically will scan the directory and search for ${} entries referring to POM properties or entries. All this is happening during compile time.

Say for instance we have a project.properties in /src/main/resources with the following entry:

${project.artifactId}
${project.version}
${project.build.sourceEncoding}

then after compilation in the produced jar we will find:

MavenResourcesFiltering
1.0
UTF-8

How to create rpm using Maven

Include in your POM the following:

	 <build>
...
            <finalName>GreatApp</finalName>
...	
               <plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>buildnumber-maven-plugin</artifactId>
			<version>1.0-beta-2</version>
			<executions>
				<execution>
					<phase>validate</phase>
					<goals>
						<goal>create</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<doCheck>false</doCheck>
				<doUpdate>false</doUpdate>
				<format>{0,number,#}</format>
				<items>
					<item>buildNumber</item>
				</items>
				<timestampFormat>{0,date,yyyy-MM-dd HH:mm:ss}</timestampFormat>
			</configuration>
		</plugin>
		<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>rpm-maven-plugin</artifactId>
				<version>2.0-beta-2</version>
				<configuration>
					<copyright>2010 - dimitrisli</copyright>
					<group>Application/Collectors</group>
					<release>${buildNumber}</release>
					<mappings>
						<mapping>
							<directoryIncluded>false</directoryIncluded>
							<directory>/usr/share/tomcat5/webapps</directory>
							<username>tomcat</username>
							<groupname>tomcat</groupname>
							<sources>
								<source>
									<location>${project.build.directory}/${finalWarName}.war</location>
								</source>
							</sources>
						</mapping>
					</mappings>
				</configuration>
			</plugin>
  </plugins>
...
  </build>
...
  <properties>
		<finalWarName>GreatFinalWarName</finalWarName>
  </properties>

And in the UNIX box where the rpm tool is installed run:

mvn package rpm:rpm