GWT - adding external model package with JPA annotations to client project.

 Usually we want to use our model classes inside of the GWT project. But it is impossible without additional configuration of a model package.

 

The first step is to add file model.gwt.xml to the model package for conversion of the regular java package to the GWT module.

 

model.gwt.xml :

<module>
	<inherits name='com.google.gwt.user.User' />

	<source path="model"></source>
</module>

 

The second step add this new module to the main GWT module. My GWT project’s name is ‘web’.

 

web.gwt.xml :

<module rename-to='web'>
	<inherits name='com.google.gwt.user.theme.clean.Clean' />
	<inherits name="com.google.gwt.resources.Resources" />
	<inherits name="com.sencha.gxt.ui.GXT"></inherits>
	<inherits name='com.XX.model'/>


	<entry-point class='com.XX.web.client.MainApp' />


	<!-- Specify the paths for translatable code -->
	<source path='client' />
	<source path='shared' />
	
	<set-property name="user.agent" value="safari"/>
	<collapse-all-properties />
</module>

 

The third step we combine the modules using Maven and add support for JPA annotations.

 

web/pom.xml :

 

<?xml version="1.0" encoding="UTF-8"?>
<project>
....
	
	<dependencies>

		<dependency>
			<groupId>com.XX</groupId>
			<artifactId>model</artifactId>
			<version>${project.version}</version>
		</dependency>
		
		<!-- Add JPA annotations -->

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
                <version>3.6.3.Final</version>
			<exclusions>
				<exclusion>
					<groupId>cglib</groupId>
					<artifactId>cglib</artifactId>
				</exclusion>
				<exclusion>
					<groupId>dom4j</groupId>
					<artifactId>dom4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	....
</dependencies>
	<build>


		<plugins>
...
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>gwt-maven-plugin</artifactId>
				<version>${gwtmaven}</version>
				<configuration>
					<logLevel>${gwt.loglevel}</logLevel>
					<style>${gwt.outputstyle}</style>
					<gwtVersion>${gwtversion}</gwtVersion>
					<compileReport>true</compileReport>
					
					<sourcesArtifacts>
						<artifact>org.codehaus.mojo:model</artifact>
					</sourcesArtifacts>

					<runTarget>index.html</runTarget>
					<hostedWebApp>${project.build.directory}/${project.build.finalName}</hostedWebApp>
				</configuration>
				<executions>
					<execution>
						<phase>prepare-package</phase>
						<goals>
							<goal>compile</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<argLine>-Xmx216m</argLine>
					<additionalClasspathElements>
						<!-- allows to move the .gwt.xml file to resources folder -->
						<additionalClasspathElement>src/main/java</additionalClasspathElement>
						<additionalClasspathElement>src/test/java</additionalClasspathElement>
						<additionalClasspathElement>${project.basedir}/src/main/resources</additionalClasspathElement>

						<additionalClasspathElement>\${project.build.sourceDirectory}</additionalClasspathElement>
						<additionalClasspathElement>\${project.build.testSourceDirectory}</additionalClasspathElement>
					</additionalClasspathElements>
					<useManifestOnlyJar>false</useManifestOnlyJar>
					<forkMode>always</forkMode>
					<systemProperties>
						<property>
							<name>gwt.args</name>
							<value>-out target/gwt-tests</value>
						</property>
						<property>
							<!-- Setting this to true (default) causes a exception first time 
								test are run Gwt issue_6443 -->
							<name>gwt.persistentunitcache</name>
							<value>false</value>
						</property>
					</systemProperties>
				</configuration>
			</plugin>


		<!-- allows to move the .gwt.xml file to resources folder -->
		<resources>
			<resource>
				<directory>../model/src/main/java</directory>
				<includes>
					<include>*.java</include>
				</includes>
			</resource>
			<resource>
				<directory>../model/src/main/resources</directory>
				<includes>
					<include>**/*.gwt.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/client/**/*.java</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.gwt.xml</include>
				</includes>
			</resource>
		</resources>
	</build>

</project>

 

The structure of packages:

 

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com