Chris' Tech Blog

August 27, 2010

Maven filtering with deployment profiles

Filed under: Maven — Chris Ey @ 4:32 am

When you set up a maven project, especially for web application projects (producing a WAR file), you often need to configure the WAR file before it gets deployed. I use maven filtering together with maven profiles.

There are tons of ways to accomplish this, but I prefer to integrate deployment properties into the WAR itself. This way, the build process already produces a stand-alone WAR file ready to use, and you don’t need an additional step to modify the WAR content.

This is how you can accomplish this (pom.xml):

	<build>
		<resources>
			<resource>
				<directory>${basedir}/src/main/java</directory>
				<filtering>true</filtering>
				<includes>
					<include>**</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>
					${basedir}/src/main/webapp/WEB-INF
				</directory>
				<includes>
					<include>web.xml</include>
				</includes>
				<filtering>true</filtering>
				<targetPath>..</targetPath>
			</resource>
			[...]
		<resources>
		[...]
		<filters>
			<filter>src/main/filters/filter-${env}.properties</filter>
		</filters>
	</build>
	<dependencies>
		[...]
	</dependencies>
	<properties>
		<!-- This is the default profile if no profile is selected on the command line -->
		<env>dev</env>
		[...]
	</properties>
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<env>dev</env>
			</properties>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<env>prod</env>
			</properties>
		</profile>
	<profiles>

Now, create one filter file for each profile (for the two profiles dev and prod in the example above):

src/main/filters/filter-dev.properties
src/main/filters/filter-prod.properties

insert filter token as described here, and execute your maven with a profile for deployment:

mvn clean install -P prod

August 22, 2010

Solar Roadways: Driving on solar panels!

Filed under: Solar — Chris Ey @ 2:52 pm

This is truly “Out Of The Box” thinking:

http://www.consciousmedianetwork.com/video/2010/061810.htm

In short: They are trying to develop a pavement that is made of solar panels, where you can drive on and which produces energy at the same time. Amazing idea! Would love to see this become reality.

August 21, 2010

Hibernate Event Listeners – Mappings for Spring

Filed under: Hibernate, Spring — Chris Ey @ 1:15 am

When I wanted to use hibernate event listeners or interceptors to modify an entity before it is saved and after it is loaded from the DB, I did not find a satisfying list of available events! Fortunately I found this post (in German) which simply listed all hibernate mappings from the code:

http://www.nautsch.net/2008/05/16/hibernate-eventlisteners-in-spring-konfigurieren/

I think – as I too didn’t find a better documentation about it – this is a great idea.

And it proves once more: Code is the best documentation. :)

static {
                eventInterfaceFromType = new HashMap();

                eventInterfaceFromType.put("auto-flush", AutoFlushEventListener.class);
                eventInterfaceFromType.put("merge", MergeEventListener.class);
                eventInterfaceFromType.put("create", PersistEventListener.class);
                eventInterfaceFromType.put("create-onflush", PersistEventListener.class);
                eventInterfaceFromType.put("delete", DeleteEventListener.class);
                eventInterfaceFromType.put("dirty-check", DirtyCheckEventListener.class);
                eventInterfaceFromType.put("evict", EvictEventListener.class);
                eventInterfaceFromType.put("flush", FlushEventListener.class);
                eventInterfaceFromType.put("flush-entity", FlushEntityEventListener.class);
                eventInterfaceFromType.put("load", LoadEventListener.class);
                eventInterfaceFromType.put("load-collection", InitializeCollectionEventListener.class);
                eventInterfaceFromType.put("lock", LockEventListener.class);
                eventInterfaceFromType.put("refresh", RefreshEventListener.class);
                eventInterfaceFromType.put("replicate", ReplicateEventListener.class);
                eventInterfaceFromType.put("save-update", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("save", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("update", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("pre-load", PreLoadEventListener.class);
                eventInterfaceFromType.put("pre-update", PreUpdateEventListener.class);
                eventInterfaceFromType.put("pre-delete", PreDeleteEventListener.class);
                eventInterfaceFromType.put("pre-insert", PreInsertEventListener.class);
                eventInterfaceFromType.put("pre-collection-recreate", PreCollectionRecreateEventListener.class);
                eventInterfaceFromType.put("pre-collection-remove", PreCollectionRemoveEventListener.class);
                eventInterfaceFromType.put("pre-collection-update", PreCollectionUpdateEventListener.class);
                eventInterfaceFromType.put("post-load", PostLoadEventListener.class);
                eventInterfaceFromType.put("post-update", PostUpdateEventListener.class);
                eventInterfaceFromType.put("post-delete", PostDeleteEventListener.class);
                eventInterfaceFromType.put("post-insert", PostInsertEventListener.class);
                eventInterfaceFromType.put("post-commit-update", PostUpdateEventListener.class);
                eventInterfaceFromType.put("post-commit-delete", PostDeleteEventListener.class);
                eventInterfaceFromType.put("post-commit-insert", PostInsertEventListener.class);
                eventInterfaceFromType.put("post-collection-recreate", PostCollectionRecreateEventListener.class);
                eventInterfaceFromType.put("post-collection-remove", PostCollectionRemoveEventListener.class);
                eventInterfaceFromType.put("post-collection-update", PostCollectionUpdateEventListener.class);
                eventInterfaceFromType = Collections.unmodifiableMap( eventInterfaceFromType );
}

And this is how you can use this information:

In Spring, you can configure an event listener for a session factory:

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        [...]

        <property name="eventListeners">
            <map>
                <entry key="save-update">
                    <ref local="yourSaveOrUpdateListener" />
                </entry>
                <entry key="save">
                    <ref local="yourSaveOrUpdateListener" />
                </entry>
                <entry key="update">
                    <ref local="yourSaveOrUpdateListener" />
                </entry>
                <entry key="post-load">
                    <ref local="yourPostLoadListener" />
                </entry>

                [...]

            </map>
        </property>

        [...]

    </bean>

You can use the key in Hibernate’s map above to know which events are available and how to name the key in the spring configuration. The class name above tells you which interface to implement for the given listener.

Powered by WordPress